Can a global ever be used simultaneously?

AceLegend90

New Member
Reaction score
6
As the title states, can a global variable ever be used in two instantaneous triggers at the same time?

For example, I often use a global variable 'tempUnit' and set it to the caster of a certain spell and use 'tempUnit' in another function that checks if a unit is an enemy or not in a 'GroupEnumUnitsInRange' call. My question is, if two spells are initiated, is there ever a chance that the setting of 'tempUnit' happen to be executed one after the other, therefore overriding one spell's effectiveness?

If so, I plan on using a private global variable in each spell that uses 'tempUnit'.

EDIT: This question is more about vJASS. Look at my second comment to try to understand what I'm asking. I'm asking if an entire function's code guaranteed to execute before another function's.
 

Vassilev

New Member
Reaction score
39
it is possible if you do not use wait functions at all..

Its much safer to just make a local variable... whats the fuss? :confused:
 

Hero

─║╣ero─
Reaction score
250
Once you use a global variable twice in 2 different places it is over written. Unless you use arrays, but then you will eventually run out of space.
 

AceLegend90

New Member
Reaction score
6
You guys aren't getting what I'm saying. I can't use locals because I use the global in another function (mainly due to "sliding units" spells).

I have code like this:

JASS:
set tempUnit = .caster
call GroupEnumUnitsInRange(.nears, GetUnitX(.hook), GetUnitY(.hook), 100., Condition(function HookSpell.NearConditions))


Then, in 'HookSpell.NearConditions':

JASS:
static method NearConditions takes nothing returns boolean
        if IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE) then
            return false
        elseif GetWidgetLife(GetFilterUnit()) <= 0. then
            return false
        elseif IsUnitAlly(GetFilterUnit(), GetOwningPlayer(tempUnit)) then
            return false
        elseif IsUnitInvisible(GetFilterUnit(), GetOwningPlayer(tempUnit)) then
            return false
        endif
        return true
endmethod


Since 'tempUnit' is set and used instantaneously, spells should work fine. What I'm worried about is, does the system ever do this:

JASS:
set tempUnit = someSpellCaster
set tempUnit = otherSpellCaster
GroupEnumUnitsInRange(blah)
GroupEnumUnitsInRange(blah)


Or if the function's entire code will be executed and gauranteed to do so in sequence.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
As the title states, can a global variable ever be used in two instantaneous triggers at the same time?

For example, I often use a global variable 'tempUnit' and set it to the caster of a certain spell and use 'tempUnit' in another function that checks if a unit is an enemy or not in a 'GroupEnumUnitsInRange' call. My question is, if two spells are initiated, is there ever a chance that the setting of 'tempUnit' happen to be executed one after the other, therefore overriding one spell's effectiveness?

If so, I plan on using a private global variable in each spell that uses 'tempUnit'.

EDIT: This question is more about vJASS. Look at my second comment to try to understand what I'm asking. I'm asking if an entire function's code guaranteed to execute before another function's.

I'm pretty sure it's guaranteed when you execute functions, but not when you evaluate them (run them, rather than use as a condition or use the return value).

I heard that in something like this:

JASS:
function A takes nothing returns boolean
    call TriggerSleepAction(5)
    return true
endfunction

function B takes nothing returns nothing
    if A() then
        call BJDebugMsg("true")
    else
        call BJDebugMsg("false")
    endif
endfunction


the evaluation of function A will not wait the 5 seconds. Instead, it would probably return null since it never reached a return line.

However, if you look at the PolledWait function, you'll see that it's a created function, and is executed, yet it still works like a TriggerSleepAction -- the original function waits for the Polled Wait function to finish.

Basically the editor works in "threads". One thread starts, and the other threads must wait until the thread finishes or is paused by a wait of some sort. Then, the next thread in line will be run. Think of the script you wrote as a que. The system holds commands into your RAM, until they are ready to be processed by a processor.

Start reading here if you don't care how it works. :p
To answer your question - the second function will be put in the que after the first function. If the first function waits for any amount of time, it then goes to the back of the que, and the second function will start to execute. If there is no wait, then the first function will finish before even starting the second function.
 

AceLegend90

New Member
Reaction score
6
Okay, thank you very much.

I never use 'TriggerSleepAction' or 'PolledWait' functions anymore so I should be safe in that department.

+Rep
 

Sooda

Diversity enchants
Reaction score
318
TriggerSleepAction and custom PolledWait are both useful, everything does not need to run on timers. Keep simple thing in mind while coding, if you pause trigger store reference(s) to global variable(s) with local variable(s) or game cache. After pause load global value(s) with method you stored them.
 

quraji

zap
Reaction score
144
Functions won't cut into another function's execution, unless there is a TriggerSleepAction.
 

Viikuna

No Marlo no game.
Reaction score
265
What about events? If I have function which calls UnitDamageTarget and trigger with EVENT_UNIT_DAMAGED, doesnt it cut my function?
 

Sooda

Diversity enchants
Reaction score
318
Answering to your last question. Trigger without waits never gets interrupted, nothing "cuts" it. "Cutting" occurs when other trigger uses same variables which waiting trigger.

All variables are shareable among enabled triggers. Only trigger in front of first queue has access to variables. As soon as trigger execution encounters pause action trigger is moved to second queue. Second queue triggers are "frozen" until pause end. Trigger moving from first queue to second provokes new trigger execution in first queue until


There are only three trigger queues. First is global trigger queue with low priority, all triggers which fire are added here. Both other queues are designed to keep track of time. Second is paused trigger queue with medium priority, paused trigger from first queue gets moved here. Third is timer trigger queue with high priority, all triggers which use timer expire event are added here. Lower priority trigger execution always gets stopped when higher priority trigger execution needs to be continued.

When event occurs, firing trigger is added to first queue. Triggers are picked from queue and executed by their adding order. Action line by action line trigger gets executed until end.

All variables are shareable among triggers. Only executing trigger has privilege to change variables. When trigger execution encounters pause action then trigger is moved to second queue until time is passed. Paused trigger can not access to variables. Pause duration is used to process second trigger in queue, second trigger in queue gets privilege to access variables and is executed in the same way as our first trigger.

Second trigger execution stops immediately on currently processed action line, line is stored and later continued, and is moved once backward in first queue with all other triggers to make room for trigger with medium priority which actions execution is continued. When medium priority trigger gets entirely executed it is discarded until its event occurs again.

When expired timer queue trigger needs to run (timer expires), priority rule applies, medium priority trigger execution stops immediately on currently processed action line, line is stored and later continued, and is moved once backward in first queue (Second trigger is pushed to third, other triggers in first queue move also one step back).

When high priority trigger gets entirely executed, trigger with medium priority execution is continued, when finished follows trigger with low priority.

To sum it up triggers move between three queues. In first queue actual trigger executing takes place action line by action line. Before each new action line execution other queues with higher priority are checked for pause end or timer expiration. Trigger with highest priority is moved to front of first queue and gets privilege to access variables and continue its actions execution.

End of story.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top