• 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] Global triggers and local triggers.

Status
Not open for further replies.
Level 11
Joined
Oct 11, 2012
Messages
711
Hi there.
My first quesion: please see the two examples below:
JASS:
function InitTrig_test takes nothing returns nothing
    set gg_trg_test = CreateTrigger()
    call DisableTrigger(gg_trg_test) //Disable it first then enable it when I want to use it.
    .........
endfunction

However, when using a local trigger, it cannot be turned off:
JASS:
function onInit takes nothing returns nothing
    local trigger t = CreateTrigger()
    call DisableTrigger(t) //This is useless because I don't know how to turn it on later unless store it in some place
    .....
endfunction
So if my map has lots of triggered spells, is it better to use global triggers since disabled global triggers won't be fired? For example, I have 500 spells using global trigger variables and 490 of them are turned off, then when a unit casts a spell, only 10 triggers will be fired (checking the conditions). But if I use 500 local trigger variables, then 500 of them will all be fired, right?

Second question: on map initialization, are there differences between global triggers and local triggers? They all got initialized, right? Even the disabled global triggers? I am concerning about hitting the op limit.
 
If it is disabled and you need to enable it later, you should use global triggers. We only use locals because they are easy to declare and you usually don't need to do anything with the trigger later on. If you do need to, make it global. That is the lovely thing about JASS. You can change things like that so easily depending on whatever you need. And you feel cool doing it.

Globals and local triggers are essentially equivalent. But it is best to use locals when you can. If you clutter up the globals block with unnecessary globals, it can slow down the performance of accessing globals. Now, in realistic cases, this only makes a difference of microseconds, but it is usually just a matter of good habits. :)
 
Level 11
Joined
Oct 11, 2012
Messages
711
If it is disabled and you need to enable it later, you should use global triggers. We only use locals because they are easy to declare and you usually don't need to do anything with the trigger later on. If you do need to, make it global. That is the lovely thing about JASS. You can change things like that so easily depending on whatever you need. And you feel cool doing it.

Globals and local triggers are essentially equivalent. But it is best to use locals when you can. If you clutter up the globals block with unnecessary globals, it can slow down the performance of accessing globals. Now, in realistic cases, this only makes a difference of microseconds, but it is usually just a matter of good habits. :)

Got it, thnx! :) +Rep if I can.
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
Purgeandfire said:
If you clutter up the globals block with unnecessary globals, it can slow down the performance of accessing globals

Hold on, the access time to a global is not O(1)? Are all the globals inside some kind of table / data structure?

Dr Super Good said:
For the most part global triggers are preferable as that leaves you with some form of reference to turn them on and off if required.

This would come at the cost of slight increase of space / RAM, most likely negligible as Purgeandfire points out. But if the OP is writing on a system / hardware with very limited memory, locals might be preferable where possible.
 

LeP

LeP

Level 13
Joined
Feb 13, 2008
Messages
543
Hold on, the access time to a global is not O(1)? Are all the globals inside some kind of table / data structure?

[...]

See this and this.

So as the hashtable *probably* does not get resized, yes more globals slow down your programm. But i don't think it's noticeable at all.
If you want to test it yourself here is a list of ~300_000 (11mb file) variables which all map to the same bucket. I don't even know if wc3 allows so many variables tbh.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
Hold on, the access time to a global is not O(1)? Are all the globals inside some kind of table / data structure?
They are stored in a hashtable (Computer Science data structure not JASS type). This is why JASS is technically run by an interpreter as every function and variable name is resolved at run time into an address rather than at compile time. This is the main reason JASS is so painfully slow. In contrast Galaxy (SC2) compiles script with static links so uses a virtual machine in run time that directly looks up addresses. This is why Galaxy actually is quite a bit faster than JASS (it is slowed down a bit due to better error checking, it is virtually impossible to crash SC2 using triggers alone). Why they decided JASS should resolve all names at run time is beyond me and reeks of pure laziness. At least languages like Python have an excuse for run time name resolution of all variables (yes, Python stores everything in a complex tree of dictionaries or at least did), it is designed to do stuff compiled languages cannot really do.

But if the OP is writing on a system / hardware with very limited memory, locals might be preferable where possible.
Modern computers that can run WC3 most certainly are not memory limited. Even mobile phones have 1-2 GB of memory minimum now (unless budget, heck they are phones not even a computer). If you have 20,000 triggers chances are you do not need so many.
 
Status
Not open for further replies.
Top