Let’s dive into the detail of our procedural system. As I mentioned last time, the procedural nature of the environments for the game can be broken into 3 basic layers
- Random Loot
- Randomized Prop Placement
- Random City layout
This post, I’m going to go over the second point – Randomized Prop placement, as this is the core of the entire system.
What is a Prop?
In the system that I’m designing a ‘Prop’ is a hierarchical set of elements that has 3 key behaviours:
- Spawn a set of children
- Spawn a Random entry from a list of children
- Automatic discovery & configuration at Edit time
The first key feature is that Props will spawn a collection of children. For example, a section of a level could be built up from a number of different meshes – floors, walls, ceiling for example. A Prop is the container that we use to spawn the entire section at once – we want each section to have a floor, walls and ceilings. The Prop doesn’t know or care about placement of these elements, all it knows is that when we load the level, we spawn the necessary dependencies.
If we think of a typical location, say a building, it might look something like this:
- Front Entrance
Each of these sub-elements (Hallways, Suites, Front Entrance, Elevator) are containers that manage the child objects within. A multi-level building is simply a different collection of the same elements, minus the Front Entrance:
- First Floor
- Second Floor
And so on.
The beauty of this hierarchical system is in it’s simplicity – each element only cares about it’s immediate children and nothing else. So the ‘Floor’ element doesn’t know ‘what’ is in the Hallway or Suite ‘Props’, only that it needs to spawn one.
This is where things get interesting – the key to build an entire environment from a collection of props is that they are hierarchical in nature. Props can spawn props which can spawn props (and so on). This scales from individual elements all the way up to buildings and city blocks.
The second feature is the ability to spawn a random entry from a list of children. What this means is that a Prop can have a collection of children, but when spawning, will only pick ONE of the list of children, instead of spawning them all like we have described above. This lets us configure a wide range of potential configurations for our child elements and the Prop will pick one at random during initialization.
For example, to continue our Building example from above, the ‘Suite’ prop might have multiple configurations, like so:
- 1 bedroom
- 2 bedroom
- 3 bedroom
When the Building is initialized, it first spawns the ‘Floor’ Prop and then the ‘Suite’ prop from the list. Instead of the suite spawning all 3 potential layouts, it will randomly pick ONE of them. Some Suite props will be 1 bedroom, some will be 2 bedroom and so on.
This also works in a hierarchical nature, so you can have Random Room Layouts within the Suite – and within a Room have random placement of furniture – a living room Prop might have different furniture, different placement of the furniture, a book case might have random placement of books and so on.
Automatic Discovery & Configuration
This is the most critical piece of the puzzle for me to be able to rapidly set up procedural scenarios – automatic configuration. As I mentioned before, I’m an extremely lazy designer – and I don’t want to spend a huge amount of time preparing content for use in the game.
With this in mind, the Prop component has the ability to ‘discover’ any child elements in the scene hierarchy and automatically configure itself appropriately. This makes it very easy to port / migrate an existing Unity scene and turn it into a Procedural scene – we simply add the “Prop’ component to the parent GameObject of a scene element and the Prop will automatically prepare itself.
That’s enough theory for today, next time I’ll jump into a scene and show some examples of how the Prop system is used in practice!