Posted 30 May 2014 - 04:40 PM
I've got a program I'm writing that I have a file with a serialized table stored for an input handler type thing with lables and fields and such. I want to be able to specify custom handler functions for some things and so I figured, I'd just put the function name as a string in the table and then use loadstring() to be able to call the function. Well it's not working…. This is an example of what I'm doing:
Serialized Table in file:
Now in the code I may do something like this, we'll say the unserialized table.fields is loaded into the variable fieldList for this example:
The problem I'm having is loadstring(fieldList[1].handler) is always returning nil. Even when I know that fieldList[1].handler is returning "aHandler" (and I have another function in the program called aHandler). Obvious the code above is just example code to show the problem I'm having because my program is a lot longer and I didn't want to post all of it atm. But I can assure you I've tested and the arg I'm using in loadstring definitely has the anticipated and correct value in it that is the name of a function in my program that I intend to be used as a handler function.
All of the examples I can find of loadstring show it working just like that. What am I doing wrong here?
Serialized Table in file:
{
fields = {
{
name="a_field",
x=1,
y=1,
default="Value",
handler="aHandler"
},
},
}
Now in the code I may do something like this, we'll say the unserialized table.fields is loaded into the variable fieldList for this example:
local fieldHandler=nil
if fieldList[1].handler then fieldHandler=loadstring(fieldList[1].handler) end
if fieldHandler then
fieldHandler(arg1,arg2)
end
function aHandler(arg1,arg2)
--Do stuff
end
The problem I'm having is loadstring(fieldList[1].handler) is always returning nil. Even when I know that fieldList[1].handler is returning "aHandler" (and I have another function in the program called aHandler). Obvious the code above is just example code to show the problem I'm having because my program is a lot longer and I didn't want to post all of it atm. But I can assure you I've tested and the arg I'm using in loadstring definitely has the anticipated and correct value in it that is the name of a function in my program that I intend to be used as a handler function.
All of the examples I can find of loadstring show it working just like that. What am I doing wrong here?
Edited on 30 May 2014 - 05:53 PM