I feel like it hasn't been said out loud that _init() seems like it breaks the normal rules of scope.
The below code works fine, printing out the string stored in a:
function _draw() cls() print(a,10,10) end function _init() a="yo" end |
edit: Technically, no rules are broken, as _init is called first, and since the variable assignment isn't local, it refers to the variable in the global scope, so it all works out.
But it still feels weird.
I rationalized this as lua maintaining a hashtable of <name,value> pairs that will just return nil for any key lookup (i.e. a variable name) until it's been set somewhere. Whether that's actually what's happening, I don't know. Feels less weird though :)
However, I've noticed there's a slight performance difference between accessing 'real' global variables and ones declared within an inner scope, so they may not actually be the same thing (in the tests I did, 'real' ones were faster to access than 'runtime' ones, though it only becomes apparent over several hundred iterations).
That definitely feels weird.
It sounds like you figured it out, and it only felt weird because of the order of the function definitions is different from the order of execution, is that correct? Just wondering if there were any remaining questions.
I almost want to edit parts of the manual into a fantasy welcome document for the dev kit focusing on the structure, best practices, and quirks.
I guess my ultimate dream is for more code that makes things easier when someone sees a game and wonders "how did they do that?!"
[Please log in to post a comment]