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

[Solved] Unit groups and malfunctioning loops

Status
Not open for further replies.
Level 6
Joined
May 15, 2009
Messages
191
Hello everyone, I am in need of triggering help again. (Im starting to feel like I should pay back the Hive somehow?)

This time around I have tried to do a "Needle Trap" when a unit steps upon a placed trap, it will eject waves of knives. However, the trap won't seem to start ejecting knives, when you come close to it.

Here are my triggers, a bit messy looking sorry.

  • Needle Trap
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
      • (Ability being cast) Equal to Needle Trap
    • Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • Or - Any (Conditions) are true
            • Conditions
              • (Level of Needle Trap for (Triggering unit)) Equal to 1
              • (Level of Needle Trap for (Triggering unit)) Equal to 2
        • Then - Actions
          • Set NeedleTrap_Loop = 1
        • Else - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • Or - Any (Conditions) are true
                • Conditions
                  • (Level of Needle Trap for (Triggering unit)) Equal to 3
                  • (Level of Needle Trap for (Triggering unit)) Equal to 4
            • Then - Actions
              • Set NeedleTrap_Loop = 2
            • Else - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • Or - Any (Conditions) are true
                    • Conditions
                      • (Level of Needle Trap for (Triggering unit)) Equal to 5
                • Then - Actions
                  • Set NeedleTrap_Loop = 3
                • Else - Actions
      • Set NeedleTrap_Caster = (Triggering unit)
      • Set NeedleTrap_CasterLoc = (Position of NeedleTrap_Caster)
      • Set NeedleTrap_CasterOwner = (Owner of NeedleTrap_Caster)
      • Unit - Create 1 Needle Trap for NeedleTrap_CasterOwner at NeedleTrap_CasterLoc facing Default building facing degrees
      • Unit - Add a 30.00 second Healing Ward expiration timer to (Last created unit)
      • Unit Group - Add (Last created unit) to NeedleTrap_Group[1]
      • Custom script: call RemoveLocation(udg_NeedleTrap_CasterLoc)
And for checking whenever units get too close, and to start knifing.

  • Needle Trap Loop
    • Events
      • Time - Every 1.00 seconds of game time
    • Conditions
    • Actions
      • Unit Group - Pick every unit in NeedleTrap_Group[1] and do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • ((Matching unit) is alive) Equal to True
            • Then - Actions
              • Set NeedleTrap_Picked = (Picked unit)
              • Set NeedleTrap_PickedLoc = (Position of NeedleTrap_Picked)
              • Unit Group - Pick every unit in (Units within 375.00 of NeedleTrap_PickedLoc matching ((((Matching unit) is A structure) Not equal to True) and (((Matching unit) belongs to an enemy of NeedleTrap_CasterOwner) Equal to True))) and do (Actions)
                • Loop - Actions
                  • For each (Integer NeedleTrap_Loop) from 1 to NeedleTrap_Loop, do (Actions)
                    • Loop - Actions
                      • Unit Group - Add (Picked unit) to NeedleTrap_Group[2]
                      • Unit - Create 1 Dummy for NeedleTrap_CasterOwner at NeedleTrap_PickedLoc facing Default building facing degrees
                      • Unit - Add a 2.00 second Generic expiration timer to (Last created unit)
                      • Unit - Add Needle Spray to (Last created unit)
                      • Unit - Set level of Needle Spray for (Last created unit) to (Level of Needle Trap for NeedleTrap_Caster)
                      • Unit - Order (Last created unit) to Night Elf Warden - Fan Of Knives
                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    • If - Conditions
                      • (Number of units in NeedleTrap_Group[2]) Greater than 0
                    • Then - Actions
                      • Set NeedleTrap_SeconPickedLoc = (Position of (Picked unit))
                      • Unit - Create 1 Dummy for NeedleTrap_CasterOwner at NeedleTrap_SeconPickedLoc facing Default building facing degrees
                      • Unit - Add a 2.00 second Generic expiration timer to (Last created unit)
                      • Unit - Add Needle Poison to (Last created unit)
                      • Unit - Order (Last created unit) to Neutral Naga Sea Witch - Frost Arrows (Picked unit)
                      • Unit - Remove NeedleTrap_Picked from the game
                    • Else - Actions
            • Else - Actions
      • Custom script: call RemoveLocation(udg_NeedleTrap_PickedLoc)
      • Custom script: call RemoveLocation(udg_NeedleTrap_SeconPickedLoc)
      • Custom script: call DestroyGroup(udg_NeedleTrap_Group[2])
Help much appreciated
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
I think the problem is in your second trigger where it says "Matching unit is alive equal to true". It should say "Picked unit is alive equal to true". Also, it looks like your second group (Unit Group - Pick every unit in (Units within 375.00 of NeedleTrap_PickedLoc...) leaks. Put this custom script just before it to prevent leaking:

  • Custom script: set bj_wantDestroyGroup = true
 

Bannar

Code Reviewer
Level 26
Joined
Mar 19, 2008
Messages
3,140
You do not need 3 point variables, two are enough.
Player variable isn't required either, stick with GetOwningPlayer() if you are saving (Triggering unit) anyways.
It's not the first time you are skipping 'formula' part - think for a while to improve efficiency of maths (jass math is horrible enough).

Second group isn't needed actually, furthermore you haven't declared both group nor used properly/removed leaks. Array variable is completely waste of memory here. Array groups have to be declared by:
  • Custom script: set udg_NeedleTrap_Group[1] = CreateGroup()
Don't prolong variable names, they are pain to look at - stick with simple names.
Mr_Bean already mentioned that you have used (Matching unit) instead of (Picked unit) - comparison will always return null.

I can't belive this ^^:
  • For each (Integer NeedleTrap_Loop) from 1 to NeedleTrap_Loop, do (Actions)
It's completely loop-kill statement, function will refer only to one instance. I made use of integer 'i' variable.

EDIT: Add 'trigger - turn off' posibility to prevent trigger from running if there are no available instances.
  • Needle Trap
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
      • (Ability being cast) Equal to Needle Trap
    • Actions
      • Set NeedleTrap_Loop = ((((Level of Needle Trap for (Triggering unit)) - 1) / 2) + 1)
      • Set NeedleTrap_Caster = (Triggering unit)
      • Set NT_tempp = (Position of NeedleTrap_Caster)
      • Unit - Create 1 Needle Trap for (Triggering player) at NT_tempp facing Default building facing degrees
      • Unit - Add a 30.00 second Healing Ward expiration timer to (Last created unit)
      • Unit Group - Add (Last created unit) to NeedleTrap_Group
      • Custom script: call RemoveLocation(udg_NT_tempp)
  • Needle Trap Loop
    • Events
      • Time - Every 1.00 seconds of game time
    • Conditions
    • Actions
      • Unit Group - Pick every unit in NeedleTrap_Group and do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • ((Picked unit) is alive) Equal to True
            • Then - Actions
              • Set NT_unit = (Picked unit)
              • Set NT_tempp = (Position of NT_unit)
              • Custom script: set bj_wantDestroyGroup = true
              • Unit Group - Pick every unit in (Units within 375.00 of NT_tempp matching ((((Matching unit) is A structure) Not equal to True) and (((Matching unit) belongs to an enemy of (Owner of NeedleTrap_Caster)) Equal to True))) and do (Actions)
                • Loop - Actions
                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    • If - Conditions
                      • (Picked unit) Not equal to No unit
                    • Then - Actions
                      • For each (Integer i) from 1 to NeedleTrap_Loop, do (Actions)
                        • Loop - Actions
                          • Unit - Create 1 Dummy for (Owner of NeedleTrap_Caster) at NT_tempp facing Default building facing degrees
                          • Unit - Add a 2.00 second Generic expiration timer to (Last created unit)
                          • Unit - Add Needle Spray to (Last created unit)
                          • Unit - Set level of Needle Spray for (Last created unit) to (Level of Needle Trap for NeedleTrap_Caster)
                          • Unit - Order (Last created unit) to Night Elf Warden - Fan Of Knives
                      • Set NT_tempp2 = (Position of (Picked unit))
                      • Unit - Create 1 Dummy for (Owner of NeedleTrap_Caster) at NT_tempp2 facing Default building facing degrees
                      • Unit - Add a 2.00 second Generic expiration timer to (Last created unit)
                      • Unit - Add Needle Poison to (Last created unit)
                      • Unit - Order (Last created unit) to Neutral Naga Sea Witch - Frost Arrows (Picked unit)
                      • Unit - Remove NT_unit from the game
                      • Custom script: call RemoveLocation(udg_NT_tempp2)
                    • Else - Actions
              • Custom script: call RemoveLocation(udg_NT_tempp)
            • Else - Actions
              • Unit - Remove (Picked unit) from NeedleTrap_Group
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • (NeedleTrap_Group is Empty) Equal to True
                • Then - Actions
                  • Trigger - Turn off (This trigger)
                • Else - Actions
 
Last edited:
Level 6
Joined
May 15, 2009
Messages
191
Thx for the help guys, sadly I can't rep you two again(did +rep for u Bean).

Im pretty sure I failed somehow with the For each integer do... Im quite bad at using those.
Anyway, the trap now activates, but it will also make the map freeze due to some sort of overload. Im pretty sure the problem is in the loop, since thats the only thing which can really crate alot of effects.

Here is my triggers after attempting to fix:

  • Needle Trap
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
      • (Ability being cast) Equal to Needle Trap
    • Actions
      • Set NeedleTrap_Loop = (1 + (((Level of Needle Trap for (Triggering unit)) - 1) / 2))
      • Set NeedleTrap_Caster = (Triggering unit)
      • Set NeedleTrap_TempLoc = (Position of NeedleTrap_Caster)
      • Unit - Create 1 Needle Trap for (Owner of NeedleTrap_Caster) at NeedleTrap_TempLoc facing Default building facing degrees
      • Unit - Add a 30.00 second Healing Ward expiration timer to (Last created unit)
      • Unit Group - Add (Last created unit) to NeedleTrap_Group
      • Trigger - Turn on Needle Trap Loop <gen>
      • Custom script: call RemoveLocation(udg_NeedleTrap_TempLoc)
  • Needle Trap Loop
    • Events
      • Time - Every 1.00 seconds of game time
    • Conditions
    • Actions
      • Unit Group - Pick every unit in NeedleTrap_Group and do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • ((Picked unit) is alive) Equal to True
            • Then - Actions
              • Set NeedleTrap_Picked = (Picked unit)
              • Set NeedleTrap_TempLoc = (Position of NeedleTrap_Picked)
              • Custom script: set bj_wantDestroyGroup = true
              • Unit Group - Pick every unit in (Units within 375.00 of NeedleTrap_TempLoc matching ((((Matching unit) is A structure) Not equal to True) and (((Matching unit) belongs to an enemy of (Owner of NeedleTrap_Caster)) Equal to True))) and do (Actions)
                • Loop - Actions
                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    • If - Conditions
                      • NeedleTrap_Picked Not equal to No unit
                    • Then - Actions
                      • For each (Integer NeedleTrap_Loop) from 1 to NeedleTrap_Loop, do (Actions)
                        • Loop - Actions
                          • Unit - Create 1 Dummy for (Owner of NeedleTrap_Caster) at NeedleTrap_TempLoc facing Default building facing degrees
                          • Unit - Add a 2.00 second Generic expiration timer to (Last created unit)
                          • Unit - Add Needle Spray to (Last created unit)
                          • Unit - Set level of Needle Spray for (Last created unit) to (Level of Needle Trap for NeedleTrap_Caster)
                          • Unit - Order (Last created unit) to Night Elf Warden - Fan Of Knives
                      • Set NeedleTrap_TempLoc2 = (Position of (Picked unit))
                      • Unit - Create 1 Dummy for (Owner of NeedleTrap_Caster) at NeedleTrap_TempLoc2 facing Default building facing degrees
                      • Unit - Add a 2.00 second Generic expiration timer to (Last created unit)
                      • Unit - Add Needle Poison to (Last created unit)
                      • Unit - Order (Last created unit) to Neutral Naga Sea Witch - Frost Arrows (Picked unit)
                      • Unit - Remove NeedleTrap_Picked from the game
                      • Custom script: call RemoveLocation(udg_NeedleTrap_TempLoc2)
                    • Else - Actions
            • Else - Actions
              • Unit - Remove NeedleTrap_Picked from the game
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • (Number of units in NeedleTrap_Group) Equal to 0
                • Then - Actions
                  • Trigger - Turn off (This trigger)
                • Else - Actions
Im so confused:goblin_cry:
 
Level 6
Joined
May 15, 2009
Messages
191
Thx a thousand times (I actually mean that) to both of you. I just used For Every Integer A... But I heard it has some bug chances, so I might replace it with something else.

Anyroad, my spell works brilliantly and you both deserve some rep I can't quite give yet. You have helped me too much! :(??

Anyway, thx.
 
Last edited:
Status
Not open for further replies.
Top