• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Unit Group Variable Help

Level 2
Joined
Nov 27, 2017
Messages
7
I had some issues when using variables, specifically, Unit Group. I tried to use this as this prevent memory leak but it does not go out as I expected. When using the "random point in region", the creeps created stays in one place, I want them to be scattered randomly and not stacked in a random point in the region

  • Spawn Level 1
    • Events
      • Time - BalanceTimer1 expires
    • Conditions
    • Actions
      • Set PointLeak2 = (Random point in Spawn 1 <gen>)
      • Unit - Create 1 Spider for Neutral Hostile at PointLeak2 facing (Center of (Entire map))
      • Unit - Create 1 Murloc Tiderunner for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
      • Unit - Create 1 Satyr for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
      • Unit - Create 1 Satyr Trickster for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
      • Unit - Create 1 Harpy Scout for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
      • Unit - Create 1 Quillboar for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
      • Unit - Create 1 Razormane Scout for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
      • Unit - Create 1 Draenei Guardian for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
      • Unit - Create 1 Lesser Voidwalker for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
      • Unit - Create 1 Bandit for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
      • Unit - Create 1 Fallen Priest for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
      • Custom script: call RemoveLocation(udg_PointLeak2)

Instead of spreading around the region like it was without variable, they are stacked in one place instead, moreover, they tend to spawn again on the same area or in another area but are still stacked (timer).

  • Spawn Level 2
    • Events
      • Time - BalanceTimer2 expires
    • Conditions
    • Actions
      • For each (Integer A) from 1 to 100, do (Actions)
        • Loop - Actions
          • Set PointLeak2 = (Random point in 3 Weaker Creep Spawn <gen>)
          • Unit - Create 1 Dark Troll for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
          • Unit - Create 1 Lightning Lizard for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
          • Unit - Create 1 Dark Troll Shadow Priest for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
          • Unit - Create 1 Murloc Huntsman for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
          • Unit - Create 1 Brigand for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
          • Unit - Create 1 Mud Golem for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
          • Unit - Create 1 Draenei Disciple for Neutral Hostile at PointLeak2 facing (Random point in (Playable map area))
          • Custom script: call RemoveLocation(udg_PointLeak2)
I also use the integer actions as well, and the random points worked (scattered around), but instead it causes to multiply the number of units instead of the intended 1 spawn only. This could be troublesome as it has a repeating timer. there are also cases they are stacked with the same number of spawned units, just multiplied.
Please help :< (I'm still a beginner and I only realized memory leak today)
 
Level 21
Joined
Feb 27, 2019
Messages
623
Setting a point creates a single point. In these triggers it gets set to a single random point in a region and then all units are created at that point. What needs to be done is a new point be set before each and every units creation. Also "facing (Random point in (Playable map area)) too creates a point but I would have used the function Create Unit Facing Angle (Math - Random angle) instead.

What could be done if there are a lot of repeating actions is to initiate some variables to use. It can make it easier to handle, make changes and can require less code. Heres are two simple triggers to show the concept.

CreepsLevelTwo = Unit-Type variable array
  • Spawn Level 2 Init
    • Events
      • Time - Elapsed game time is 0.50 seconds
    • Conditions
    • Actions
      • Set VariableSet CreepsLevelTwo[1] = Dark Troll
      • Set VariableSet CreepsLevelTwo[2] = Lightning Lizard
      • Set VariableSet CreepsLevelTwo[3] = Dark Troll Shadow Priest
      • Set VariableSet CreepsLevelTwoMax = 3
  • Spawn Level 2
    • Events
      • Time - BalanceTimer2 expires
    • Conditions
    • Actions
      • For each (Integer LoopA) from 1 to CreepsLevelTwoMax, do (Actions)
        • Loop - Actions
          • Set VariableSet PointLeak2 = (Random point in (Playable map area))
          • Unit - Create 1 CreepsLevelTwo[LoopA] for Neutral Hostile at PointLeak2 facing (Random angle) degrees
          • Custom script: call RemoveLocation(udg_PointLeak2)
 
Level 2
Joined
Nov 27, 2017
Messages
7
Spawn Level 2
Events
Time - BalanceTimer2 expires
Conditions
Actions
For each (Integer LoopA) from 1 to CreepsLevelTwoMax, do (Actions)
Loop - Actions
Set VariableSet PointLeak2 = (Random point in (Playable map area))
Unit - Create 1 CreepsLevelTwo[LoopA] for Neutral Hostile at PointLeak2 facing (Random angle) degrees
Custom script: call RemoveLocation(udg_PointLeak2)
[/TRIGGER]
I want to clarify on the Integer loop part.
If the timer was to set again, will it choose a new point in the map or will it keep the same point recorded on the first timer? I want the spawning to be purely random, and I tend to assume that it will spawn on the same area where the unit once spawned.
 
Level 21
Joined
Feb 27, 2019
Messages
623
I want to clarify on the Integer loop part.
If the timer was to set again, will it choose a new point in the map or will it keep the same point recorded on the first timer? I want the spawning to be purely random, and I tend to assume that it will spawn on the same area where the unit once spawned.
It will choose a new point. Actions are executed when the trigger runs so having a random action will guarantee that its random every time. Events on the other hand are set in stone when they are created.

What is happening in that Integer loop is basically this:
  • Spawn Level 2
    • Events
      • Time - BalanceTimer2 expires
    • Conditions
    • Actions
      • Set VariableSet PointLeak2 = (Random point in (Playable map area))
      • Unit - Create 1 CreepsLevelTwo[1] for Neutral Hostile at PointLeak2 facing (Random angle) degrees
      • Custom script: call RemoveLocation(udg_PointLeak2)
      • Set VariableSet PointLeak2 = (Random point in (Playable map area))
      • Unit - Create 1 CreepsLevelTwo[2] for Neutral Hostile at PointLeak2 facing (Random angle) degrees
      • Custom script: call RemoveLocation(udg_PointLeak2)
      • Set VariableSet PointLeak2 = (Random point in (Playable map area))
      • Unit - Create 1 CreepsLevelTwo[3] for Neutral Hostile at PointLeak2 facing (Random angle) degrees
      • Custom script: call RemoveLocation(udg_PointLeak2)
Here is the loop in jass. A loop repeats itself until a number is higher than another number, increasing a number each time it repeats.
JASS:
    set udg_LoopA = 1
    loop
        exitwhen udg_LoopA > udg_CreepsLevelTwoMax
        set udg_PointLeak2 = GetRandomLocInRect(GetPlayableMapRect())
        call CreateNUnitsAtLoc( 1, udg_CreepsLevelTwo[udg_LoopA], Player(PLAYER_NEUTRAL_AGGRESSIVE), udg_PointLeak2, GetRandomDirectionDeg() )
        call RemoveLocation(udg_PointLeak2)
        set udg_LoopA = udg_LoopA + 1
    endloop
udg_LoopA will start at 1 before the loop and is increased by 1 at the end of each repetition. At the start of each repition it checks if udg_LoopA is higher than udg_CreepsLevelTwoMax, if true the loop ends which in this case occurs when udg_LoopA is greater than 3. The variable udg_LoopA is used in the udg_CreepsLevelTwo array to spawn the Unit-Type at the array index of udg_LoopA.
 
Last edited:
Top