• 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.

[JASS] Are globals sometime better than locals?

Status
Not open for further replies.
Level 2
Joined
Jan 3, 2008
Messages
8
Hi,
Is it better to use a global, udg_RevSpoot(integerA), for example,
or "local it" everywhere I need it?
Here is what i used and the two options :

A custom script:
JASS:
function PlayerSlotState takes nothing returns boolean
    return ( GetPlayerSlotState(GetEnumPlayer()) == PLAYER_SLOT_STATE_PLAYING )
endfunction

Fisrt option (globals):
JASS:
function Create_n000 takes nothing returns nothing    
    if ( PlayerSlotState() ) then
        call CreateNUnitsAtLoc( 1, 'n000', GetEnumPlayer(), udg_SpootStart[GetConvertedPlayerId(GetEnumPlayer())], 270.0 )
    endif    
endfunction

function Create_ncop takes nothing returns nothing
    if ( PlayerSlotState() ) then
        call CreateNUnitsAtLoc( 1, 'ncop', GetEnumPlayer(), udg_SpootRevive[GetConvertedPlayerId(GetEnumPlayer())], 270.0 )
    endif
endfunction

function Trig_Time_elapsed_010_Actions takes nothing returns nothing
    call ForForce( udg_Team1, function Create_n000 )
    call ForForce( udg_Team1, function Create_ncop )
    call ForForce( udg_Team2, function Create_n000 )
    call ForForce( udg_Team2, function Create_ncop )
endfunction

//===========================================================================
function InitTrig_Time_elapsed_010 takes nothing returns nothing
    set gg_trg_Time_elapsed_010 = CreateTrigger(  )
    call TriggerRegisterTimerEventSingle( gg_trg_Time_elapsed_010, 0.18 )
    call TriggerAddAction( gg_trg_Time_elapsed_010, function Trig_Time_elapsed_010_Actions )
endfunction

Second option (locals):
JASS:
function Create_n000 takes nothing returns nothing
 local integer a
 local integer b
 local location array SS
    set SS[2] = GetRectCenter(gg_rct_Respawn_Blu)
    set SS[3] = GetRectCenter(gg_rct_Respawn_Teal)
    set SS[4] = GetRectCenter(gg_rct_Respawn_Viola)
    set SS[5] = GetRectCenter(gg_rct_Respawn_Giallo)
    set SS[6] = GetRectCenter(gg_rct_Respawn_Orange)
    set SS[8] = GetRectCenter(gg_rct_Respawn_Pink)
    set SS[9] = GetRectCenter(gg_rct_Respawn_Grey)
    set SS[10] = GetRectCenter(gg_rct_Respawn_Celeste)
    set SS[11] = GetRectCenter(gg_rct_Respawn_DarkGreen)
    set SS[12] = GetRectCenter(gg_rct_Respawn_Brown)
    if ( PlayerSlotState() ) then
        call CreateNUnitsAtLoc( 1, 'n000', GetEnumPlayer(), SS[GetConvertedPlayerId(GetEnumPlayer())], 270.0 )
    endif
    set a = 2
    set b = 12
    loop
        a > b
        set SS[a] = null
        set a = a + 1  
    endloop
endfunction

function Create_ncop takes nothing returns nothing
 local integer a
 local integer b
 local location array SS
    set SS[2] = GetRectCenter(gg_rct_Start_Blu)
    set SS[3] = GetRectCenter(gg_rct_Start_Teal)
    set SS[4] = GetRectCenter(gg_rct_Start_Giallo)
    set SS[6] = GetRectCenter(gg_rct_Start_Orange)
    set SS[8] = GetRectCenter(gg_rct_Start_Pink)
    set SS[9] = GetRectCenter(gg_rct_Start_Grey)
    set SS[10] = GetRectCenter(gg_rct_Start_Celeste)
    set SS[11] = GetRectCenter(gg_rct_Start_DarkGreen)
    set SS[12] = GetRectCenter(gg_rct_Start_Brown)
    if ( PlayerSlotState() ) then
        call CreateNUnitsAtLoc( 1, 'ncop', GetEnumPlayer(), SS[GetConvertedPlayerId(GetEnumPlayer())], 270.0 )
    endif
    set a = 2
    set b = 12
    loop
        a > b
        set SS[a] = null
        set a = a + 1  
    endloop
endfunction

function Trig_Time_elapsed_010_Actions takes nothing returns nothing
    call ForForce( udg_Team1, function Create_n000 )
    call ForForce( udg_Team1, function Create_ncop )
    call ForForce( udg_Team2, function Create_n000 )
    call ForForce( udg_Team2, function Create_ncop )
endfunction

//===========================================================================
function InitTrig_Time_elapsed_010 takes nothing returns nothing
    set gg_trg_Time_elapsed_010 = CreateTrigger(  )
    call TriggerRegisterTimerEventSingle( gg_trg_Time_elapsed_010, 0.18 )
    call TriggerAddAction( gg_trg_Time_elapsed_010, function Trig_Time_elapsed_010_Actions )
endfunction

The fact is that i used much udg_SpootRevive(intA) in much triggers so it may be better use it than "local it" in every trigger.
 
Last edited:
Level 5
Joined
Dec 17, 2007
Messages
145
It depends on the situation.

9 times out of 10, locals are better. Globals are only really useful for:


Referencing things on the map (that you made globals for, like rects)
Carrying information (Assuming you don't care about MUI)
Needing information in more than 1 function or trigger (without using H2I, etc.)
Using the same data (like a building on the map) in multiple triggers.


I think those are all the real, hard uses for Globals. Mostly anything else can be used with locals.
And btw, all your code up there is:

Insanely hard to read (no jass tags)
Really GUI-quality (Bad.)

Is this code you use, or code you made for example? Because if you actually use this code, you really need to optimize it. It's terrible (and not MUI because of the bj_ForloopindexA crap) But to really answer your question:

Yes, you want to use globals, simply because you use the information so often in so many different triggers.
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
Globals are actually useful for a lot of things, since you don't have to keep initializing them (for handles), so in processor-heavy situations where, for example, you need to constantly do an enumeration, having a single global group which is created upon map initialization and you reuse over and over (since GroupEnum doesn't even require you to clear the group, it does it automatically) is significantly faster than initializing a local group, creating it, enumerating it, <clearing it?>, destroying it, and nulling it every since time this enumeration needs to be performed.

DarkWulf: yes it's MUI, he doesn't have anything that halts for a unit of time (TriggerSleepAction/its variants, or a timer), though it's not the cleanest code either.

Nettuno: MUI means Multi (Unit) Instanceable. (Though people don't always refer to units, and often just mean generally multiinstanceable.)
 
Level 5
Joined
Dec 17, 2007
Messages
145
The bj_LoopIndexA (or whatever the hell it is), is not MUI. If another trigger that also uses bj_LoopIndexA runs at the exact same time, it can mess up.

Simply use 1 or 2 local integers (depends on what the first integer needs refrence to in the loop). Then you know for sure it's MUI.


AKA: Replace the bj_LoopIndexA thing with a local integer, and just increase that by 1 instead of the bj. It's faster and safer.
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
The bj_LoopIndexA (or whatever the hell it is), is not MUI. If another trigger that also uses bj_LoopIndexA runs at the exact same time, it can mess up.

Simply use 1 or 2 local integers (depends on what the first integer needs refrence to in the loop). Then you know for sure it's MUI.


AKA: Replace the bj_LoopIndexA thing with a local integer, and just increase that by 1 instead of the bj. It's faster and safer.
That will only happen if

  • he uses TriggerSleepAction inside either loop
  • he needs to store the index through a timer
  • a function, while within a bj_forLoopAIndex loop, calls another function which uses a bj_forLoopAIndex loop.
 
Level 19
Joined
Aug 24, 2007
Messages
2,888
Get A Jass NewGen Pack :D

There is no point of using globals for a thing you wont call later
Locals are better (faster)

And you dont need globals for these

(tip loop exitwhen a == 10 do actions for player(a) set a = a+1 instead of ForForce)
 
Level 3
Joined
Dec 28, 2007
Messages
46
And btw, all your code up there is:

Insanely hard to read (no jass tags)

Jeeze. You people are picky hehe. :xxd: I think its very easy to read. At least its formatted very cleanly. Syntax highlighting cant help THAT much can it? I guess maybe im just used to de-bugging C++...*shrugs*
 
Status
Not open for further replies.
Top