Log In  

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.

P#32474 2016-11-14 17:35 ( Edited 2017-01-09 01:40)

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.

P#32478 2016-11-14 21:19 ( Edited 2016-11-15 02:47)

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.

P#32512 2016-11-15 13:37 ( Edited 2016-11-15 18:37)

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?!"

P#32518 2016-11-15 14:25 ( Edited 2016-11-15 19:26)

X-Files Theme

P#35296 2017-01-08 20:40 ( Edited 2017-01-09 01:40)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 00:39:10 | 0.010s | Q:17