Basic question regarding variable scope
Hi all!
Just a super quick question about variable scope. Lua Code:
For the code snippet above, it would print 1 for both testVar. The question is, can you change the value of outer variable even if you declare a new local variable with an outer one's name within a function? or should I prevent such circumstances if I'd like to do so? |
Variables are values of places in memory, when you create a new local variable inside that function, that is an entirely new memory position the value is stored in.
Every time you run that function the local variable is created anew, and will get the "latest" from the global variable (there's no static variables in Lua). The only reason you'd want to differentiate the variable naming in a scenario like this is to avoid confusion, nothing more. |
Quote:
So, Lua doesn't have an ability to access to an address of variable, does it? (Like that is in C++ and so on) |
Quote:
It might help to know what you are trying to accomplish. |
Quote:
Lua Code:
|
do-end-blocks create a scope, too. This way you can change the outer testVar.
Lua Code:
|
|
Quote:
Thanks for letting me know! Quote:
Quote:
|
All times are GMT -6. The time now is 04:03 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI