How to make random generation of trees?

you would need to move the logic out of _ready to a function that isn’t called immediately upon entering the scene. you would then need to call it after the generation is complete.

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 trees() -> 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(0,tile,0,tree)

func _input(event) -> void:
     if event.is_action_pressed("ui_select"):
           trees()

something like this might work just press spacebar after you have generated the map. the indentation might be slightly off as i use C# and just wrote this GDScript here in this post so using TAB to indent did not work correctly

and again an error

thats what i was talking about you need to remove the spaces i entered and replace them with tabs. back the if statement up to line up with the function start and then hit tab. then back trees() up to line up with function start and hit tab twice

I don’t really understand how to do this. Do I need to launch the scene and then press space? If so, then everything hangs.

In general, I’m thinking of rewriting the generation from the tutorials, at the same time with the trees. Thank you all for your help and response