1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. We have recently started the 16th edition of the Mini Mapping Contest. The theme is mini RPG. Do check it out and have fun.
    Dismiss Notice
  4. Choose your ride to damnation in the 5th Special Effect Contest Poll.
    Dismiss Notice
  5. The winners of the 13th Techtree Contest have been announced!
    Dismiss Notice
  6. Vote for the best entries in the 13th Music Contest Poll.
    Dismiss Notice
  7. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

Using Damage as universal variable

Discussion in 'World Editor Help Zone' started by Recklessness, Nov 5, 2019.

Tags:
  1. Recklessness

    Recklessness

    Joined:
    Aug 17, 2014
    Messages:
    81
    Resources:
    0
    Resources:
    0
    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.
     
  2. Uncle

    Uncle

    Joined:
    Aug 10, 2018
    Messages:
    763
    Resources:
    0
    Resources:
    0
    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.
    Code (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: Nov 5, 2019
  3. Ceday

    Ceday

    Joined:
    Feb 22, 2010
    Messages:
    1,111
    Resources:
    0
    Resources:
    0
    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.
     
  4. Pyrogasm

    Pyrogasm

    Joined:
    Feb 27, 2007
    Messages:
    3,678
    Resources:
    1
    Spells:
    1
    Resources:
    1
  5. Wrda

    Wrda

    Joined:
    Nov 18, 2012
    Messages:
    1,231
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    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.
    Honestly, that would be stupid. We all just need to be conscious about we're doing.
     
    Last edited: Nov 5, 2019
  6. Ceday

    Ceday

    Joined:
    Feb 22, 2010
    Messages:
    1,111
    Resources:
    0
    Resources:
    0
    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.
     
  7. Recklessness

    Recklessness

    Joined:
    Aug 17, 2014
    Messages:
    81
    Resources:
    0
    Resources:
    0
    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.
     
  8. Wrda

    Wrda

    Joined:
    Nov 18, 2012
    Messages:
    1,231
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    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.