Adding Unit to Unit Group

Zurtrogx

Active Member
Reaction score
24
Problem status: SOLVED

______________________________________________________________


In my map, the structures automatically spawn units like Footmen Frenzy. However, there is a slight difference beacuse in my map there is a population limit. Once this limit is reached, the structure will stop spawning units.

You are able to construct a building type (light, medium, heavy) and then a unit type (melee, ranged, magic) and the building will start producing the corresponding unit.

However, there seems to be a problem:
Only the first building you create will stop spawning when the population limit is reached.
I did a few tests, modifying the trigger, and it seems like only the first loop will add a unit into the unit group. From 2-50, no units are added into the group.

Why is this occuring?

Pay attention to this trigger only, because I feel that is where the problem is
This trigger causes the unit the spawn and adds it into a unit group
Trigger:
  • Unit Spawn
    • Events
      • Time - Every 0.01 seconds of game time
    • Conditions
    • Actions
      • For each (Integer US_check_loop) from 1 to 50, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • US_structure[US_check_loop] Not equal to No unit
            • Then - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • (Number of units in US_current_population[US_check_loop]) Less than US_population_cap[US_check_loop]
                • Then - Actions
                  • Set US_spawn_time_left[US_check_loop] = (US_spawn_time_left[US_check_loop] - 0.01)
                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    • If - Conditions
                      • US_spawn_time_left[US_check_loop] Less than or equal to 0.00
                    • Then - Actions
                      • Set US_temp_point = (Position of US_structure[US_check_loop])
                      • Unit - Create 1 (Unit-type(US_unit_type[US_check_loop])) for (Player(US_owner_player_number[US_check_loop])) at US_temp_point facing Default building facing degrees
                      • Custom script: call RemoveLocation (udg_US_temp_point)
                      • Unit Group - Add (Last created unit) to US_current_population[US_check_loop]
                      • Set US_spawn_time_left[US_check_loop] = US_spawn_time_max[US_check_loop]
                    • Else - Actions
                • Else - Actions
            • Else - Actions




This unit removes the unit from the correct group when it dies
Trigger:
  • Unit Death
    • Events
      • Unit - A unit Dies
    • Conditions
    • Actions
      • For each (Integer US_death_check_loop) from 1 to 50, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • ((Triggering unit) is in US_current_population[US_death_check_loop]) Equal to True
            • Then - Actions
              • Unit Group - Remove (Triggering unit) from US_current_population[US_death_check_loop]
              • Set US_death_check_loop = 51
            • Else - Actions




This trigger determines the unit that will be spawned
Trigger:
  • Unit Type upgrade
    • Events
      • Unit - A unit Begins casting an ability
    • Conditions
      • Or - Any (Conditions) are true
        • Conditions
          • (Ability being cast) Equal to Melee
          • (Ability being cast) Equal to Ranged
          • (Ability being cast) Equal to Magic
    • Actions
      • Unit - Order (Triggering unit) to Stop
      • For each (Integer US_set_loop) from 1 to 50, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • US_structure[US_set_loop] Equal to No unit
            • Then - Actions
              • Set US_structure[US_set_loop] = (Triggering unit)
              • Set US_structure_type = (Substring((String((Unit-type of (Triggering unit)))), 10, 10))
              • Set US_upgrade_selected = (Substring((String((Name of (Ability being cast))) as Lower case), 1, 2))
              • Set US_unit_type[US_set_loop] = ((custom_ + US_race_type[(Player number of (Owner of (Triggering unit)))]) + (US_structure_type + US_upgrade_selected))
              • Set US_owner_player_number[US_set_loop] = (Player number of (Owner of (Triggering unit)))
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • US_structure_type Equal to l
                • Then - Actions
                  • Set US_population_cap[US_set_loop] = 5
                  • Set US_spawn_time_max[US_set_loop] = 15.00
                • Else - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • US_structure_type Equal to m
                • Then - Actions
                  • Set US_population_cap[US_set_loop] = 3
                  • Set US_spawn_time_max[US_set_loop] = 45.00
                • Else - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • US_structure_type Equal to h
                • Then - Actions
                  • Set US_population_cap[US_set_loop] = 2
                  • Set US_spawn_time_max[US_set_loop] = 135.00
                • Else - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • US_race_type[(Player number of (Owner of (Triggering unit)))] Equal to b
                • Then - Actions
                  • Set US_spawn_time_max[US_set_loop] = (US_spawn_time_max[US_set_loop] x 1.50)
                • Else - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • US_race_type[(Player number of (Owner of (Triggering unit)))] Equal to l
                • Then - Actions
                  • Set US_spawn_time_max[US_set_loop] = (US_spawn_time_max[US_set_loop] x 0.50)
                • Else - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • US_race_type[(Player number of (Owner of (Triggering unit)))] Equal to x
                • Then - Actions
                  • Set US_spawn_time_max[US_set_loop] = (US_spawn_time_max[US_set_loop] x 0.90)
                • Else - Actions
              • Set US_spawn_time_left[US_set_loop] = US_spawn_time_max[US_set_loop]
              • Set US_set_loop = 51
            • Else - Actions
      • Unit - Remove Melee from (Triggering unit)
      • Unit - Remove Ranged from (Triggering unit)
      • Unit - Remove Magic from (Triggering unit)
 

DL.PIGGY

New Member
Reaction score
3
I dunno about you. But i seem to have trouble adding units to unit group arrays. That might be a reason. In my map i realise its impossible to add units to unit group arrays. For unit group arrays i would suggest using set [unitgrouparray] = blah blah instead. But thats for me and my map. You could try to confirm this by constantly displaying number of units in the unit group array to see if it really is the problem
 

Zurtrogx

Active Member
Reaction score
24
I think it is the problem.
I just tested it on another map, creating units and randomly adding them into a unit group array, but it seems like they can only be added into the first array.

However, won't using set [unitgrouparray] = unit cause there to only be one unit in the group? Or will it keep adding units?
Edit: I tested it, and it does cause there to be only one unit in the group.

How can I keep adding units into a unit group array, then?

SOLUTION:
Usually, with most variable arrays, you can set the size to 1, and it doesn't affect the array. However, with unit group arrays, you need to set the size to an appropriate size.
 

DL.PIGGY

New Member
Reaction score
3
"However, won't using set [unitgrouparray] = unit cause there to only be one unit in the group? Or will it keep adding units?
Edit: I tested it, and it does cause there to be only one unit in the group.
"

Exactly. Which is where i have issues as well. For me i wanted to make a dummy unit projectile damage units as it passes by them. I had a every X seconds pick all units around it, damage it and add it to unitgroup[customvalueofthedummyunit] clearly it didnt work. Im trying to use unit take damage trigger to do the adding of the picked units to the dummy unit custom value unitgroup. But thats for my case. I would just advise that you do a trial and error, work with whatever u can think up of. Think out of the box =)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top