• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Using Damage as universal variable

Status
Not open for further replies.
Level 4
Joined
Aug 17, 2014
Messages
102
I have some doubts as of late. So for almost every spell I have so far in my rpg I used a variable named damage. Will this create a conflict? For example if two different spells and completely different triggers are used at the same time; lets say spell 1 deals (5x str damage) and spell 2 deals (7 x Intel damage) is it possible that due to the variable they use is set under the same name that they get mixed up? So spell 1 will deal spell 2's damage.
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,539
Triggers will never execute at the "same time". They're always ordered just like how the actions in a trigger execute from top to bottom. So as long as you do something like this you'll be fine:
  • example 1
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
      • (Ability being cast) Equal to Some Spell
    • Actions
      • Set CastingUnit = (Triggering unit)
      • Set TargetUnit = (Target unit of ability being cast)
      • Set Damage = 100.00
      • Unit - Cause CastingUnit to damage TargetUnit, dealing Damage damage of attack type Spells and damage type Normal
But where you can go wrong is with Waits/Timers. So in this next trigger we do the same thing but our variables are referenced after a 2.00 second Wait. That's a problem, because if any of our variables were to change at any point during those 2.00 seconds then it won't work properly. The damage might be the wrong number, or maybe the castingunit or targetunit will be the wrong units.
  • example 2
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
      • (Ability being cast) Equal to Some Spell
    • Actions
      • Set CastingUnit = (Triggering unit)
      • Set TargetUnit = (Target unit of ability being cast)
      • Set Damage = 500.00
      • Wait 2.00 seconds
      • Unit - Cause CastingUnit to damage TargetUnit, dealing Damage damage of attack type Spells and damage type Normal
To fix the problem in example 2 you have multiple options.
1) You can use multiple global variables (depending on your map this may create 100's of extra variables)
2) Use local variables (requires JASS knowledge)
3) Use shadowed global variables (this is a nice mix between GUI and JASS and can be very simple to use)

Search for "shadowing global variables" in this link (unless you already know what I'm talking about):
Things You Should Know When Using Triggers / GUI

And here's an example of using local variables that I have written in Lua. It should look very similar to the example 2 trigger I posted above, however I didn't bother putting in the Events/Conditions. So I store 3 local variables, the casting unit, the target unit of ability being cast, and the amount of damage to deal just like we did before. Then I create a timer that expires after 2.00 seconds (basically the same as "Wait 2.00 seconds" but even more precise and problem-free) that deals damage once expired.
Lua:
function SpellExample()
    local castingunit = GetTriggerUnit()
    local targetunit = GetSpellTargetUnit()
    local damage = 500
    TimerStart(CreateTimer(), 2.00, false, function()
        UnitDamageTarget(castingunit, targetunit, damage, false, true, ATTACK_TYPE_CHAOS, DAMAGE_TYPE_UNIVERSAL, nil)
        PauseTimer(GetExpiredTimer())
        DestroyTimer(GetExpiredTimer())
    end)
end
What makes local variables great is that they will not cause any of the problems that I mentioned before. Damage will always be equal to 500.00 and CastingUnit/TargetUnit will not change. This is because every time you run this code it's creating new unique local variables instead of replacing existing global variables. Variables in Lua are also great because you can name them whatever you want and you don't have to define their types (like Unit, Real, Integer, Point).
 
Last edited:
Level 12
Joined
Feb 22, 2010
Messages
1,115
If you are not sure, using separate variables for different things is always the safest option. By using single variable everywhere, at some point you will run into a trouble, like when a trigger causes another trigger to run.
 

Wrda

Spell Reviewer
Level 26
Joined
Nov 18, 2012
Messages
1,887
Variables only conflit if you have a trigger (A) that triggers, or runs another one (B) if they share the same variables. The trigger (B) that was triggered by another will run and the other one (A) will wait till it is finished, then the latter (A) finishes its execution.
  • A Trigger
    • Events
      • Player - Player 1 (Red) types a chat message containing test as An exact match
    • Conditions
    • Actions
      • Set Test = 100.00
      • Hero - Create Claws of Attack +15 and give it to Mountain King 0001 <gen>
      • Game - Display to (All players) the text: (String(Test))
  • Triggered by Another Trigger
    • Events
      • Unit - A unit owned by Player 1 (Red) Acquires an item
    • Conditions
    • Actions
      • Set Test = 300.00
      • Game - Display to (All players) the text: (String(Test))
Test in the first trigger will display 300 and not 100, and value 300. If the text display action was put before the item creation for the hero then it would display 100, but its value will be 300 after. If you put the variable and display text after the item creation, it will display 100 and value 100.
I would recommend using the least variables possible, because GUI variable editor is lame when full of them.
If you are not sure, using separate variables for different things is always the safest option.
Honestly, that would be stupid. We all just need to be conscious about we're doing.
 
Last edited:
Level 12
Joined
Feb 22, 2010
Messages
1,115
Honestly, that would be stupid. We all just need to be conscious about we're doing.
You can go ahead and continue with your "use least variables possible" strategy, thinking about every possible conflict when making a trigger and celebrate the fact that every time you did not create a new variable you saved 4 bytes of space in your RAM. But its just not going to work, at some point it will not work and you will need to create separate variables for stuff. You might as well abandon that non-sense idea from the start.
 
Level 4
Joined
Aug 17, 2014
Messages
102
Thanks, guys and thank you @Uncle for the well written explanation. I have mainly used basic gui due to lack of coding knowledge, but the shadowed variables looks interesting and a bit easy to use thank you for the link too lots of helpful info there.
 

Wrda

Spell Reviewer
Level 26
Joined
Nov 18, 2012
Messages
1,887
celebrate the fact that every time you did not create a new variable you saved 4 bytes of space in your RAM.
That wasn't even my point. My point was that GUI variable dialog sucks, simply because you add variables and they will stack up. By the end of project, if it's something big, you'll have difficulty in navigating the variables options when choosing a specific action, and it becomes a nuisance.
No big deal, but I would appreciate if you read my whole post for the reason instead of making up something I never said, "saving 4 bytes", because it is there.
I would recommend using the least variables possible, because GUI variable editor is lame when full of them.
 
Status
Not open for further replies.
Top