Thalassicus
Bytes and Nibblers
Any variable you use in the string portion, such as "f" above, is going to be a global variable.
This will not work:
However, this will work:Code:local filename = "FEATURE_CRATER"; include (filename); local className = "FeatureCrater"; local s = loadstring("f = className:new()"); s(); print(f:CanBe(10,5));
The local variable className works properly, because it is evaluated for its value to add to the string, rather than being used as part of the string to be loaded. On the global level, className is nil, and you'd get errors stating such.Code:local filename = "FEATURE_CRATER"; include (filename); local className = "FeatureCrater"; local s = loadstring("f = "..className..":new()"); s(); print(f:CanBe(10,5));
Okay, so I ran into a problem today where this would be helpful. I have a long table called BuildingStats where each value in the table is code to execute, and I would like to export it to XML for better modularity.
XML
Code:
<GameData>
. . .
<BuildingStats>
<Row>
<Type>Name</Type>
<Value>(not bExcludeName)</Value>
</Row>
. . .
Lua
Code:
function GetHelpTextForBuilding(iBuildingID, bExcludeName, bExcludeHeader, bNoMaintenance)
. . .
for row in GameInfo.BuildingStats() do
local statType = row.Type
local statValue = loadstring(row.Value)
statValue()
. . .
This gives an error that loadstring is nil. How would I modify it to work as indicated in the first quote?