03 Randomizing Combatants

In this tutorial we’ll give our combatants randomly generated names and appearances.

Our game will use the same combatants for the player and enemy – and the player can have multiple of each combatant. They’re just soldiers fighting for the princess after all. We’ll only use the following combatants in these tutorials:

  • warrior
  • rogue
  • ranger
  • wizard
  • cleric
  • monk

To make them a bit distiguishable from each other, we’ll generate a random name and have one of 6 different appearances (prefabs) for each of them. The random names are generated out of lists of first and last names using a schematic. The random prefab uses a variable on the combatant and conditional prefabs.

We’ll set up:

  • name generation schematic
  • update combatants
  • update start schematic (joining the army of the princess)

Let’s get to it!

Random Name Generation Schematic #

The tutorial assets contain 2 text files, one for first names and one for last names – each name is on it’s own line. We’ll read those names, store them in string list variables and randomly combine them.

This schematic will be used by player combatants when they’re initilized.

Navigate to Schematics and start working on a new schematic.

Variable Exits #

Add Node > Value > Variable > Variable Exists

This node checks if a variable exists – we’ll only need to read the names from files once, so we check if we already did it and created the variable.

  • Variable Key
    Set to firstName.
  • Variable Origin
    Select Global.
  • Is List
    Enable this setting.
  • Type
    Select String.

Read Text File Lines #

Add Node > Value > Text File > Read Text File Lines

This node reads a text file line-by-line and stores them into a string list variable.

This node is conencted to the Failed slot of the Variable Exists node.

  • Text Asset
    Select firstNames.
  • Remove Empty Lines
    Enable this setting.
  • Variable Key
    Set to firstName.
  • Variable Origin
    Select Global.

Read Text File Lines #

Copy the previous Read Text File Lines node, we’ll now read the last names.

  • Text Asset
    Select lastNames.
  • Variable Key
    Set to lastName.

Change Variables #

Add Node > Value > Variable > Change Variables

We’ll now combine a random first and a random last name.

This node is connected to the Success slot of the Variable Exists node and the Next slot of the 2nd Read Text File Lines node.

Click on Add Variable.

  • Change Type
    Select Variable.
  • Variable Key
    Set to name.
  • Variable Origin
    Select Local.
  • Type
    Select String.
  • Operator
    Select Set.
  • String Value
    Select the Variable > String Variable value type (right of the text field).
    Set the value to firstName.
  • Variable Origin
    Select Global List.
  • List Origin
    Select Random.

Copy Variable Change 0.

  • Operator
    Select Append Back.
  • String Value
    Select the Value > Value value type.
    Set the value to a space (‘ ‘), since we want to have a space between first and last name.

Copy Variable Change 1.

  • String Value
    Select the Variable > String Variable value type.
    Set the value to lastName.
  • Variable Origin
    Select Global List.
  • List Origin
    Select Random.

Set Combatant Name #

Add Node > Combatant > Combatant > Set Combatant Name

We’ll now set the combatant’s name to the name we generated in the name variable.

This schematic will be used as init schematic of the combatant, i.e. the combatant is available as Machine Object.

  • Use Object
    Enable this setting.
  • Object
    Select Machine Object.
  • Name
    Select the Variable > String Variable value type.
    Set the value to name.
  • Variable Origin
    Select Local.

And that’s it for the schematic. Click on Save Schematic to save it, e.g. as RandomNameGeneration.

Combatants General Settings #

Next, we’ll set up some general combatant settings for our randomization.

Navigate to Combatants > Combatants > General Settings.

Base Settings > Object Variable Settings #

We’ll add (local) object variables to all combatants and set an int variable to a random value from 0 to 5 – this’ll be used for the combatant’s conditional prefabs to define their appearance.

  • Use Object Variables
    Enable this setting.
  • Local Variables
    Enable this setting.
    This’ll make the variables unique for each instance of a combatant.
    Otherwise, we’d have to define an object ID, making them share the variables.

Click on Add Variable.

  • Variable Key
    Set to look.
  • Type
    Select Int.
  • Operator
    Select Set.
  • Float Value
    Select Value > Random.
    Set the first value to 0.
    Set the second value to 5.
  • Math Function
    Select Round.

Base Settings > Default Schematic Settings > Init Schematics #

We’ll only use the schematic for the player combatants – we don’t randomize the enemy names and keep their role names, so we know what we’re dealing with.

  • Player Schematic
    Select the RandomNameGeneration schematic.

Updating Combatants #

Next, we’ll update our combatants and set up the prefabs and conditional prefabs.

First, however, we’ll prevent our princess from getting a random name.

Navigate to Combatants > Combatants and select the Princess combatant (and the others afterwards).

17: Princess #

We’ll override the default init schematics

Base Settings > Schematic Settings > Init Schematics #

  • Own Init Schematics
    Enable this setting.

0: Warrior #

Alright, we’ll start with the prefabs for our warrior – the setup will be the same for all combatants, just using different prefabs.

We’ll also set up an icon to hint at this combatant’s role, it’ll be displayed in combatant selections to know what kind of combatant we’re actually using.

  • Sprite (Icon)
    Select icons_482.

Click on the Find TMP Tag button to get the icon’s tag for displaying it in texts (via text codes). This uses TextMesh Pro’s sprite asset setup.

Base Settings > Prefab Settings #

  • Prefab
    Select BlueNinja.
  • Update Mode
    Select Fixed Update.

Click on Add Conditional Prefab

  • Prefab
    Select BlueSamurai.
  • Update Mode
    Select Fixed Update.

We’ll add a variable condition to check for look being 1. Click on Add Variable in the Variable Conditions.

  • Variable Key
    Set to look.
  • Variable Origin
    Select Local.
    In conditoina prefabs, the combatant’s variables are Local variables.
  • Type
    Select Int.
  • Check Type
    Select Is Equal.
  • Check Value
    Select Value > Value.
    Set the value to 1.

Copy Conditional Prefab 0.

  • Prefab
    Select GoldKnight.
  • Check Value (Variable Condition)
    Set the value to 2.

Copy Conditional Prefab 1.

  • Prefab
    Select Knight.
  • Check Value (Variable Condition)
    Set the value to 3.

Copy Conditional Prefab 2.

  • Prefab
    Select RedSamurai.
  • Check Value (Variable Condition)
    Set the value to 4.

Copy Conditional Prefab 3.

  • Prefab
    Select Samurai.
  • Check Value (Variable Condition)
    Set the value to 5.

1: Rogue #

Continuing with the rogue’s setup – like before, we set up the base prefab and conditional prefabs.

  • Sprite (Icon)
    Select icons_408.

Click on the Find TMP Tag button to get the icon’s tag for displaying it in texts (via text codes). This uses TextMesh Pro’s sprite asset setup.

Base Settings > Prefab Settings #

  • Prefab
    Select Boy.
  • Update Mode
    Select Fixed Update.

Click on Add Conditional Prefab

  • Prefab
    Select DarkNinja.
  • Update Mode
    Select Fixed Update.

We’ll add a variable condition to check for look being 1. Click on Add Variable in the Variable Conditions.

  • Variable Key
    Set to look.
  • Variable Origin
    Select Local.
  • Type
    Select Int.
  • Check Type
    Select Is Equal.
  • Check Value
    Select Value > Value.
    Set the value to 1.

Copy Conditional Prefab 0.

  • Prefab
    Select GrayNinja.
  • Check Value (Variable Condition)
    Set the value to 2.

Copy Conditional Prefab 1.

  • Prefab
    Select GreenNinja.
  • Check Value (Variable Condition)
    Set the value to 3.

Copy Conditional Prefab 2.

  • Prefab
    Select RedNinja.
  • Check Value (Variable Condition)
    Set the value to 4.

Copy Conditional Prefab 3.

  • Prefab
    Select RedNinja2.
  • Check Value (Variable Condition)
    Set the value to 5.

2: Ranger #

And the same for the ranger.

  • Sprite (Icon)
    Select icons_392.

Click on the Find TMP Tag button to get the icon’s tag for displaying it in texts (via text codes). This uses TextMesh Pro’s sprite asset setup.

Base Settings > Prefab Settings #

  • Prefab
    Select EggBoy.
  • Update Mode
    Select Fixed Update.

Click on Add Conditional Prefab

  • Prefab
    Select EggGirl.
  • Update Mode
    Select Fixed Update.

We’ll add a variable condition to check for look being 1. Click on Add Variable in the Variable Conditions.

  • Variable Key
    Set to look.
  • Variable Origin
    Select Local.
  • Type
    Select Int.
  • Check Type
    Select Is Equal.
  • Check Value
    Select Value > Value.
    Set the value to 1.

Copy Conditional Prefab 0.

  • Prefab
    Select Greenman.
  • Check Value (Variable Condition)
    Set the value to 2.

Copy Conditional Prefab 1.

  • Prefab
    Select Lion.
  • Check Value (Variable Condition)
    Set the value to 3.

Copy Conditional Prefab 2.

  • Prefab
    Select MaskedNinja.
  • Check Value (Variable Condition)
    Set the value to 4.

Copy Conditional Prefab 3.

  • Prefab
    Select MaskFrog.
  • Check Value (Variable Condition)
    Set the value to 5.

3: Wizard #

This is getting exhausting, but we’re halfway there – continuing with the wizard.

  • Sprite (Icon)
    Select icons_269.

Click on the Find TMP Tag button to get the icon’s tag for displaying it in texts (via text codes). This uses TextMesh Pro’s sprite asset setup.

Base Settings > Prefab Settings #

  • Prefab
    Select EskimoNinja.
  • Update Mode
    Select Fixed Update.

Click on Add Conditional Prefab

  • Prefab
    Select Inspector.
  • Update Mode
    Select Fixed Update.

We’ll add a variable condition to check for look being 1. Click on Add Variable in the Variable Conditions.

  • Variable Key
    Set to look.
  • Variable Origin
    Select Local.
  • Type
    Select Int.
  • Check Type
    Select Is Equal.
  • Check Value
    Select Value > Value.
    Set the value to 1.

Copy Conditional Prefab 0.

  • Prefab
    Select Noble.
  • Check Value (Variable Condition)
    Set the value to 2.

Copy Conditional Prefab 1.

  • Prefab
    Select Villager.
  • Check Value (Variable Condition)
    Set the value to 3.

Copy Conditional Prefab 2.

  • Prefab
    Select Villager2.
  • Check Value (Variable Condition)
    Set the value to 4.

Copy Conditional Prefab 3.

  • Prefab
    Select Villager3.
  • Check Value (Variable Condition)
    Set the value to 5.

4: Cleric #

Aaaaand the cleric!

  • Sprite (Icon)
    Select icons_280.

Click on the Find TMP Tag button to get the icon’s tag for displaying it in texts (via text codes). This uses TextMesh Pro’s sprite asset setup.

Base Settings > Prefab Settings #

  • Prefab
    Select Cavegirl.
  • Update Mode
    Select Fixed Update.

Click on Add Conditional Prefab

  • Prefab
    Select Cavegirl2.
  • Update Mode
    Select Fixed Update.

We’ll add a variable condition to check for look being 1. Click on Add Variable in the Variable Conditions.

  • Variable Key
    Set to look.
  • Variable Origin
    Select Local.
  • Type
    Select Int.
  • Check Type
    Select Is Equal.
  • Check Value
    Select Value > Value.
    Set the value to 1.

Copy Conditional Prefab 0.

  • Prefab
    Select Eskimo.
  • Check Value (Variable Condition)
    Set the value to 2.

Copy Conditional Prefab 1.

  • Prefab
    Select OldMan.
  • Check Value (Variable Condition)
    Set the value to 3.

Copy Conditional Prefab 2.

  • Prefab
    Select OldMan3.
  • Check Value (Variable Condition)
    Set the value to 4.

Copy Conditional Prefab 3.

  • Prefab
    Select Villager4.
  • Check Value (Variable Condition)
    Set the value to 5.

5: Monk #

Finally, the last one!

  • Sprite (Icon)
    Select icons_429.

Click on the Find TMP Tag button to get the icon’s tag for displaying it in texts (via text codes). This uses TextMesh Pro’s sprite asset setup.

Base Settings > Prefab Settings #

  • Prefab
    Select Caveman.
  • Update Mode
    Select Fixed Update.

Click on Add Conditional Prefab

  • Prefab
    Select Caveman2.
  • Update Mode
    Select Fixed Update.

We’ll add a variable condition to check for look being 1. Click on Add Variable in the Variable Conditions.

  • Variable Key
    Set to look.
  • Variable Origin
    Select Local.
  • Type
    Select Int.
  • Check Type
    Select Is Equal.
  • Check Value
    Select Value > Value.
    Set the value to 1.

Copy Conditional Prefab 0.

  • Prefab
    Select Master.
  • Check Value (Variable Condition)
    Set the value to 2.

Copy Conditional Prefab 1.

  • Prefab
    Select Monk.
  • Check Value (Variable Condition)
    Set the value to 3.

Copy Conditional Prefab 2.

  • Prefab
    Select Monk2.
  • Check Value (Variable Condition)
    Set the value to 4.

Copy Conditional Prefab 3.

  • Prefab
    Select OldMan2.
  • Check Value (Variable Condition)
    Set the value to 5.

Phew!

Save Changes #

That’s it for the setup in the editor.

Don’t forget to save your changes by clicking on Save Settings at the bottom of the editor.

Updating the Start Game Schematic #

As a final push for this tutorial, we’ll update the StartGame schematic and join a bunch of combatants to the player group.

Navigate to Schematics and open the StartGame schematic. We’ll just add new nodes at the end.

Join Group #

Add Node > Group > Group > Join Group

We’ll add a warrior – and we’ll give each of our combatants a random level between 3 and 5.

  • Group Origin
    Select Active Player Group.
  • Combatant
    Select Warrior.

We’ll change the start level in the Level Settings.

  • Set Level
    Select Random.
  • Minimum Random
    Set to 3.
  • Maximum Random
    Set to 5.

Join Group #

Copy the previous Join Group node, we’ll just add another warrior.

Join Group #

Copy the previous Join Group node, we’ll now add a rogue.

  • Combatant
    Select Rogue.

Join Group #

Copy the previous Join Group node, we’ll just add another rogue.

Join Group #

Copy the previous Join Group node, we’ll now add a ranger.

  • Combatant
    Select Ranger.

Join Group #

Copy the previous Join Group node, we’ll just add another ranger.

Join Group #

Copy the previous Join Group node, we’ll now add a wizard.

  • Combatant
    Select Wizard.

Join Group #

Copy the previous Join Group node, we’ll now add a cleric.

  • Combatant
    Select Cleric.

Join Group #

Copy the previous Join Group node, we’ll now add a monk.

  • Combatant
    Select Monk.

And that’s it for the schematic – click on Save Schematic to save the changes.

 

We’re done for now.

Next, we’ll set up a bit of UI.