Random Choice
Contents |
Overview
This tutorial will cover the basics of how to create a script to randomly select one of two Unit Spawners every time the Player enters a specific area and then spawn a Monster. It will assume you already have a fundamental understanding of how to navigate and use the GUTS editor along with being familiar with Basic Scripting.
Step-by-Step
Initial Set Up
- Create/Load a level with a pathable area for the Player to walk and Monster Objects to spawn.
- Turn Show Helpers on ( Ctrl + H ) to make the logic objects that will be created visible in the editor.
Add Necessary Objects
Right-click on an empty space in the Layout Scene Manager and add the following objects:
- Logic Group
- Logic > Player Sphere Trigger
- Logic > Random Choice
- Logic > Unit Spawner
- Logic > Unit Spawner
Move Objects
- Move the Player Sphere Trigger to a pathable area where the Player should trigger the Random Choice to happen.
- Move the two Unit Spawners to pathable areas where the Monsters can spawn.
Change Properties
Select the first Unit Spawner and change the following properties in the Properties Window:
- "Spawn on Create" to False.
- This sets the Unit Spawner to only spawn when it receives a "Spawn Units" Input event instead of automatically on level load.
- "Resource" to "Skeleton".
- This determines the type of Monster to spawn.
- "Count" to 1.
- This is the total number of Monsters to spawn.
Select the second Unit Spawner and change the following properties in the Properties Window:
- "Spawn on Create" to False.
- This sets the Unit Spawner to only spawn when it receives a "Spawn Units" Input event instead of automatically on level load.
- "Resource" to "Zombie".
- This determines the type of Monster to spawn.
- "Count" to 1.
- This is the total number of Monsters to spawn.
Select the Player Sphere Trigger and change the following properties in the Properties Window:
- "Radius" to 4.
- This sets the size of the Player Sphere Trigger. The Player must enter this sphere to trigger it.
Select the Random Choice Object and change the following properties in the Properties Window:
- "One" and "Two" to 1.
- This creates two choices for the Random Choice Object to pick between and gives them both an equal weight of 1
Edit Logic Group
Select the Logic Group, open the Logic Editor, and add the Player Sphere Trigger, Random Choice, and two Unit Spawner objects to the Logic Editor window.
- Specify the "Triggered" Output on the Player Sphere Trigger.
- This Output event is fired every time the Player enters the Player Sphere Trigger.
- Specify the "Roll" Input on the Random Choice Object.
- This makes the Random Choice Object randomly select an output.
- Specify the "One" and "Two" Outputs on the Random Choice Object.
- These are the two Outputs which will be randomly selected between.
- Specify the "Spawn Units" Input on both of the Unit Spawners.
- This will spawn the "Resource" set on the Unit Spawner.
Link the "Triggered" Output to the "Roll" Input. Then link the "One" Output to the "Spawn Units" Input on one Unit Spawner and the "Two" Output to the "Spawn Units" Input on the other Unit Spawner.
Conclusion
When the Player enters the Player Sphere Trigger the Random Choice Object will randomly select one of the two Unit Spawners and spawn the specified Monster. The Player can enter and exit the Player Sphere Trigger repeatedly to re-roll the Random Choice Object and verify it is actually randomly choosing one of the Unit Spawners every time. Random Choice Objects can have up to five different choices and be linked to other Random Choice Objects for even further randomization.