Random Map Generation (Issue with Collisions)

I created a roll-and-write board game outside of Gdevelop. One of the key draws to the game is the randomization of hex tiles on a map. I am making an app that allows the user to randomly generate hex tiles on the map and then save and print the result to play the actual game.

There is a limited amount of certain tile objects so I have the code to limit the amount of certain tiles that can be added to the map. I also include an option for the user to increase the amount of a certain tile up or down with a max and min set.

I have a version of this working, but it take too much time when the user adds more tiles to the map. I have a set amount of invisible tiles that I call “spawner” that are positioned to fit the tiles in the image of the map. I have code that randomly picks one of these spawners and creates a tile to that selected spawner’s position.

I have a condition that says if tilesgroup (this group includes all visible tiles created at spawner positions) collides with tilesgroup to pick a random spawner tile and move to tilesgroup to the position of spawner.X(), spawner.Y(). This solves the problem of having multiple tilesgroup objects stacked on top of each other essentially hiding dozens of tiles.

The problem:

*When the amount of tiles increase, this creates a loop of tiles shuffling around endlessly up to 5 minutes before it stops (if it eventually does) due to the sheer amount of tiles being shuffled all at once.

My question: How would I go about reducing the time it takes to generate the tiles? The solution I am working on is to try to get the tiles to stop moving around once they have found a spawner tile that does not have another tilesgroup in that spot. I have not been successful so far.

Update: I have further discovered that it is an issue with an object tied to a group that when it collides with itself, I have it pick a random spawner and move there, but it constantly moves both at the same time. I can’t figure out how to select one of the objects (could be a stack of 5+) and move each one one at a time.

Hi,

Can you explain more how the tiles are added to the map? When the player adds tiles, should they be added on a grid in order (top-left → right-bottom), or are they added randomly in any empty tile on the grid, or are the spawners in specific places?

Maybe you can share a screenshot or a sketch of what you’re trying to do exactly to understand you better.

The tiles are automatically added to the map. The user just presses the Space key to delete all of the tiles and then automatically recreate them.

The image above is the map where the tiles are populating. There is an invisible spawner object at every hex on the grid.

In the image below, the different colors represent different objects added to the map. Each object is included in the tilesgroup object group. I have a condition that checks for the collision between two tilesgroup’s. The idea is when the two collide they will pick another hex (spawner object) at random and move there.

I also am trying to say when a tilesgroup collides with a spawner and is not colliding with another tilesgroup to set a variable that stops it from moving if another object collides with it.

The problem I am having is the collisions between two of the same objects. So, if two reds collide with each other, they seem to do two things:

  • Move to another random tile together, thus creating an infinite loop of the two objects moving around forever.
  • The objects stack on each other and essentially hide the proper amount of tiles that need to show.

I figured it out! Part of the issue was collision masks were too large. Guess I just needed to think through it some more.

Appreciate the help!

1 Like

This is what came to my mind and I was about to answer the same thing. Glad you figured it out on your own!

Also I think this can be done with a different approach that doesn’t involve collisions and multiple objects and is less performance demanding. But if the performance is good and the generation is quick after the fix, then there’s no need to change this method.

Good luck with your game!

1 Like