Skip to main content
Version: 3.9

Custom Create Dungeon Function

Introduction

The provided Dungeon Generator class in the plugin has a basic default generation algorithm.
Its behavior is to create a first room and then add new rooms to existing doors.

See the Create Dungeon function in the diagram below:

dungeon changed
OR generate called
Idle
all levels
unloaded
Unload Levels
all levels loaded
and initialized
Load Levels
is server AND
generate called
else
Pre Generation
Post Generation
Generation Init
no
Choose First Room
Choose Next Room
no
On Room Added
no
yes
Is Dungeon
Valid?
yes
no
All Tries
Exhausted?
Initialize Dungeon
Generation Failed
Must be overriden
Can be overriden
Internal only
Dungeon Generator's
State Machine
Continue To
Add Room?
yes
yes
no
Create Dungeon
Function
Start
End
LEGEND
Room Limit
Reached?
yes
yes
no
All Tries
Exhausted?
On Failed to Add Room
yes
Can Place
Room?
This function is
overridable !!!
Create Dungeon
Generation Success

If you are not satisfied with this default behavior, you can write your own Create Dungeon function while keeping the core features of the generator.

The Dungeon Generator Base class

To write your own custom Create Dungeon, you'll need to create a new class deriving from Dungeon Generator Base.
This class has the core features of a dungeon generator: the state machine to load/unload the level instances (shown in the diagram above), the network replication, the room culling system, etc.

You have to create a new blueprint class deriving from Dungeon Generator Base.

Then, the Create Dungeon function become overridable.

There are several functions to use inside the Create Dungeon.
See the screenshot below for a list of them, and how to use them.

caution

This is a non-working example!
Just to show you the most important functions and their use.

Here the list of functions provided by the Dungeon Generator Base class:

  • Start New Dungeon: Call this at the beginning of a dungeon creation. It will reset the rooms and call Generation Init.
  • Finalize Dungeon: Call this at the end of dungeon creation. It will initialize all room instances and call Initialize Dungeon.
  • Create Room Instance: Use this function to create a new room instance based on a room data. You will be able to move it afterward and try to place it in the dungeon.
  • Discard Room Instance: Use this function to explicitly discard a room instance from the dungeon creation. The room must not be placed in the dungeon as this instance will be pooled and may be reused afterward when calling Create Room Instance.
  • Try Place Room: Call this to move the room instance, using one of its door to target an existing door location. This function will return whether the room instance overlaps another existing room in the dungeon or optionally colliding with the world (outside of the dungeon). The room is not added to the dungeon yet!
  • Try Place Room At Transform: Same as Try Place Room but you provide directly the room transform instead of letting the plugin compute the room transform from a target door location.
  • Add Room To Dungeon: Use this to finally add the room instance to the dungeon. It will try to connect its door indices you provide (or all doors if you don't provide them). It will call On Room Added if the room is actually added to the dungeon. You may call On Failed To Add Room if the room is not added to the dungeon.
  • Yield Generation: See below section for its use.

Splitting the workload on multiple frames

If your Create Dungeon function does a heavy workload that causes CPU spikes, you can split the workload on multiple frames.

To do so, you can use the node Yield Generation which will tell the generator to call the Create Dungeon function once again in the next frame.

That way, you can for example group the room placements in small batches each frame!

note

Make sure to add some flags in your algorithm to avoid calling Start New Dungeon or Finalize Dungeon each time.