[Solved] Storing Units Into An Array, Adding and Subtracting

Level 12
Joined
Jul 21, 2015
Messages
278
Hi everyone,

I need help with conceputalizing how this trigger should work. It involves Arrays because that is how I learned from a previous trigger I asked about, First Time Asking About Arrays. Last time Duckfarter was able to help me out. I'm hoping to summon him again haha.

Anyways what I want this trigger to do is,

*if any neutral passive unit is attacked of the type Eagle (wow), then they are turned into a Neutral Hostile
*After that, the second trigger should check for whether the attacked unit is nearby the attacking unit. (I know the main issue with my posted triggers is that I am not storing them correctly. And also am open to complete redesign of the triggers I have thus far)
*if second trigger condition is met, then the attacked unit becomes neutral passive again and goes back to its original location
*I am completely unsure of how to remove the stored unit from the array
*I also am unsure of what to have the Loop C be in regards to the "For each (Integer LoopC) from 1 to 10, do (Actions)", I just chose 1 to 10 because shrug

Thank you!

Also if this is too vague I can add more information. Also here is a picture of my eagle friends :D

Capture.PNG

  • GlobalNeutralPassive
    • Events
      • Unit - A unit Is attacked
    • Conditions
      • (Owner of (Attacked unit)) Equal to Neutral Passive
      • (Unit-type of (Attacked unit)) Equal to Eagle (wow)
    • Actions
      • Unit - Change ownership of (Attacked unit) to Neutral Hostile and Change color
      • Set VariableSet PointEagle2[1] = (Position of (Attacked unit))
      • Set VariableSet UnitAttacked[1] = (Attacked unit)
      • Set VariableSet UnitAttacking[1] = (Attacking unit)
      • Unit - Remove Sleep Always from (Attacked unit)

  • SleepingEagles
    • Events
      • Time - Every 20.00 seconds of game time
    • Conditions
    • Actions
      • For each (Integer LoopC) from 1 to 10, do (Actions)
        • Loop - Actions
          • Set VariableSet RegionEagle = (Region centered at (Position of UnitAttacking[LoopC]) with size (400.00, 400.00))
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (RegionEagle contains UnitAttacking[LoopC]) Equal to False
            • Then - Actions
              • Custom script: call RemoveRect(udg_RegionEagle)
              • Unit - Change ownership of UnitAttacking[LoopC] to Neutral Passive and Retain color
              • Unit - Order UnitAttacking[LoopC] to Move To PointEagle2[LoopC]
              • Wait 5.00 seconds
              • Unit - Add Sleep Always to UnitAttacking[LoopC]
              • Custom script: call RemoveLocation(udg_PointEagle2)
            • Else - Actions
              • Custom script: call RemoveRect(udg_RegionEagle)
 
Last edited:
1) Don't create Regions to enumerate over nearby units, this is inefficient and awkward to use:
  • Set VariableSet RegionEagle = (Region centered at (Position of UnitAttacking[LoopC]) with size (400.00, 400.00))
Instead use Unit Groups + the Pick Every Unit action.

2) Using the "Wait" action is often guaranteed to break your triggers, especially when they have a time related Event. They're really only safe to use when you know for a fact that your trigger is only capable of having a single instance running at a time. They're also imprecise, so Timers/Periodic Intervals are more reliable and will make your map feel more responsive and can even help avoid bugs.

Anyway, for a solution like this I would avoid using For Loops and instead rely on Unit Groups to track the Eagles and their Victims.

I attached a map with an example of this, let me know if it's what you had in mind. How it works is that a "Victim", which is anything that gets attacked by an Eagle, will remain Neutral Hostile until it's 400.00 or more distance away from ALL of the Eagles. Let me know if it's meant to work differently, I actually made another version that uses a slightly separate design which you might prefer.

(Edit: Re-uploaded map, fixed some minor mistakes)
 

Attachments

Last edited:
Wow Uncle you are so quick, thank you!

I appreciate you in highlighting some of the issues I was making with my concept. I tested your map right now and it answers all the of issues I think. You left good notes in the comments so that will make it very easy in putting it into my map.

:goblin_jawdrop:
 
Hi Uncle sorry to double-post (I can delete first post if need be),

but the only issue I seem to be having is I want the eagles to be the attacked unit and I would like the trolls (or every other unit) to be the attacking unit. I tried to switch some triggers such as:

  • Set VariableSet Eagle_Victim = (Attacked unit)
and this condition:
  • (Unit-type of (Attacked unit)) Equal to Eagle_Unit_Type
It seems me changing two things is not enough though.
 
Last edited:
Hi Uncle sorry to double-post (I can delete first post if need be),
but the only issue I seem to be having is I want the eagles to be the attacked unit and I would like the trolls (or every other unit) to be the attacking unit. I tried to switch some triggers such as:
  • Set VariableSet Eagle_Victim = (Attacked unit)
and this condition:
  • (Unit-type of (Attacked unit)) Equal to Eagle_Unit_Type
It seems me changing two things is not enough though.
Okay, so how does it work exactly?

An eagle returns to normal when:
1) It's far enough away from all attackers.
OR
2) It's far enough away from the specific Unit that attacked it.

If #1:
What's considered an "attacker" in this context?
For instance, is it any unit owned by the attacker? Any unit owned by an enemy?

If #2:
What happens when a unit attacks two eagles at once?
What if two different attackers go after the very same eagle? Does that eagle check to see if it's far enough away from both of them?

Oh and I also forgot to implement the "returns to original location" stuff.
 
Last edited:
Okay, so how does it work exactly?

An eagle returns to normal when:
1) It's far enough away from all attackers.
OR
2) It's far enough away from the specific Unit that attacked it.

If #1:
What's considered an "attacker" in this context?
For instance, is it any unit owned by the attacker? Any unit owned by an enemy?

If #2:
What happens when a unit attacks two eagles at once?
What if two different attackers go after the very same eagle? Does that eagle check to see if it's far enough away from both of them?

Oh and I also forgot to implement the "returns to original location" stuff.
Yes so to answer with more information. It should ideally work with an eagle (neutral passive) returns to normal when its far enough away from:

1) It's far enough away from all attackers.

An attacker is considered any units controlled by Player 1, Player 2, Player 3 (for my map that I am creating) that attacked it.



I attempted to reworked your trigger enough to solve for the issue but with no success. I think I messed some things up. Perhaps you could check my alteration. :D

Edit: something in my map is broke after I edited, because sorceress does not affect anything, sorry
 

Attachments

How about this version, I made it so that:

1) The Eagles return after 3.00 seconds of not seeing a nearby threat (an enemy within 400 range).

2) The Eagles always return to their origin, which gets set once per Eagle. This should always be their nest unless something weird happens.

You'll see the number "6" used in a few places as a value to represent the "far enough away from threats" time. Change this value to adjust the time needed, the logic is: (0.50 seconds * X), where X is the value. So a value of 6 = 3.00 seconds and a value of 3 = 1.50 seconds. Note that this is directly related to the Periodic Interval of the Loop trigger which is set to 0.50 seconds, so adjusting the interval will change the effects of this value.

Edit:
Also, this version uses a Unit Indexer. This is a lightweight system used to attach data to Units. It shouldn't interfere, HOWEVER, there's a rule:

You can no longer use this Action yourself:
  • Unit - Set the custom value of (Triggering unit) to 0
But this is fine since the Unit Indexer gives you a better alternative. You can use an Integer Array variable and have as many "custom values" as you'd like.
 

Attachments

Last edited:
Yes, this is exactly perfect. Sorry I was so vague with what I needed. This is perfect! And once again thank you for your time and the reworking of the trigger! And for educating me with how they work as well in the map itself bro.

Here is demonstration of your work Uncle :goblin_yeah:

 
Last edited:
Yes, this is exactly perfect. Sorry I was so vague with what I needed. This is perfect! And once again thank you for your time and the reworking of the trigger! And for educating me with how they work as well in the map itself bro.

Here is demonstration of your work Uncle :goblin_yeah:
Pretty neat, but it looks buggy. It should've fallen back asleep the first time it returned to the nest and on the second time it failed to return because I think the Sleep Always ability forced it to stop. Maybe use something other than that ability. You can modify sleeping behavior through triggers I believe.

Did you copy over the Unit Indexer as well?

Edit: I can't even get non-Neutral Hostile units to Sleep, curious how you did it for Neutral Passive.
 
Last edited:
Did you copy over the Unit Indexer as well?
Yes I did that :)
Pretty neat, but it looks buggy. It should've fallen back asleep the first time it returned to the nest and the second time it failed to return because I think the Sleep Always ability forced it to stop. Maybe use something other than that ability. You can modify sleeping behavior through triggers I believe.
That is probably the best call. I already (from other trigger) a dummy unit that casts sleep. Do you think I should go that route and cast sleep upon Eagle?

Here is the use of it inside another trigger in-game:

  • Then - Actions
    • Unit - Create 1 Dummy Sleeper for Neutral Hostile at (Position of (Hero manipulating item)) facing Default building facing degrees
    • Set VariableSet UnitMushroom4 = (Last created unit)
    • Unit - Order UnitMushroom4 to Undead Dreadlord - Sleep (Hero manipulating item)
    • Wait 2.00 seconds
    • Unit - Remove UnitMushroom4 from the game

And like you said, remove wait times because they = not good
 
Well since you don't actually need the Sleep mechanic (prevent aggro) because the Eagles are owned by Neutral Passive and are already passive, how about just faking it. Here's an example where I use a fake Always Sleep ability for the Art, and enable/disable it for Neutral Passive units depending on whether it's day or night. I do this using an Upgrade dependency + time of day trigger.

The only issue here is that they will begin "sleeping" during their return to the nest, but this is purely visual and it might be a worthwhile compromise to keep things simple. Otherwise, it wouldn't be TOO difficult to delay the adding back of this ability during their return.
 

Attachments

Last edited:
Well since you don't actually need the Sleep mechanic (prevent aggro) because the Eagles are owned by Neutral Passive and are already passive, how about just faking it. Here's an example where I use a fake Always Sleep ability for the Art, and enable/disable it for Neutral Passive units depending on whether it's day or night. I do this using an Upgrade.

The only issue here is that they will begin "sleeping" during their return to the nest, but this is purely visual and it might be a worthwhile compromise to keep things simple. Otherwise, it wouldn't be TOO difficult to delay the adding back of this ability during their return.
Yes I will try that, also could you perhaps re-attach the map it says Oops! We ran into problem
 
Well since you don't actually need the Sleep mechanic (prevent aggro) because the Eagles are owned by Neutral Passive and are already passive, how about just faking it. Here's an example where I use a fake Always Sleep ability for the Art, and enable/disable it for Neutral Passive units depending on whether it's day or night. I do this using an Upgrade dependency.

The only issue here is that they will begin "sleeping" during their return to the nest, but this is purely visual and it might be a worthwhile compromise to keep things simple. Otherwise, it wouldn't be TOO difficult to delay the adding back of this ability during their return.
Alright Uncle I have as a treat for helping me the finished product, including the Quest that involves all the work you put in!

skip to 1:00 if you want to skip the quest prelude, everything after that timestamp showcases what i've added from your help :ogre_haosis:

Once again thank you UNCLE!!!!
 
Back
Top