Level generation

Hi, I would like to generate a maze as a part of my level. I know how to do that in JS or another programming language, but can I do it in GDevelop?

For example, let’s take Aldous-Broder algorithm

1. Pick a random cell as the current cell and mark it as visited.
2. While there are unvisited cells:
  1. Pick a random neighbour.
  2. If the chosen neighbour has not been visited:
    1. Remove the wall between the current cell and the chosen neighbour.
    2. Mark the chosen neighbour as visited.
  3. Make the chosen neighbour the current cell.

Most certainly. But you’ll need to be aware of GDevelop’s “quirk” where if you have an event in which you select one instance of an object with conditions, then you can’t easily work on other instances of that object within the scope of the event.

The way I’d approach it is to have a tester object, place it on all 4 sides of the current cell object. Remove any tester objects that are on a visited cell objects. Then pick a random tester, and set the cell object it is on as visited and current.

The one thing I don’t understand from your description f the Aldous-Broder algorithm, is what happens if the algorithm paints itself into a corner, like this (starts at 1, current cell is 6):

Thanks for the hint.

When there are no “not visited” cells near the current cell, we have to take a “step back”. So if your current cell is 6 and there are no more cells to visit, you have to set the current cell to “5”. In more detail, you can read about that algorithm here:

weblog.jamisbuck.org/2011/1/17/maze-generation-aldous-broder-algorithm

Personally, I prefer the Growing Tree algorithm

weblog.jamisbuck.org/2011/1/27/maze-generation-growing-tree-algorithm.html

So it becomes a recursive function. That’ll be a bit trickier in GDevelop. Two possible ways to do it:

  1. Create an extension with a function to operate on each cell as per algorithm. You could store the grid in an array of arrays.

  2. If you already know how to do it in JS, you could create a JavaScript event, code it in that and store the result in a GDevelop array variable to be accessed in the events.

I assume you’d look at using bitmasking to determine which walls of a cell are removed (or kept), which you could then use in the events with relative ease.

Maybe this can help:

1 Like

Thanks for the help, I will share any results about that. Unfortunately, currently I have limited time for “for fun development” so it may take a while :frowning:

As I promised, I sent a short video on what I achieved.
3a61fb6896714d

@MrMen @davy thanks for help :slight_smile:

1 Like