How do I spawn my player in a procedural world?

Godot Version



I’ve made a world that is procedurally generated with the Gaea addon and I’m using a noise generator and tilemap renderer to spawn the tiles. However, my player keeps spawning in the water tiles, which has collision so my player can’t move, and I instead want them to spawn on the grass tiles I have.

How do I spawn the player randomly positioned and yet, specifically on the grass, and never in the water?

A quick read of the Gaea plugin doesn’t show me that there’s an easy way to “get all tiles of a certain type” so you can pick one at random for your player. As a result, you have a couple of options.

First, you could modify the Gaea plugin to simply add any grass tiles’ location (a Vector2i) to an Array for you, and then pick a location randomly from that Array.

If you’re less comfortable with that, then you can simply pick a point at random on the map and see if it’s a grass tile. If it is, then spawn the player. If not, then try again. This is faster to implement, but it’s generally a bad idea because - in theory - you could have a player sitting around forever and never finding a grass tile. Especially true if your world generates with only a tiny amount of grass.

The approach I would take would be similar to the first, but without editing the plugin. I would simply loop through every tile in the TileMap, adding the location to an Array if its a grass tile, and finally pick from that Array at random. This has the benefit of not modifying the plugin, while getting you the same result. A simple check of the tile data shouldn’t be too slow, and even if the map is exceptionally large and it takes a while, you can always hide it from the player with a “Generating Map…” loading screen and just have it part of the process.

Does that make sense or do you need more concrete examples?

I was thinking of doing the solution of finding tiles and if its a grass tile I spawn there but I don’t know how to code that in. I’ve been searching everywhere and none if it makes sense. I just want to load the scene, check tile 1 for whether it grass or water, if its water continue checking the rest of the tiles until you get grass, then spawn the player there.