Game Toolkit for Unity
Setting up the procedural level generation.
In this tutorial we’ll set up schematics to generate the game level using sprite prefabs. We need 2 schematics, one for randomly spawning prefabs (walls, food, enemies) in the level and one for setting up the level.
First, we’ll create the schematic to randomly place game objects in the level. The objects will be placed on fixed positions – to prevent having 2 objects at the same position, we’ll make use of variable lists storing the positions.
The Vector3 variable list will be filled in a different schematic – this schematic only handles spawning prefabs at the positions and removing the used position.
Open the Makinom editor, navigate to Schematics and create a new schematic. Change the following settings.
We need to add a prefab resource.
We’ll be using the resource override to spawn different game objects with this schematic. Click on Add Prefab Resource to add a prefab resource.
There isn’t anything else to do – we’ll override the prefab in the tagged machines later.
Add Node > Value > Variable > Check Variables
This node will check for variable conditions. We’ll use it to check if any game object should be spawned at all.
The objectCount variable defining the quantity of game objects we’ll spawn will be set by the tagged machines as local start variable.
Click on Add Variable to add a variable condition.
Add Node > Game Object > Prefab > Spawn Prefab
This node is used to spawn a prefab. We’ll use it to spawn a prefab at one of the Vector3 positions stored in the position local variable list.
It’s connected to the previous node’s Success slot.
Add Node > Base > Loop
This node is used to increase the counter until it reaches a maximum value. The objectCount variable defining the quantity of game objects we’ll spawn will be set by the tagged machines as local start variable.
This node’s Loop slot will be connected back to the Spawn Prefab node.
The Variable Settings (Counter) define the variable we’re using as counter, it’ll be increased by 1 each time the node is used.
That’s it for the schematic – click on Save Schematic and save it as RandomPlacement in Assets/Schematics/.
Next, we’ll create the schematic to set up the level. This schematic will fill the position list, create the floor and walls around the level, and start the tagged machines that’ll randomly place the other objects.
The level is set up using a defined number of rows and columns – they’ll be set up as local start variables of the machine. In the schematic, we’ll be going through the rows and columns field by field to generate positions and spawn prefabs.
The different tasks (filling the position list, creating floor and walls, randomply placing objects) will be set up on separate Node Layers. The node layer selection can be found in the upper right corner of the editor. You can connect node layers using Layer Gate nodes.
Create a new schematic and change the following settings.
The following nodes are placed in layer 0: Base Layer. Layer 0 is the default layer.
We need to define 2 local start variables, initialize 2 local variables and 3 prefab resources.
We’ll set up local variables as Machine Start Variable for easy setup in the machine component at a later time. When using the schematic in a machine component, the defined start variables will be added automatically, using their default values.
Click on Add Start Variable to add a local start variable that will be exposed to the machine component’s inspector.
Copy the previous start variable and change the following setting.
Click on Add Variable to add a variable change.
Copy the previous variable and change the following setting.
We need 3 prefab resources – the outer walls, the floor and the exit. Click on Add Prefab Resource to add the first prefab resource.
We need 3 prefabs in this prefab resource, add the needed amount by clicking on Add Prefab.
Click on Add Prefab Resource again to add another prefab resource.
This time, we need 8 prefabs.
Click on Add Prefab Resource to add the last prefab resource.
There’s only one exit prefab, so we don’t need to add additional prefabs.
Add Node > UI > Fade Screen
This node is used to fade the screen. We’ll use it to initially set the screen to black when the game first starts.
Add Node > UI > Dialogue > Show Dialogue
This node is used to show a dialogue. We’ll use it to display the current day (level).
We’ll set up the actual UI box to display the message at a later time, but we already have the Default box available (which we’ll set up later).
Add Node > Value > Variable > Change Variables
We’ll use this node to add a position (Vector3) based on the local variables x and y to the position list.
The X-axis will use the local int variable x.
The Y-axis will use the local int variable y.
The Z-axis isn’t used, so we just set it to 0.
We’ll loop y until we reach rows – 1.
This node’s Loop slot will be connected back to the previous Change Variables node (adding a position).
Now, we’ll reset y to 1.
This node is connected to the previous node’s Finished slot.
We’ll loop x until we reach columns – 1.
This node’s Loop slot will be connected back to the first Change Variables node (adding a position).
Next, we’ll add a Layer Gate node to the reach the next layer. This node is connected to the previous node’s Finished slot.
Right click on the Finished slot and select Add Node > Gate to Layer > Add New Layer. This will create a new layer and add a layer gate to it.
To display the new layer, either double click on the on the node’s title or select it in the layer popup field in the upper right corner of the editor.
You can rename the layer by clicking on the Rename Layer icon right of the popup field – e.g. name the layer Create Level. Clicking on the icon again will stop editing the layer’s name.
The following nodes are placed in layer 1: Create Level. In this layer, we’ll create the floor and walls around the level.
First, we’ll set x and y to -1, because we also need to place floor and walls around the actual level (rows/columns).
This node is connected to Next slot of the Gate Exit from layer 0.
Copy the previous variable change.
We’ll use this node to check if we’re at the outer edge of the level, i.e. if x or y are -1 or at their maximum (rows/columns)
Copy the previous variable condition and change the following settings.
Again, copy the prevous variable condition.
Again, copy the previous variable condition.
We’ll use this node to spawn an outer wall.
This node is connected to the Success slot of the previous node – i.e. the outer wall is spawned when we are at the edge of the level.
This time, we’ll spawn the floor. Copy the previous node and change the following settings.
The first Check Variables node’s Failed slot is connected to this node, i.e. we’ll spawn a floor in any case.
We’ll loop y until we reach rows + 1.
This node’s Loop slot will be connected back to the first Check Variables node (checking if it’s an outer edge before spawning the prefabs).
Now, we’ll reset y to -1.
We’ll loop x until we reach columns + 1.
You can rename the layer by clicking on the Rename Layer icon right of the popup field – e.g. name the layer Random Placement. Clicking on the icon again will stop editing the layer’s name.
The following nodes are placed on layer 2: Random Placement. In this layer, we’ll start tagged machines, spawning the in-level walls, food and enemies.
Add Node > Machine > Start Tagged Machine
This node is used to start a tagged machine. We’ll use it to start a tagged machine with the tag walls, which will use the RandomPlacement schematic to randomly place walls in the level.
This node is connected to the Next slot of the Gate Exit from layer 1.
Click on Add Tag to add a starting tag.
We’ll use this node to start a tagged machine with the tag food, which will use the RandomPlacement schematic to randomly place food in the level.
Copy the previous node and change the following settings.
Before placing the enemies, we’ll set the objectCount for it in this schematic instead of the tagged machine as local start variables. The enemy count will be determined by the level used in a logarythmic calculation.
We’ll use this node to start a tagged machine with the tag enemies, which will use the RandomPlacement schematic to randomly place enemies in the level.
Copy one of the previous Start Tagged Machine nodes, connect it to the Change Variable node’s Next slot and change the following settings.
Now, we’ll spawn the exit at the upper right corner of the level (i.e. rows – 1 and columns – 1).
Now it’s time to fade in the screen.
Add Node > Game > Game > Change Game State
This node is used to change game states. We’ll use it to activate Game Running and In Control.
The game states are used to trigger the roguelike game mechanics – waiting for the player’s input and moving the enemies afterwards. This will be handled by later schematics.
Click on Add Game State to add a game state change.
Click on Add Game State again to add another state change.
And that’s it for this schematic – click on Save Schematic and save it as BoardManager in Assets/Schematics/.
The next tutorial will handle adding the level generation machines.