• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

How To Make Random Race Selection System?

Status
Not open for further replies.
Level 3
Joined
Sep 15, 2021
Messages
31
Greetings,

I want to give players more options to enjoy my map and have been trying to make a random race selection system
but I haven't figure it out.

As the photo attached below, players can choose any race and i want to put Random race as well.
It doesn't matter that there's no more slots in the selection building, i can make another one

Plz give me some ideas

random race.png
 
Level 28
Joined
Sep 26, 2009
Messages
2,515
I assume clicking an icon in the shop creates worker of that race?
If so, create another type of unit (even better: make it a dummy unit - so it is invisible, etc.). This new unit type will represent your "random" race and act as placeholder.
Add this placeholder-worker to the shop.

Now a bit of triggerring:
  1. Create a Unit-type array
  2. Create Map Initialization trigger and fill the array with worker unit-type of each of your race, e.g.:
    • workers[1] = Blood Elf Worker
    • workers[2] = Murloc Worker
    • ... etc.
  3. Create a trigger with "Unit enters Playable map area" event
  4. Check if unit-type of triggering unit is equal to the placeholder-worker type
  5. If yes, pick a random number between 1 and max index in the unit-type array (in my example it is workers[] array)
    • e.g. set random_race_index = Random number between 1 and 9
  6. Use that random number as index for the unit-type array
  7. Replace the triggering unit with a new unit of type workers[random_race_index]

In practice, when a player "buys" the placeholder worker, that worker will appear in game, triggering the "Unit enters Playable map area" event. Since the condition is met (unit-type of triggering unit is equal to the placeholder-worker type) that placeholder-worker will be replaced by a random unit-type from workers[] array
 
Level 3
Joined
Sep 15, 2021
Messages
31
I assume clicking an icon in the shop creates worker of that race?
If so, create another type of unit (even better: make it a dummy unit - so it is invisible, etc.). This new unit type will represent your "random" race and act as placeholder.
Add this placeholder-worker to the shop.

Now a bit of triggerring:
  1. Create a Unit-type array
  2. Create Map Initialization trigger and fill the array with worker unit-type of each of your race, e.g.:
    • workers[1] = Blood Elf Worker
    • workers[2] = Murloc Worker
    • ... etc.
  3. Create a trigger with "Unit enters Playable map area" event
  4. Check if unit-type of triggering unit is equal to the placeholder-worker type
  5. If yes, pick a random number between 1 and max index in the unit-type array (in my example it is workers[] array)
    • e.g. set random_race_index = Random number between 1 and 9
  6. Use that random number as index for the unit-type array
  7. Replace the triggering unit with a new unit of type workers[random_race_index]

In practice, when a player "buys" the placeholder worker, that worker will appear in game, triggering the "Unit enters Playable map area" event. Since the condition is met (unit-type of triggering unit is equal to the placeholder-worker type) that placeholder-worker will be replaced by a random unit-type from workers[] array

I've done till the step 4 but from step 5 i don't get what it means and what it would look like in the trigger editor..
Could u plz show me with using trigger code?
 
Level 3
Joined
Sep 15, 2021
Messages
31
I assume clicking an icon in the shop creates worker of that race?
If so, create another type of unit (even better: make it a dummy unit - so it is invisible, etc.). This new unit type will represent your "random" race and act as placeholder.
Add this placeholder-worker to the shop.

Now a bit of triggerring:
  1. Create a Unit-type array
  2. Create Map Initialization trigger and fill the array with worker unit-type of each of your race, e.g.:
    • workers[1] = Blood Elf Worker
    • workers[2] = Murloc Worker
    • ... etc.
  3. Create a trigger with "Unit enters Playable map area" event
  4. Check if unit-type of triggering unit is equal to the placeholder-worker type
  5. If yes, pick a random number between 1 and max index in the unit-type array (in my example it is workers[] array)
    • e.g. set random_race_index = Random number between 1 and 9
  6. Use that random number as index for the unit-type array
  7. Replace the triggering unit with a new unit of type workers[random_race_index]

In practice, when a player "buys" the placeholder worker, that worker will appear in game, triggering the "Unit enters Playable map area" event. Since the condition is met (unit-type of triggering unit is equal to the placeholder-worker type) that placeholder-worker will be replaced by a random unit-type from workers[] array
To be speicific I've done all the steps excpet for step 5 and 6

JASS:
Unit Type Initial
    Events
        Map initialization
    Conditions
    Actions
        Set VariableSet Workers[1] = Human
        Set VariableSet Workers[2] = Orc
        Set VariableSet Workers[3] = Elf
        Set VariableSet Workers[4] = Undead
        Set VariableSet Workers[5] = Devil
        Set VariableSet Workers[6] = CurruptElf
        Set VariableSet Workers[7] = HighElf
        Set VariableSet Workers[8] = Spiders
        Set VariableSet Workers[9] = Merlock
        Set VariableSet Workers[10] = BloodElf
        Set VariableSet Workers[11] = Dwarf

JASS:
Unit Type Initial 2
    Events
        Unit - A unit enters (Playable map area)
    Conditions
        (Unit-type of (Triggering unit)) Equal to Randon Race Unit
    Actions
        
        Unit - Replace (Triggering unit) with a Workers[11] using The old unit's relative life and mana
 

Uncle

Warcraft Moderator
Level 70
Joined
Aug 10, 2018
Messages
7,375
So you're referencing Workers[11] in your trigger which is the Dwarf.

You need to reference a random Workers[] from 1 to 11:
  • Unit - Replace (Triggering unit) with a Workers[Random integer number between 1 and 11] using The old unit's relative life and mana

Some info regarding Arrays (and Loops as well): [Trigger] - Loops and Arrays tutorial

 
Level 3
Joined
Sep 15, 2021
Messages
31
So you're referencing Workers[11] in your trigger which is the Dwarf.

You need to reference a random Workers[] from 1 to 11:
  • Unit - Replace (Triggering unit) with a Workers[Random integer number between 1 and 11] using The old unit's relative life and mana

Some info regarding Arrays (and Loops as well): [Trigger] - Loops and Arrays tutorial
Hi #Uncle !
Oh got it i was only referencing [11] ok ok .. hmm is there anything else i missed? according to #Nichilus's Answer
I didn't get what Step 5 and 6 mean .. can you give me some help?
 

Uncle

Warcraft Moderator
Level 70
Joined
Aug 10, 2018
Messages
7,375
Hi #Uncle !
Oh got it i was only referencing [11] ok ok .. hmm is there anything else i missed? according to #Nichilus's Answer
I didn't get what Step 5 and 6 mean .. can you give me some help?
That's all you need for this to work.

Nichilus was showing you how to do it with a Variable but it's not actually needed:
  • Set Variable random_race_index = Random integer number between 1 and 11
  • Unit - Replace (Triggering unit) with a Workers[random_race_index] using The old unit's relative life and mana
He was just explaining it in a way that would help you understand the overall concept.


Some more info:
The Integer inside of the [Bracket] is called an Index. It's what allows you to store multiple things inside of a single variable array.
The Max Index is the highest # you use in your Array, which in this case is [11] for the Dwarf. If you added a 12th Unit-Type then your Max Index would be 12.

A decent analogy would be to think about Arrays like a grocery list for when you go shopping:
1: Eggs
2: Milk
3: Potatoes

Note how this single list can contain near endless amounts of items. The list in this analogy is your variable array.
If you wanted a random one of these 3 items then you would pick a random number between 1 and 3.
If you picked 1 you would get Eggs, 2 gets you Milk, and 3 gets you Potatoes. Also, the Max Index here would be 3 since we have 3 total items.

So Arrays are great because they save you from having to create multiple Variables. For instance, if you ever find yourself creating a new Variable for EACH player, there's a good chance that you could save yourself the trouble by using a single Variable array instead.

For example if you wanted to create an enemy unit at each Player's "spawn" Point:
  • Player Group - Pick every player in (All players) and do (Actions)
    • Loop - Actions
      • Unit - Create 1 Footman for Neutral Hostile at SpawnPoint[(Player number of (Picked player))] facing Default building facing degrees
See how I use Player number here. Each Player in the game has a "Player Number", Red = 1, Blue = 2, Teal = 3, etc.

This allows you to use the [Index] of your Array to represent a different thing for each player:
  • Events
  • Map Initialization
  • Conditions
  • Actions
  • Set Variable SpawnPoint[1] = (Center of P1Spawn <gen>)
  • Set Variable SpawnPoint[2] = (Center of P2Spawn <gen>)
  • Set Variable SpawnPoint[3] = (Center of P3Spawn <gen>)

So as you can see, clever use of Arrays will cut down your Variable usage and Trigger count by A LOT. The [Index] can represent anything you want.
 
Last edited:
Level 3
Joined
Sep 15, 2021
Messages
31
That's all you need for this to work.

Nichilus was showing you how to do it with a Variable but it's not actually needed:
  • Set Variable random_race_index = Random integer number between 1 and 11
  • Unit - Replace (Triggering unit) with a Workers[random_race_index] using The old unit's relative life and mana
He was just explaining it in a way that would help you understand the overall concept.


Some more info:
The Integer inside of the [Bracket] is called an Index. It's what allows you to store multiple things inside of a single variable array.
The Max Index is the highest # you use in your Array, which in this case is [11] for the Dwarf. If you added a 12th Unit-Type then your Max Index would be 12.

A decent analogy would be to think about Arrays like a grocery list for when you go shopping:
1: Eggs
2: Milk
3: Potatoes

If you wanted a random one of these 3 items then you would pick a random number between 1 and 3.
If you picked 1 you would get Eggs, 2 gets you Milk, and 3 gets you Potatoes. Also, the Max Index here would be 3 since we have 3 total items.
This single list can contain near endless amounts of items.

Arrays are great because they save you from having to create multiple Variables. For instance, if you ever find yourself creating a new Variable for EACH player, there's a good chance that you could save yourself the trouble by using a single Variable array here instead. Clever use of Arrays will cut down your Variable usage and Trigger count by A LOT.
I see.. now i get what Index stuff is .. Thanks ! I'm new to these things such Jass or Java such etc..
hmm it seems my triggers are good to go, but if i choose Random Race, the "Currupt Elf" only comes out ..

It seems Random Number is always set to 6, that is why only BloodElf comes out.
What do you think?

Did i put something wrong in my trigger?





JASS:
Unit Type Initial
    Events
        Map initialization
    Conditions
    Actions
        Set VariableSet Workers[1] = Human
        Set VariableSet Workers[2] = Orc
        Set VariableSet Workers[3] = Elf
        Set VariableSet Workers[4] = Undead
        Set VariableSet Workers[5] = Devil
        Set VariableSet Workers[6] = CurruptElf
        Set VariableSet Workers[7] = HighElf
        Set VariableSet Workers[8] = Spiders
        Set VariableSet Workers[9] = Merlock
        Set VariableSet Workers[10] = BloodElf
        Set VariableSet Workers[11] = Dwarf

JASS:
Unit Type Initial 2
    Events
        Unit - A unit enters (Playable map area)
    Conditions
        (Unit-type of (Triggering unit)) Equal to Random Race
    Actions
        Set VariableSet Workers_Index = (Random integer number between 1 and 11)
        Unit - Replace (Triggering unit) with a Workers[Workers_Index] using The old unit's relative life and mana
 
Last edited:

Uncle

Warcraft Moderator
Level 70
Joined
Aug 10, 2018
Messages
7,375
So although you're technically using Jass, you're going to confuse people on here if you refer to your Triggers as Jass because you aren't actually writing any code. What you're doing is creating triggers in what we call GUI (Graphical User Interface) which is just the basic Trigger Editor.

Here's how you post your GUI triggers: How To Post Your Trigger

Jass looks like this. It's code that you would write in a text editor like Visual Studio:
JASS:
function SomeFunctionYouCreated takes nothing returns nothing
  local unit u = GetTriggerUnit()
  call KillUnit(u)
endfunction

Anyway, you don't need the Workers_Index variable but regardless that trigger should work fine. What I think is happening here is that your Game Seed isn't randomized so you're getting the same results every time. Note that this issue will only exist in Singleplayer tests as maps played online ALWAYS have randomized Game Seeds.

Go into the World Editor and click File/Preferences and uncheck the box: Use Fixed Random Seed

The Seed is basically just predetermined RNG. If it's fixed then the RNG never changes between game tests.
 
Last edited:
Level 3
Joined
Sep 15, 2021
Messages
31
So although you're technically using Jass, you're going to confuse people on here if you refer to your Triggers as Jass because you aren't actually writing any code. What you're doing is creating triggers in what we call GUI (Graphical User Interface) which is just the basic Trigger Editor.

Here's how you post your GUI triggers: How To Post Your Trigger

Jass looks like this. It's code that you would write in a text editor like Visual Studio:
JASS:
function SomeFunctionYouCreated takes nothing returns nothing
  local unit u = GetTriggerUnit()
  call KillUnit(u)
endfunction

Anyway, you don't need the Workers_Index variable but regardless that trigger should work fine. What I think is happening here is that your Game Seed isn't randomized so you're getting the same results every time. Note that this issue will only exist in Singleplayer tests as maps played online ALWAYS have randomized Game Seeds.

Go into the World Editor and click File/Preferences and uncheck the box: Use Fixed Random Seed

The Seed is basically just predetermined RNG. If it's fixed then the RNG never changes between game tests.


Cheers! This was because of what you assumed !
Now it works correctly Thanks #Uncle !

Thanks #Nichilus !
 
Status
Not open for further replies.
Top