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

Random Unit

Status
Not open for further replies.
Level 7
Joined
May 30, 2018
Messages
290
Dear community,

following question: is it possible to make that every player gets a random unit, from let's hypothetically say a range of 100 units?
So when the game begins everybody gets a random unit and after someone dies the same process should occur so that he gets another random unit out of these 100.

Kind regards,
and thanks in advance.
 
Level 20
Joined
Aug 29, 2012
Messages
832
I think you'd have to store your units into a variable with array, like:

YourUnit[0] = Footman
YourUnit[1] = Grunt
YourUnit[2] = Ghoul

Etc. Then, you create YourUnit[(Random number between 0 and 100)]. It's a bit time consuming to register all units, but I'm not sure there's a way to do it with standard functions.
 
Level 6
Joined
Jun 4, 2017
Messages
172
I think you'd have to store your units into a variable with array, like:

YourUnit[0] = Footman
YourUnit[1] = Grunt
YourUnit[2] = Ghoul
This would work greatly but I think that if you need to do this for 100 units it would be really annoying. You could try to create all these unit types in a region of the map that the player will never see, then you can do a trigger like this:
  • Test
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Set Num = -1
      • Custom script: set bj_wantDestroyGroup = true
      • Unit Group - Pick every unit in (Units in Test Region <gen>) and do (Actions)
        • Loop - Actions
          • Set Num = (Num + 1)
          • Set Unit_Type[Num] = (Unit-type of (Picked unit))
          • Unit - Remove (Picked unit) from the game
Remember to put only one unit for every different type, or else if you put the same unit type more than once there will be more probability for it to spawn.(which might be what you want to do so, idk, do what you want XD)
 
Level 7
Joined
May 30, 2018
Messages
290
This would work greatly but I think that if you need to do this for 100 units it would be really annoying. You could try to create all these unit types in a region of the map that the player will never see, then you can do a trigger like this:
  • Test
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Set Num = -1
      • Custom script: set bj_wantDestroyGroup = true
      • Unit Group - Pick every unit in (Units in Test Region <gen>) and do (Actions)
        • Loop - Actions
          • Set Num = (Num + 1)
          • Set Unit_Type[Num] = (Unit-type of (Picked unit))
          • Unit - Remove (Picked unit) from the game
Remember to put only one unit for every different type, or else if you put the same unit type more than once there will be more probability for it to spawn.(which might be what you want to do so, idk, do what you want XD)

Hey thank you for this suggestion :) I'll give it a try ;) Does this method also include the process of assigning a new unit to a player once he died? (+ Iam currently trying to copy your trigger, but how do I get the "set num = -1" , I cant find that action ...:s)
 
Last edited:
Level 6
Joined
Jun 4, 2017
Messages
172
Hey thank you for this suggestion :) I'll give it a try ;) Does this method also include the process of assigning a new unit to a player once he died?
Yes, you can just create an action like this when you want to assign a new unit to a player:

  • Unit - Create 1 Unit_Type[(Random integer number between 0 and 99)] for Player at Point facing Angle degrees
(+ Iam currently trying to copy your trigger, but how do I get the "set num = -1" , I cant find that action ...:s)
You can find "set Num = -1" in the "Set variable" action.
However, before you do that, you have to create a variable(which i called "Num" because it stands for "number", but you can give it any name you want, it doesn't change anything). To create a variable you have to go to the "X" button in the trigger editor(if you want to know more about variables you can read more in this tutorial).
In my trigger, the variable "Num" is an integer variable.
Other than that there is one more variable: "Unit_Type", which is an array unit type variable.
 
Level 7
Joined
May 30, 2018
Messages
290
Yes, you can just create an action like this when you want to assign a new unit to a player:

  • Unit - Create 1 Unit_Type[(Random integer number between 0 and 99)] for Player at Point facing Angle degrees

You can find "set Num = -1" in the "Set variable" action.
However, before you do that, you have to create a variable(which i called "Num" because it stands for "number", but you can give it any name you want, it doesn't change anything). To create a variable you have to go to the "X" button in the trigger editor(if you want to know more about variables you can read more in this tutorial).
In my trigger, the variable "Num" is an integer variable.
Other than that there is one more variable: "Unit_Type", which is an array unit type variable.

Thank you for the fast reply :) I found the Num= -1 action, but now iam struggling on how you did the num = (Num +1) :s, sorry
 
Level 6
Joined
Jun 4, 2017
Messages
172
Thank you for the fast reply :) I found the Num= -1 action, but now iam struggling on how you did the num = (Num +1) :s, sorry
Don't worry :). To do num = (num + 1) you don't have to try to type it in the "value" category, but instead you have to search for "Arithmetic" in the "functions" category.
Image.PNG
 
Level 7
Joined
May 30, 2018
Messages
290
Don't worry :). To do num = (num + 1) you don't have to try to type it in the "value" category, but instead you have to search for "Arithmetic" in the "functions" category.View attachment 315758

I added everything now, but when I test the map nothing happens :s , i think i did something wrong

upload_2019-2-10_22-13-35.png


upload_2019-2-10_22-14-11.png

here is the unit type array I made
 
Level 6
Joined
Jun 4, 2017
Messages
172
I added everything now, but when I test the map nothing happens :s , i think i did something wrong

View attachment 315760

View attachment 315761
here is the unit type array I made
This trigger will just store all the different unit types that have a chance of spawning(remember that you will have to place all the types of unit spawnable in the region 000).
You will then be able in another trigger to spawn a random unit out of the ones in the region 000. To spawn a unit you can use the action that I wrote earlier:
unit.gif
Unit - Create 1 Unit_Type[(Random integer number between 0 and 99)] for Player at Point facing Angle degrees
 
Level 7
Joined
May 30, 2018
Messages
290
This trigger will just store all the different unit types that have a chance of spawning(remember that you will have to place all the types of unit spawnable in the region 000).
You will then be able in another trigger to spawn a random unit out of the ones in the region 000. To spawn a unit you can use the action that I wrote earlier:

Sorry to bother you once again. Do I have to put this trigger inside of the one above or do I need to make a complete new trigger?
Thank you :v

And Iam to sutpid to figure out how to even make this unit spawn trigger(could you explain it step for step) I just can't find the action under the Unit category ..., I really try to follow your instructions, but my lack of knowledge hinders me...
 
Last edited:
Level 6
Joined
Jun 4, 2017
Messages
172
Hi, sorry for the late answer.
Sorry to bother you once again. Do I have to put this trigger inside of the one above or do I need to make a complete new trigger?
Thank you :v
Don't worry. It depends on what event you want.
And Iam to sutpid to figure out how to even make this unit spawn trigger(could you
explain it step for step)
You aren't stupid, you just need to get used to triggering. As for explaining step by step this unit spawn trigger I can't help you much because I need to know more about what you want to create(do you want to create the units when the map starts or after some time? And when a player unit dies, do you want it to respawn immedialty or after x time? And in a random point or a specific spawn location?).
This trigger, for example, will store the units and create a random unit for 8 players(if you want to change the number of players, change the variable num = 8 to another number) when the map starts around the center of the map.
  • Test
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Set Num = -1
      • Custom script: set bj_wantDestroyGroup = true
      • Unit Group - Pick every unit in (Units in Region 000 <gen>) and do (Actions)
        • Loop - Actions
          • Set Num = (Num + 1)
          • Set Unit_Type[Num] = (Unit-type of (Picked unit))
          • Unit - Remove (Picked unit) from the game
      • Set Num = 8
      • Set Point1 = (Center of (Playable map area))
      • Set Angle = 0.00
      • For each (Integer A) from 1 to Num, do (Actions)
        • Loop - Actions
          • Set Point2 = (Point1 offset by 1000.00 towards Angle degrees)
          • Unit - Create 1 Unit_Type[(Random integer number between 0 and 99)] for (Player((Integer A))) at Point2 facing Point1
          • Custom script: call RemoveLocation(udg_Point2)
          • Set Angle = (Angle + (360.00 / (Real(Num))))
      • Custom script: call RemoveLocation(udg_Point1)
The new variables are: "Point1" and "Point2" that are both point variables and "Angle" that is a real variable.
Remember that I put "Random integer number between 0 and 99" because you have 100 units, but if you have 80, for example, you will have to change that to "Random integer number between 0 and 79".
This trigger is a little bit more complicate, so if you don't know how to write some parts of it, you can always ask here :).
I just can't find the action under the Unit category ..., I really try to follow your instructions, but my lack of knowledge hinders me...
Image.JPG
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,565
This would work greatly but I think that if you need to do this for 100 units it would be really annoying. You could try to create all these unit types in a region of the map that the player will never see, then you can do a trigger like this:
  • Test
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Set Num = -1
      • Custom script: set bj_wantDestroyGroup = true
      • Unit Group - Pick every unit in (Units in Test Region <gen>) and do (Actions)
        • Loop - Actions
          • Set Num = (Num + 1)
          • Set Unit_Type[Num] = (Unit-type of (Picked unit))
          • Unit - Remove (Picked unit) from the game
Remember to put only one unit for every different type, or else if you put the same unit type more than once there will be more probability for it to spawn.(which might be what you want to do so, idk, do what you want XD)

I like this method and I've used it before many times. I was making a card game that dealt players random cards (units) from a region like in the trigger above. This was great because I could store the player's deck (basically a bunch of units that represented cards) to a region specific to the player. Then I could pull random units (cards) from this region, so for example when the player drew a card I would remove a random unit from the "deck region" so it couldn't be drawn again and then I'd add an ability to your Hero equivalent to the card that you drew. So if it removed a "Pikachu" from the region, then it would add the "Summon Pikachu" ability to your hero.

Maybe not the most efficient or pretty method, but it certainly got the job done and opened up a lot of possibilities. For example, I stored all of the cards information in a hashtable, so I could save/load information directly from the unit. I could also index the units in the "deck region" so that the deck was actually shuffled properly and you could say "Draw the tenth card from the top of your deck". That way it wasn't even random and worked like a proper deck similar to games like Magic the Gathering. It also allowed you to do neat things like say you played a card that said "units in your deck with power 1 or less permanently gain +1 power", you could then pick every unit from your deck that meets the requirement and update their stats using the hashtable/index method.

@Meshcash This method can be a bit confusing since you're still learning the basics but I can send you an example of my map which might help. Seeing other people's work and tweaking it is the best way to learn a lot of times, at least in my opinion.
 
Last edited:
Level 7
Joined
May 30, 2018
Messages
290
Hi, sorry for the late answer.

Don't worry. It depends on what event you want.

You aren't stupid, you just need to get used to triggering. As for explaining step by step this unit spawn trigger I can't help you much because I need to know more about what you want to create(do you want to create the units when the map starts or after some time? And when a player unit dies, do you want it to respawn immedialty or after x time? And in a random point or a specific spawn location?).
This trigger, for example, will store the units and create a random unit for 8 players(if you want to change the number of players, change the variable num = 8 to another number) when the map starts around the center of the map.
  • Test
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Set Num = -1
      • Custom script: set bj_wantDestroyGroup = true
      • Unit Group - Pick every unit in (Units in Region 000 <gen>) and do (Actions)
        • Loop - Actions
          • Set Num = (Num + 1)
          • Set Unit_Type[Num] = (Unit-type of (Picked unit))
          • Unit - Remove (Picked unit) from the game
      • Set Num = 8
      • Set Point1 = (Center of (Playable map area))
      • Set Angle = 0.00
      • For each (Integer A) from 1 to Num, do (Actions)
        • Loop - Actions
          • Set Point2 = (Point1 offset by 1000.00 towards Angle degrees)
          • Unit - Create 1 Unit_Type[(Random integer number between 0 and 99)] for (Player((Integer A))) at Point2 facing Point1
          • Custom script: call RemoveLocation(udg_Point2)
          • Set Angle = (Angle + (360.00 / (Real(Num))))
      • Custom script: call RemoveLocation(udg_Point1)
The new variables are: "Point1" and "Point2" that are both point variables and "Angle" that is a real variable.
Remember that I put "Random integer number between 0 and 99" because you have 100 units, but if you have 80, for example, you will have to change that to "Random integer number between 0 and 79".
This trigger is a little bit more complicate, so if you don't know how to write some parts of it, you can always ask here :).

View attachment 316033

Thanks for the kind words :)


You aren't stupid, you just need to get used to triggering. As for explaining step by step this unit spawn trigger I can't help you much because I need to know more about what you want to create(do you want to create the units when the map starts or after some time? And when a player unit dies, do you want it to respawn immedialty or after x time? And in a random point or a specific spawn location?).


to specify: yes units should be created when the game starts, when a player unit dies there should be a respawn after a set time as another random hero from the pool at a random spawn location.
These are exactly the settings I need for my map.

Thanks for your patience with me and the supportive replies. I really appreciate that!
 
Level 6
Joined
Jun 4, 2017
Messages
172
Thanks for the kind words :)





to specify: yes units should be created when the game starts, when a player unit dies there should be a respawn after a set time as another random hero from the pool at a random spawn location.
These are exactly the settings I need for my map.

Thanks for your patience with me and the supportive replies. I really appreciate that!
No problem. I'll soon create a trigger that does that and I will also explain everything, however do you want fixed or random spawn location for when units are created on map start? And are there any other units other than a unit for player?
 
Level 7
Joined
May 30, 2018
Messages
290
No problem. I'll soon create a trigger that does that and I will also explain everything, however do you want fixed or random spawn location for when units are created on map start? And are there any other units other than a unit for player?

You are my hero c:

I'd like to have random spawn locations, and no there aren't any other units :)

Thanks for your awesome support. You allow my wish to create an own map to become true :D.
 
Level 6
Joined
Jun 4, 2017
Messages
172
You are my hero c:

I'd like to have random spawn locations, and no there aren't any other units :)

Thanks for your awesome support. You allow my wish to create an own map to become true :D.
I'm happy that I have been helpful :)
This turned out to be a little more complicated than I thought. There are 7 triggers in total. I upload a map so you can easily copy and paste the triggers, if you want to change some parameters there is a trigger called "Customization" where you can customize some rules of how the other triggers work.
Remember to check the option in the world editor in file -> preferences -> "Create automatically all unknown variables when a trigger is pasted" or something like that, before pasting the triggers to your map so you won't have to create all the variables too.
 

Attachments

  • random unit.w3x
    49.8 KB · Views: 37
Level 7
Joined
May 30, 2018
Messages
290
I'm happy that I have been helpful :)
This turned out to be a little more complicated than I thought. There are 7 triggers in total. I upload a map so you can easily copy and paste the triggers, if you want to change some parameters there is a trigger called "Customization" where you can customize some rules of how the other triggers work.
Remember to check the option in the world editor in file -> preferences -> "Create automatically all unknown variables when a trigger is pasted" or something like that, before pasting the triggers to your map so you won't have to create all the variables too.

I copied everything now and it works perfectly fine :3 I can't tell you how thankful I am. Really you helped me out to such an immense degree...its unbelievable. I wish I could do something in return to show you how thankful I am.

Thank you, thank you, thank you soooo much. If I can do anything for you just tell me :)

Have a nice rest of the week! ^^
 
Status
Not open for further replies.
Top