HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

The GUI Local Variable Trick

06-26-2008, 07:40 AM#1
Pyrogasm
So anyone who has used GUI probably knows about that weird, random trick you can do to make globals act like locals in GUI. For those of you don't, here's what it looks like:
Trigger:
Collapse Events
Unit - A unit starts the effect of an ability
Conditions
Collapse Actions
Custom script: local unit udg_MyUnit
Set MyUnit = (Triggering Unit)
Wait 2.00 game-time seconds
Unit - Kill MyUnit
Custom script: set udg_MyUnit = null
(MyUnit being a unit variable created in the Variable Editor.)


Anyway, what I'm wondering is this: why does it work? Do we have any clue what this does to the parser to make it work, because it seems like if it was compiled normally it simply wouldn't compile and it would look like:
Collapse JASS:
function Trig_MyTrigger_Actions takes nothing returns nothing
    local unit udg_MyUnit
    set udg_MyUnit = GetTriggerUnit()
    call PolledWait(2.00)
    call KillUnit(udg_MyUnit)
    set udg_MyUnit = null
endfunction
And one more question: does this bug still work if you're using the NewGen pack?
06-26-2008, 07:47 AM#2
Pheonix-IV
It works because by using the script "Local unit udg_MyUnit" you're defining the variable udg_MyUnit as a local unit variable. However, that definition doesn't occur until the game runs, so the variable appears to be global.
06-26-2008, 07:51 AM#3
Pyrogasm
Okay, that makes sense... but why doesn't it generate a syntax error?
06-26-2008, 07:56 AM#4
Jazradel
Just a bug with the syntax error. Something to do with the way it handles scope, presumably.

As an interesting side note, doing that with an array does cause a syntax error.
06-26-2008, 08:29 AM#5
cohadar
There are 2 udg_MyUnit variables, one local and one global.

It compiles somewhat like this:
Collapse JASS:
globals
    unit udg_MyUnit
endglobals

function Trig_MyTrigger_Actions takes nothing returns nothing
    local unit udg_MyUnit
    set udg_MyUnit = GetTriggerUnit()
    call PolledWait(2.00)
    call KillUnit(udg_MyUnit)
    set udg_MyUnit = null
endfunction


and since local variables take precedence over globals ones global variable is never used inside Actions (but still exists)
In fact if you would to remove the local unit udg_MyUnit from the beginning of action the action would be using global variable (no-mui)
06-26-2008, 08:48 AM#6
Captain Griffen
JASS == voodoo
06-28-2008, 05:32 PM#7
Kyrbi0
Y'know, I always confuse Pyrogasm and Phoenix-IV.
06-28-2008, 08:59 PM#8
Pyrogasm
Well then, if it's such a simple syntax thing... why doesn't it work for more than one variable per function...?
06-28-2008, 09:18 PM#9
Captain Griffen
Voodoo.

Blizzard's defensive programming is awful. It's probably a bug in their compiler that it works at all.
06-29-2008, 07:30 AM#10
cohadar
Quote:
Originally Posted by Pyrogasm
Well then, if it's such a simple syntax thing... why doesn't it work for more than one variable per function...?

Make a simple test map, compile it, go to logs/inputwar3map.j and tell us.
06-29-2008, 08:11 AM#11
Pheonix-IV
I'm going to go with Griffen's explanation. It's Voodoo, a Wizard probably did it, or at least was responsible for it.
06-29-2008, 09:11 AM#12
Gorman
Wizards dont do voodoo.
06-29-2008, 11:40 AM#13
MaD[Lion]
not voodoo, just bad programming :P
06-29-2008, 02:13 PM#14
Vexorian
Quote:
Originally Posted by Pyrogasm
Well then, if it's such a simple syntax thing... why doesn't it work for more than one variable per function...?
Blizz probably screwed up when they hacked it so it becomes non-case sensitive. There could be an off by one error or something like that, no idea.

BLizz were intending GUI people to do global shadowing like this, they even hacked the compiler so it becomes non-case-sensitive after you do that, so GUI guys don't have to bother with correct capitalization...
06-30-2008, 08:24 AM#15
cohadar
Quote:
Originally Posted by Vexorian
Blizz probably screwed up when they hacked it so it becomes non-case sensitive. There could be an off by one error or something like that, no idea.

BLizz were intending GUI people to do global shadowing like this, they even hacked the compiler so it becomes non-case-sensitive after you do that, so GUI guys don't have to bother with correct capitalization...

If they were so good to GUI guys why they did not add Remove functions to GUI.