How to make random generation of trees?

I need to randomly generate trees on a specific texture from a TileMap in a procedurally generated world.

1 Like

You can do this using y sort, therefore you can learn the concept from this video

I use this Gaea godot. Is it possible to somehow do what I want using the tools built into this addon?

One possible solution of many would be to add a random chance for any grass tile to become a tree and then you can just add something like this pseudocode

if randf() < 0.25f: //twenty-five percent chance
      setcell(tree)
else
      setcell(grass)

How? And what node should I tie it to? I’m new and don’t really understand

if you provide your code i can fix it for you

I use Godot Gaea. Unfortunately, I can’t post the link, but it’s on the Git Hub. And I use a noise generator

Unfortunately that is the drawback of not generating your own resources. It will be difficult to assist you with anything that your code is not directly responsible for.

It’s a pity

This is the best i can do. attach this to your tilemap or tilemap layer depending on your version of godot and adjust the variables to match your tileset

extends TileMapLayer #You might need to extend TileMap instead if you are using an older version of Godot

var grass = Vector2i(0,0) #You would need to change these to whatver your atlas coordinate is
var tree = Vector2i(0,1) #You would need to change these to whatver your atlas coordinate is

func _ready() -> void:
	
	var grass_tiles = get_used_cells_by_id(0,grass)
	for tile in grass_tiles:
		if randf() < 0.25: #You can change the 0.25 to any float between 0 and 1 to increase or decrease the chance of a grass tile becoming a tree
			set_cell(tile,0,tree)

I have Godot 4 versions. What should be configured in variables depending on the version? And thank you very much for your time and help, I appreciate it

Godot 4.3 moves to TileMap Layers instead of Tilemaps so most likely you are still using tilemap so yours should start with “extends TileMap” instead of “extends TileMapLayer”. as for the variables without seeing your tileset its impossible to know how yours is configured but when you hover over the tile in the tileset it should give you a source id(usually 0) and an atlas coordinate. quite possibly yours is the first tile is Vector2(0,0) and the second one is Vector2(1,0) and add 1 to the x value for each subsequent tile in your tileset

This error appeared in the last line

As of your code, the middle parameter of set_cell is wrong, it will be set_cell(tile, Vector2i(x, y), tree)

nothing has changed, still the same error

Ahh i see now. You dont use a layer when using the new tilemap layers so i forgot to include that

set_cell(0,tile,0,tree)

There are no errors, but trees are not generated. Maybe I did something wrong or maybe I attached the script in the wrong place?

I would imagine it needs to run after the Gaea generation or else there won’t be any tiles to get using get used cells by id

I click on the “generate” button. Do I need to do anything else?