HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

A little chat about variables.

07-21-2004, 04:28 AM#1
Psyny
Hello,

Iam a bit confused of using of variables... Not using variables in itself... but the effects.

The standard Trigger Editor variables are global right ? So if i use one variable to store the triggering unit it dont will cause bugs ? If two players, or better , two units, cast the spell that are using the variable, at same time. What will happen ?

Lord Vexorian´s caster system uses the variables all the time, and ive read good things about this system.

If someone understand what i talking about. Plz helpme to clean my mind.

hehehe
Sorry my english.
Bye,
07-21-2004, 12:03 PM#2
SpadeZ
Well its kind of hard to understand what you're having problems with. First thing to keep in mind is that nothing runs at the same time. Everything runs in order of operation.
eg
Let 'spellunit' be a unit variable with no array and the initial value to be none.
Trigger
Events
Unit - A unit Begins casting an ability
Conditions
((Triggering unit) is A Hero) Equal to True
Actions
Set spellunit = (Triggering unit)

If a unit casts an ability, then the variable 'spellunit' will be set to the unit that just casted. But there will never be a time where two units cast an ability at once. The game will always read it as one unit casting, then another one straight after. The trigger will still run for the first unit, but then it will run for the second unit straight after.

Hope this helps abit.
07-21-2004, 12:24 PM#3
Ceo
I'd like to point out that you can have multiple threads running now and processers are even beginning to support it. However, triggers do run in order like you said.
07-21-2004, 01:27 PM#4
johnfn
Actually you are not entirely correct. Triggers can run at the same time, if they use waits. So variables storing the triggering unit or the casting unit are a must for triggers with waits, or else the trigger will fire again (while the current trigger is waiting), change the triggering unit, and then your trigger will get messed up because it is refering to the wrong unit. I hope that made sense, it seems a little confusing.

And according to weaaddar you should store all (triggering unit)s or (casting unit)s into variables because your taking them off the stack every time and slowing down the map a small bit and its more effecient to store them to unit variables.
07-21-2004, 03:54 PM#5
Shimrra
I think he meant what happens if he has a spell that sets a unit to a vaiable, but two units cast the spell at the same time. In this circumstance, one unit and will override the other and the results are varied, but I usually end up with the spell effects happening twice for the second unit and nothing for the first one. To get around this, you could use local variables.
07-21-2004, 06:40 PM#6
Anitarf
Basically, whatever you do in a trigger, if it's without a wait action, there is no way for things to get mixed up. The moment you do have a wait action, things can get messy. You must anticipate that during that wait, any other trigger may run, so the only way you can be sure the variables you use will be the same after the wait is if it is impossible for any other trigger to use them.

For example, in my map Era of War, I use a few variables to do custom spells (such as unit variables, unit group variables). The variables are arrays, so each player can get his own variable. A player can have only one hero, so I can be sure that those variables will be used by only one hero, so there's no possibility of conflict (also, this is important, because the cooldowns on the abilities are high enough for the spell effects to run completely before the spell can be recast).

Of course, using local variables kind of eliminates any problems you may have, because local variables stay the same in a thread (a trigger running) no matter what other triggers do in the meantime. (mental note: learn to use local variables. SOON!) Some event responses also work as local variables, but not all! "casting unit", for example, may not be the same after a wait, because another spell may be cast in the meantime. "summoning unit", on the other hand, stays the same in one thread even if other threads run in the meantime with different summoning units.
07-22-2004, 05:17 AM#7
Psyny
Thx all for helping.

Ive tested some triggered spells with massive number of units.
And what happens much times are just like Shimrra said, " results are varied, but I usually end up with the spell effects happening twice for the second unit and nothing for the first one."

But the things are more clear now...

local vars only acts in the trigger that they are used right ?
So, i can avoid major part of "two spell using the same reference" problems using local variables ?
07-22-2004, 02:23 PM#8
Shimrra
Quote:
Originally Posted by Psyny
So, i can avoid major part of "two spell using the same reference" problems using local variables ?

Yes. What I would suggest is that you make your spell in GUI first, using global variables, and then convert it to custom text (Jass). Once it's converted, replace the global variables with local variables. This'll save you the trouble of having to actually code the spell in Jass.