The dungeon generator in action, with the ability to control iteration count and regenerate with a new random seed. Seeds can be saved externally to reload the dungeon later.
Taking heavy inspiration from Marcin Seredynski's fantastic article on The Elder Scrolls II: Daggerfall's dungeon generation (linked below), I wanted to try my hand at recreating a similar system in Unreal Engine.
.
I started by creating a modular set of corridor, junction, room and end cap meshes in Maya, using locators to designate entrance/exit points' position and orientation. Using the prefix "Socket_" on each locator, they are then converted to sockets when the mesh is imported into Unreal.
.
After importing these meshes, I utilized the Prefabricator plugin by Code Respawn to create more detailed variations of each modular piece. Making sure these prefabs have the same origin point as their mesh counterparts, I assembled Prefab Collection assets for further randomization down the line. It's important to note that certain sub-actors seem to have trouble initializing correctly when spawned with code rather than hand-placed in the level. For example, an explosive barrel with physics enabled needed to be saved in the Prefab asset using a separate "Spawner" actor rather than the barrel actor itself.
.
Before creating the Dungeon Generator blueprint, I set up a couple structs to hold information for use in generating the preliminary layout and later swapping each component of the layout with Prefabricator assets.
.
All of this setup came together in the Dungeon Generator actor blueprint, using the construction script to allow for in-editor previsualization of the final appearance and layout of the dungeon. Since Prefabricator assets cannot be spawned with code prior to runtime, off of begin play the layout Static Mesh Components are destroyed and replaced with randomized Prefab Collection assets.
.
There are quite a few limitations with this approach, as outlined by Seredynski's article. Most notably, it's likely that most if not all branches in the generated dungeon lead to a dead end. My temporary recompense for this is to allow the player to teleport back to their initial entry point and/or offer valuable loot at these dead ends to make traversing the whole level worthwhile. I intend to keep iterating on and improving this system as the main game's development chugs along, and I'm excited to see what other interesting directions I can take it in.
.
Thanks for reading!
.
Code Respawn's Prefabricator plugin:
https://www.unrealengine.com/marketplace/en-US/product/prefabricator
.
Marcin Seredynski's article on dungeon generation:
https://www.gamedeveloper.com/design/bake-your-own-3d-dungeons-with-procedural-recipes