Luke Haslett's Portfolio
Game Development Blog
For a lot of the work I have planned towards my first milestone, I have experience to support the confidence in my approach. But the one thing I don’t have practice in is map generation, so I decided to tackle that first.
With the system I’m employing being random level generation, I’ll need to use modular level pieces in my level design, and if I’m making multiple instances of similar rooms it only made sense to handle this with a class hierarchy structure. So, I made a base room with a few common variables, basic functionality, and only a collision box.
Using this as a parent class, I made a few variations of room tiles that I could use to test my script.
For the script itself, I spawn an actor at the start of the initialisation chain that will handle all the information of the rooms. At this point, the only information is an array of spawn points for each room tile that adds and removes available spawn points dynamically. I will also use this manager system later to handle more information about gameplay, like if the room is going to contain monsters, traps, loot, or a puzzle.
Next in the script, I run a custom event that checks what the predefined map size is. If we haven’t reached that amount we spawn a random room from an array of room tiles, to a random spawn point in the manager’s array of spawn points.
Then, we do a check to see if the collision boxes from the room class returns true. If so, we destroy the room tile, remove the spawn point, and run back through the whole event. But if false, we feed the new room’s available spawn points into the manager’s array, decrement the map size left to spawn, and run back through the event.
What we then get is this:
The finished version won’t have a delay between rooms, I added this for the recording to show the spawning noticeably working.
Something to note with this style of map generation, is sometimes a room can spawn alongside another, which presents a doorway that leads into the back wall of the adjacent room.
However, I can deal with this by making a non-interactable door or flush wall visible on the room tile during the collision checks.
Finally, after the map has been made, I run another custom event to cap off any leftover spawn points that lead to nowhere, using a room tile with no attachments available.
There are other methods to random map generation that can involve using 2D grids, Voronoi diagram algorithms on random polygons, or digging rooms and corridors with instanced static meshes like the example project from the UE4 marketplace. But I decided to go with a simplistic approach of trial and error array checks as the overall scope of the project is small and the method is fit for purpose.