• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Global and local variable with the same name.

Status
Not open for further replies.
Level 11
Joined
Oct 11, 2012
Messages
711
Hi, guys. Please see the example below:
JASS:
globals
    //Declares a global group variable "gg"
    gg = CreateGroup()
endglobals

function test takes nothing returns nothing
    //Declares a local group varialbe also named "gg"
    local gg = CreateGroup()
    //Which "gg" the following function is calling? The global variable or the local variable?
    call GroupEnumUnitsInRange(gg,x,y,100.,null)
    ......
endfunction
 
Level 12
Joined
Feb 22, 2010
Messages
1,115
It doesn't matter which gg is called, you shouldn't even do something like that anyways.

It depends on how jasshelper compiles code.
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
JASS:
globals
    //Declares a global group variable "gg"
    group gg = CreateGroup()
endglobals

function test takes nothing returns nothing
    //Declares a local group varialbe also named "gg"
    local group gg = CreateGroup()
    //Which "gg" the following function is calling? The global variable or the local variable?
    call GroupEnumUnitsInRange(gg,x,y,100.,null)
    ......
endfunction

the local gg will be called. (not 100% sure about merging with structs and whatnot, but thats how vanilla jass works)

also, this is not proper code to write. under no cases should a global have the same naming syntax as a local variable.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
It's called name shadowing, is supported by Jass and every other language in existence, and has no practical uses (beside reusing names of course).

The variable closest to the scope referencing it will be used.
In this case the local is in the same scope as the code referencing it, so it will be used.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Well, it's still a pain to use in GUI, because how the GUI is splitted in several jass functions.
Even a simple if/then, and since a local variable can be used only inside where it has been declared ...
Ofc a GUI user could use it together with a temp global variable, gogo spaghetti code, meh just use jass for that.
 
Status
Not open for further replies.
Top