• 🏆 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!

Question about temp variable

Status
Not open for further replies.
Level 3
Joined
Mar 26, 2019
Messages
54
I have just used World Editor recently. When I look at the forum, I always see that people prefer using temp variable than not. Can I ask the differents between two trigger down here? Which trigger is better?
  • Untitled Trigger 001 Copy
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
      • ((Triggering unit) is A Hero) Equal to True
    • Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Ability being cast) Equal to War Stomp
        • Then - Actions
          • Set temp_point = (Position of (Triggering unit))
          • Set temp_group = (Units within 350.00 of temp_point matching (((Owner of (Matching unit)) is an enemy of (Owner of (Triggering unit))) Equal to True))
          • Custom script: call RemoveLocation(udg_temp_point)
          • Unit Group - Pick every unit in temp_group and do (Actions)
            • Loop - Actions
              • Unit - Cause (Triggering unit) to damage (Picked unit), dealing (Real((Strength of (Triggering unit) (Include bonuses)))) damage of attack type Spells and damage type Normal
          • Custom script: call RemoveGroup(udg_temp_group)
        • Else - Actions
          • Do nothing
  • Untitled Trigger 001
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
      • ((Triggering unit) is A Hero) Equal to True
    • Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Ability being cast) Equal to War Stomp
        • Then - Actions
          • Unit Group - Pick every unit in (Units within 350.00 of (Position of (Triggering unit)) matching (((Owner of (Matching unit)) is an enemy of (Owner of (Triggering unit))) Equal to True)) and do (Actions)
            • Loop - Actions
              • Unit - Cause (Triggering unit) to damage (Picked unit), dealing (Real((Strength of (Triggering unit) (Include bonuses)))) damage of attack type Spells and damage type Normal
          • Unit Group - Remove all units from (Last created unit group)
        • Else - Actions
          • Do nothing
 
Level 9
Joined
Jul 30, 2018
Messages
445
People usually prefer temp variables, so that they don't have to potentially make hundreds of variables for each trigger. Instead, if a variable is used just once for one purpose, it can be recycled. Imagine for example if your War Stomp there made a random damage: you could set TempReal = (random real between X and Y); there's no point to save that number after you've dealt the damage, so you can re-use that same TempReal variable in other triggers.

Also, a little side note: you don't need the "do nothing" action. :)
 
Level 39
Joined
Feb 27, 2007
Messages
4,992
Another benefit is reducing the number of operations in particularly long code or code that runs frequently. If you use “Player number of (Owner of (Triggering Unit))” 9 times in a trigger then you should store that in a temp variable so it only has to be computed once every time the trigger runs instead of 9 times.
 
Status
Not open for further replies.
Top