992 posts
Posted 02 May 2012 - 10:47 PM
so this is just a little experiment in multi tasking (sort of) as such this is low priority. my question is how do you call a function when the name of the function you would like to call is stored in a string.
here is my code so far it does not run.
http://pastebin.com/jEtzKUGfthe problem is line 32 and 33
Thanks in advance for any help given.
1604 posts
Posted 02 May 2012 - 11:39 PM
The problem is that the functions don't exist in the environment of the function loaded with loadstring, they are local to your program.
You could try to change the functions to global to see if it works, but it's not recommended for a real program. A solution would be to create an environment where you define those functions, and then set it to the function loaded with loadstring:
local tEnv = {}
setmetatable(tEnv, { __index = _G }) -- this is to include the global functions (and apis) to your environment
function tEnv.testFunction()
print("Hello World!")
end
local sFunc = "testFunction()"
local func, err = loadstring(sFunc)
if func ~= nil then
setfenv(func, tEnv) -- set the environment for the function
func() -- run it
else
print("Error: ", err)
end
992 posts
Posted 03 May 2012 - 12:02 AM
I haven't gotten to environments in lua for dummies yet :)/>/> /jk
ok so that is a lot more complex that i thought it would be but i think i get what you are doing there you are making your own table and storing the functions there (and copying the pre existing functions across) then calling this a new environment so all calls go to the new environments meta table.
While I thank you for the time you took to write that reply I think for now i will end the project as it is a bit over my head.
1604 posts
Posted 03 May 2012 - 12:18 AM
:)/>/>
No problem, but there might be some other easier solution, just that this is the one that ocurred to me now. You can make your functions global, so they are in the environment when you call them from the loaded function.
Anyway, if you are trying to do multitasking, I would recommend you to check coroutines. I have an os with working multitasking, there's still some bugs to fix before I release it, but it's working, so it's possible.