As long as I can declare global variables and use random values in the code, I'll be able to achieve many things I have in mind
I'll keep you updated.
If you happen to use my Lua Scenario Template, you'll find that standard global variables are disabled by default as almost the first thing I do, but I do make a global variable called _global, which is a table you can store your global variables in. The reason for this is the way that Lua handles global variables.
All variables in Lua are global, unless you use 'local' to define them. In Lua, "tables" let you register key-value pairs, and are used for storing lots of information, and for creating data types with more than one value in them. Tables also return the "nil" value for all keys that are not in the table. This is relevant, because global variables are stored in a special "global" table, and it turns out that makes debugging more complicated.
Consider this code
Code:
local myBool = true
if mybool then
print("myBool is true")
end
You probably want this code to generate an error, since you have a typo in the if statement. Instead Lua looks at mybool, sees that it is not a local variable, then looks in the global table. The string "mybool" is not a key in the global table, so that table returns nil, and hence mybool is evaluated as nil, which happens to be falsy, so the print statement is skipped. You now have to try to figure out why your program isn't working. You can get really weird and inconsistent bugs easily.
Here's an example (the error is reported
here, and the fix/explanation the post after that). If you define a local variable outside of a function, it can be used and changed by any function in the file.
If I need to link files in both directions (one direction is accomplished with the require command), I typically write a "linking" function, usually to link the 'state table' (The 'state table' is the table that is converted to a string and attached to the end of a saved game, so we can save arbitrary information. TNO gave it the name state in an example, and the name stuck.)
Code:
local flagState = "flagState not linked"
local function linkState(tableInStateTable)
if type(tableInStateTable) == "table" then
flagState = tableInStateTable
else
error("linkState: linkState takes a table as an argument.")
end
end
flag = {}
flag.linkState = linkState
return flag