• Check out the results of the Techtree Contest #19!
  • Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 22nd Icon Contest: Creep Abilities is now concluded, time to vote for your favourite set of icons! Click here to vote!

[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
 
It doesn't matter which gg is called, you shouldn't even do something like that anyways.

It depends on how jasshelper compiles code.
 
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.
 
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.
 
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.
Back
Top