Minty23185Fresh
March 27th, 2018, 19:12
In the <masterindex> code I came across a really slick use of a table. The values in the table can be function (names). This allows one to execute different functions based on a variable's value. Very similar to the switch-case statement from other languages (which lua seems to lack).
I am trying to implement such methodology in an extension of mine, and have run across a peculiarity. Depending on how/when I set up the table, sometimes the functions are there, sometimes not.
A really stripped down example follows.
If the table is populated in a function, things are good:
local tOne = { };
function runThis()
tOne = {
["runfOne"] = fOne,
["runfTwo"] = fTwo,
["runfThree"] = fThree,
};
Debug.console("tOne=", tOne);
end
Here are the results (note the appearance of "fn" for each element).
Runtime Notice: s'tOne=' | { s'runfOne' = fn, s'runfTwo' = fn, s'runfThree' = fn }
But if the table is prepopulated in the definition, the functions aren't there, the table is empty.
local tOne = {
["runfOne"] = fOne,
["runfTwo"] = fTwo,
["runfThree"] = fThree,
};
function runThis()
Debug.console("tOne=", tOne);
end
Runtime Notice: s'tOne=' | { }
This seems like an early/late binding sort of thing, but I wouldn't be surprised if it has to do with scope. All my problems seem to boil down to that.
I tried populating the table in onInit() and things work fine, so that's probably the place to do it.
But why can't I prepopulate in the modular scope table definition? Anyone have any ideas?
I am trying to implement such methodology in an extension of mine, and have run across a peculiarity. Depending on how/when I set up the table, sometimes the functions are there, sometimes not.
A really stripped down example follows.
If the table is populated in a function, things are good:
local tOne = { };
function runThis()
tOne = {
["runfOne"] = fOne,
["runfTwo"] = fTwo,
["runfThree"] = fThree,
};
Debug.console("tOne=", tOne);
end
Here are the results (note the appearance of "fn" for each element).
Runtime Notice: s'tOne=' | { s'runfOne' = fn, s'runfTwo' = fn, s'runfThree' = fn }
But if the table is prepopulated in the definition, the functions aren't there, the table is empty.
local tOne = {
["runfOne"] = fOne,
["runfTwo"] = fTwo,
["runfThree"] = fThree,
};
function runThis()
Debug.console("tOne=", tOne);
end
Runtime Notice: s'tOne=' | { }
This seems like an early/late binding sort of thing, but I wouldn't be surprised if it has to do with scope. All my problems seem to boil down to that.
I tried populating the table in onInit() and things work fine, so that's probably the place to do it.
But why can't I prepopulate in the modular scope table definition? Anyone have any ideas?