Thread Tools Display Modes
06-24-18, 07:36 PM   #1
seyan777
An Aku'mai Servant
Join Date: Feb 2017
Posts: 35
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?

Last edited by seyan777 : 06-24-18 at 07:38 PM.
  Reply With Quote
06-24-18, 07:42 PM   #2
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
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.
  Reply With Quote
06-24-18, 08:16 PM   #3
seyan777
An Aku'mai Servant
Join Date: Feb 2017
Posts: 35
Originally Posted by p3lim View Post
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)
  Reply With Quote
06-25-18, 09:59 AM   #4
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Originally Posted by seyan777 View Post
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.
  Reply With Quote
06-25-18, 10:17 AM   #5
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Originally Posted by seyan777 View Post

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

Last edited by myrroddin : 06-25-18 at 10:21 AM. Reason: Corrected myself
  Reply With Quote
06-25-18, 03:47 PM   #6
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
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
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
06-25-18, 06:34 PM   #7
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
http://lua-users.org/wiki/ScopeTutorial
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
06-25-18, 07:07 PM   #8
seyan777
An Aku'mai Servant
Join Date: Feb 2017
Posts: 35
Originally Posted by myrroddin View Post
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!

Originally Posted by Rilgamon View Post
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!

Originally Posted by Seerah View Post
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
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Basic question regarding variable scope

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off