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

trigger to add ability to a random unit in unit group?

Status
Not open for further replies.

311

311

Level 4
Joined
May 12, 2019
Messages
73
How would I go about adding an ability to a random unit in a unit group that was randomly created?

For example in my map units spawn randomly from a spawn point in groups (between 1 and 10 units at a time)
so when those 10 units spawn, I want an ability to be added possibly to some of those units, like for example 10% chance for any one of those units that spawned to have evasion added, and if the unit did have the ability added, a way to reference it, because I would also want to have other stuff added to that unit
 
Level 39
Joined
Feb 27, 2007
Messages
5,034
Put units in a group (either when spawning them one by one or using “last created unit group” if creating en masse), loop through units in the group and check if a random int from 1 to 10 is <= 1. If yes, give the picked unit abilities as desired and add it to another group that holds all the ability’d units.
 

311

311

Level 4
Joined
May 12, 2019
Messages
73
Put units in a group (either when spawning them one by one or using “last created unit group” if creating en masse), loop through units in the group and check if a random int from 1 to 10 is <= 1. If yes, give the picked unit abilities as desired and add it to another group that holds all the ability’d units.

Which trigger will add the specific picked unit to the new unit group? Just for testing purposes I have

Unit - Create (Random integer number between 1 and 10).level1creeps[(Random integer number between 1 and 6)] for Player 24 (Peanut) at (Position of Portal 0088 <gen>) facing Default building facing degrees
If ((Random integer number between 1 and 2) Equal to 1) then do (Unit - Set Armor of (Picked unit) to 1000.00) else do (Do nothing)


so It creates between 1 and 10 level 1 creeps(which I have a total of 6 level 1 creeps) (This part of the trigger works)
It then takes out of those 10 creeps randomly gives them 1000 armor at a 50% chance (this part works)


but now I want whoever did get the 1000 armor, I want to make sure they ALSO get other abilities, this is the part I am unable to understand, as if I do the same trigger doing random # between 1 and 2 give evasion, it will give evasion at 50% to any of those 10 units, I need it so only whoever got the 1000 armor will get evasion.

If I do add picked unit to a unit group it adds all 10, instead of the specific unit I want
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,206
Which trigger will add the specific picked unit to the new unit group? Just for testing purposes I have
There is a unit group action to do this. One can get the native function name by converting such a trigger to custom script and then looking up the function from the Blizzard and native JASS files. This applies to both JASS and Lua.
at (Position of Portal 0088 <gen>)
Leaks a location. One needs to assign the point to a point variable and then pass it to RemoveLocation after it has been used. Since the point is constant one could set a global variable to it on map initialization and keep reusing that variable, and hence avoiding the need to use custom script.
If ((Random integer number between 1 and 2) Equal to 1) then do (Unit - Set Armor of (Picked unit) to 1000.00) else do (Do nothing)
This is not adding an ability? There is also no picked unit in this context, hence the function will return null and not modify the armor of any unit.

but now I want whoever did get the 1000 armor, I want to make sure they ALSO get other abilities, this is the part I am unable to understand, as if I do the same trigger doing random # between 1 and 2 give evasion, it will give evasion at 50% to any of those 10 units, I need it so only whoever got the 1000 armor will get evasion.
Use the multiple if, then else block action instead of the single line RoC one. This would allow you to run multiple actions when the condition is true instead of just a single one.
If I do add picked unit to a unit group it adds all 10, instead of the specific unit I want
It should apply to no unit as you would need to iterate through all units in the unit group for Picked Unit to have a non-null value.

If you want the chance to be independent for each unit then the pick all units action block will work with a argument of last created unit group. One repeats the roll for each unit as they are picked and mutate the units as desired. If you want a specific number of randomly selected units to always be mutated then use the appropriate unit group function that selects random units. Remember that groups can leak if created and not destroyed after they are no longer useful.
 

311

311

Level 4
Joined
May 12, 2019
Messages
73
There is a unit group action to do this. One can get the native function name by converting such a trigger to custom script and then looking up the function from the Blizzard and native JASS files. This applies to both JASS and Lua.

Leaks a location. One needs to assign the point to a point variable and then pass it to RemoveLocation after it has been used. Since the point is constant one could set a global variable to it on map initialization and keep reusing that variable, and hence avoiding the need to use custom script.

This is not adding an ability? There is also no picked unit in this context, hence the function will return null and not modify the armor of any unit.


Use the multiple if, then else block action instead of the single line RoC one. This would allow you to run multiple actions when the condition is true instead of just a single one.

It should apply to no unit as you would need to iterate through all units in the unit group for Picked Unit to have a non-null value.

If you want the chance to be independent for each unit then the pick all units action block will work with a argument of last created unit group. One repeats the roll for each unit as they are picked and mutate the units as desired. If you want a specific number of randomly selected units to always be mutated then use the appropriate unit group function that selects random units. Remember that groups can leak if created and not destroyed after they are no longer useful.


as far as no picked unit there is a picked unit because I do that in the pick all units function, I just didn't post that part
as far as not being an ability, I only did the armor 1000 for testing purposes, as adding an ability would be the exact same trigger pretty much

Unit - Create (Random integer number between 1 and 10).level1creeps[(Random integer number between 1 and 3)] for Player 24 (Peanut) at (Position of Portal 0088 <gen>) facing Default building facing degrees

Unit Group - Pick every unit in (Last created unit group) and do (Actions)
Loop - Actions
If ((Random integer number between 1 and 3) Equal to 1) then do (Unit - Set Armor of (Picked unit) to 6666.00) else do (Do nothing)
If ((Random integer number between 1 and 3) Equal to 1) then do (Animation - Change (Picked unit)'s size to (500.00%, 500.00%, 500.00%) of its original size) else do (Do nothing)
Unit - Add (Elite) Molten to (Triggering unit)

so for example in the above test trigger some of the units will have 6666 armor, and some will be 500% size, I want it so it would always only apply my ability/size/stat everything to the same unit.



As far as the leak location thing, so that means every region needs to be a variable, in order for it not to leak?, and all I do is set variable to that region and it will be leak free?
As far as a unit group leaking, how would I destroy it?
 

311

311

Level 4
Joined
May 12, 2019
Messages
73
Actually I think I got it, it works testing it, but unsure if I have a lot more units if it might mess up

I just added
If ((Random integer number between 1 and 3) Equal to 1) then do (Set VariableSet addbonusstats= (Picked unit)) else do (Do nothing)

then in the other 2 triggers instead of picked unit, I use the variable above to add whatever I want to it
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,206
so for example in the above test trigger some of the units will have 6666 armor, and some will be 500% size, I want it so it would always only apply my ability/size/stat everything to the same unit.
Which I already answered. Use the block based conditional action and not the reign of chaos single line one. This allows 0 or more actions in either statement unlike the single line version.
As far as the leak location thing, so that means every region needs to be a variable, in order for it not to leak?, and all I do is set variable to that region and it will be leak free?
Every region you get the centre of should be a global "point" (location) variable which is assigned at map initialization. This lets you reuse the same point object and hence not leak. Otherwise one needs to allocate it to a temporary point (location) variable, use it and then pass it to RemoveLocation for clean up.
As far as a unit group leaking, how would I destroy it?
With last created unit group I do not think one has to since it recycles the same group.

Otherwise same as with point (location) variables, use the destructor. In this case DestroyGroup. Player groups like wise is DestroyForce.
Actually I think I got it, it works testing it, but unsure if I have a lot more units if it might mess up
Or use the TFT added (now available to everyone) block based conditional action. This allows one to put more than 1 action under each outcome of the statement.
 
Level 20
Joined
Feb 23, 2014
Messages
1,264
Well, everything was explained above, but there still seems to be some confusion, so... let me try explaining it my way :)

---

1. If you want to post triggers, the easiest way to do it is to right click on the trigger in the editor and select "Copy as Text", then paste that here and put [trigger] before the trigger text and [/trigger] after. The end result will look just like what I've posted below.

2. As for the actual trigger - just do something like this:

  • Unit Group - Pick every unit in <UnitGroup1> and do (Actions)
    • Loop - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Random integer number between 1 and 10) Less than or equal to 1
        • Then - Actions
          • Insert your actions here (i.e. adding an ability or whatever)
          • Unit Group - Add (Picked unit) to <UnitGroup2>
        • Else - Actions
Explanations:
- <UnitGroup1> is your temporary Unit Group variable that you need to set beforehand and clear after the above actions are completed. You will find how to do that here Things That Leak and here Memory Leaks.
- <UnitGroup2> is your Unit Group variable that contains units that got the buff, so that you can do something with them later. This unit group is not a leak.
- If/Then/Else block is called "If / Then / Else, Multiple Functions" in the editor. It allows you to use multiple conditions and actions within the same If/Then/Else block.
- The "Unit Group - Add (Picked unit) to <UnitGroup2> action is made by using an action called "Add Unit" under the "Unit Group" category in the editor.
 
Last edited:
Status
Not open for further replies.
Top