WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Basic question regarding variable scope (https://www.wowinterface.com/forums/showthread.php?t=56308)

seyan777 06-24-18 07:36 PM

Basic question regarding variable scope
 
Hi all!

Just a super quick question about variable scope.

Lua Code:
  1. local testVar = 1;
  2.  
  3. function testFunc()
  4.     local testVar = testVar;
  5.  
  6.     print(testVar);
  7.  
  8.     testVar = 2;
  9. end
  10.  
  11. testFunc();
  12.  
  13. print(testVar);

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?

p3lim 06-24-18 07:42 PM

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.

seyan777 06-24-18 08:16 PM

Quote:

Originally Posted by p3lim (Post 328429)
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.

Hi p3lim,

So, Lua doesn't have an ability to access to an address of variable, does it?
(Like that is in C++ and so on)

myrroddin 06-25-18 09:59 AM

Quote:

Originally Posted by seyan777 (Post 328430)
Hi p3lim,

So, Lua doesn't have an ability to access to an address of variable, does it?
(Like that is in C++ and so on)

The full implementation of Lua might, I don't know, and haven't checked. However, in WoW, we do not get the full Lua implementation. As one example, we have no access to i/o for obvious security reasons.

It might help to know what you are trying to accomplish.

myrroddin 06-25-18 10:17 AM

Quote:

Originally Posted by seyan777 (Post 328428)

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?

Not easily, if at all. It is easier and recommended to have different names for variables because of this reason. For simplicity's sake, I will rename the variables and add comments.
Lua Code:
  1. local outVar = 1 -- this variable is global scope to the entire file
  2.  
  3. local addonName, pvtTable = ... -- both of these are local to ALL Lua files within the same addon
  4.  
  5. local function TestFunction() -- this function is global scope to the entire file
  6.     -- assign a local variable that has the scope of just this function,
  7.     -- and give it the value of the file's version. Naming is irrelevant
  8.     local outVar = outVar -- value == 1
  9.  
  10.     outVar = 2 -- we are still talking about the local variable, == 2
  11.     -- the global outVar is still == 1 and we haven't overwritten it
  12. end
  13.  
  14. local TestFunction2() -- this is what you want
  15.     local innerVar = outVar -- innerVar == 1
  16.     outVar = 2 -- outVar == 2, no really this time!
  17. end
  18.  
  19. function TestFunction3() -- this function is global across the entirety of WoW (careful!)
  20.     -- global names are 99.99% bad
  21.     -- you have no idea who else named a function or variable exactly the same
  22.     -- which will have conflicts and crashes and unexpected results
  23. end

Rilgamon 06-25-18 03:47 PM

do-end-blocks create a scope, too. This way you can change the outer testVar.
Lua Code:
  1. function testFunc()
  2.  
  3. do
  4.     local testVar = testVar;
  5.  
  6.     print(testVar);
  7. end
  8.  
  9.     testVar = 2;
  10. end

Seerah 06-25-18 06:34 PM

http://lua-users.org/wiki/ScopeTutorial

seyan777 06-25-18 07:07 PM

Quote:

Originally Posted by myrroddin (Post 328434)
The full implementation of Lua might, I don't know, and haven't checked. However, in WoW, we do not get the full Lua implementation. As one example, we have no access to i/o for obvious security reasons.

It might help to know what you are trying to accomplish.

Ah... I see...

Thanks for letting me know!

Quote:

Originally Posted by Rilgamon (Post 328442)
do-end-blocks create a scope, too. This way you can change the outer testVar.
Lua Code:
  1. function testFunc()
  2.  
  3. do
  4.     local testVar = testVar;
  5.  
  6.     print(testVar);
  7. end
  8.  
  9.     testVar = 2;
  10. end

damn! didn't even think of using do-end within a function lel!

Quote:

Originally Posted by Seerah (Post 328443)

Yeah, I've already read the document, but it still didn't ease my curiosity whether you could still change the value of outer one even if you declare the same named variable within a function (or loops, do-end, etc.) or not ;)


All times are GMT -6. The time now is 04:03 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI