Can any one help me with this procedural generation?

Godot Version

3

I have this code

extends Node2D


onready var tilemap_ground = $Ground
onready var tilemap_grass = $Grass
onready var tilemap_trees = $Tree

var grid_width = 100
var grid_height = 100


var debug_frequency = 20  


var grass_count = 0
var tree_count = 0

func _ready():
	
	if tilemap_ground == null:
		push_error("TileMap 'Ground' is not found!")
		return
	if tilemap_grass == null:
		push_error("TileMap 'Grass' is not found!")
		return
	if tilemap_trees == null:
		push_error("TileMap 'Tree' is not found!")
		return
	
	print("Map generation started...")
	generate_map()
	print("Map generation completed!")
	print("Total grass tiles placed: ", grass_count)
	print("Total tree tiles placed: ", tree_count)

func generate_map():
	for x in range(grid_width):
		for y in range(grid_height):
			place_ground_tile(x, y)
			if (x + y) % debug_frequency == 0:
				print("Progress: (", x, ", ", y, ")")

			place_grass_tile(x, y)
			place_tree_tile(x, y)

func place_ground_tile(x, y):
	var ground_tile_id = 0 
	if tilemap_ground != null:
		tilemap_ground.set_cell(x, y, ground_tile_id)
		if (x + y) % debug_frequency == 0:
			print("Placed ground tile at position: (", x, ", ", y, ") with ID: ", ground_tile_id)
	else:
		push_error("TileMap 'Ground' is null!")

func place_grass_tile(x, y):
	var grass_tile_id = 1
	if randi() % 100 < 30:  
		if tilemap_grass != null:
			tilemap_grass.set_cell(x, y, grass_tile_id)
			grass_count += 1
			if (x + y) % debug_frequency == 0:
				print("Placed grass tile at position: (", x, ", ", y, ") with ID: ", grass_tile_id)
		else:
			push_error("TileMap 'Grass' is null!")

func place_tree_tile(x, y):
	var tree_tile_id = 2  
	if randi() % 100 < 10:  
		if tilemap_trees != null:
			tilemap_trees.set_cell(x, y, tree_tile_id)
			tree_count += 1
			if (x + y) % debug_frequency == 0:
				print("Placed tree tile at position: (", x, ", ", y, ") with ID: ", tree_tile_id)
		else:
			push_error("TileMap 'Tree' is null!")


And i cant understand where to look for Id of tiles

The IDs are the tile index in the tilemap; you can see this by selecting the tile in the editor. Is that your problem?

Bruh i cant still see that, you mean that z index maybe ?And the problem is that i can genarate only ground tiles the tree and grass there is nothing (

The tile index in the TileSet. Perhaps you already have this correct but the ordering of the layers here means the Ground-node is drawn after the other layers (so occludes them). Try putting the Tree and Grass layers after the Ground layer.

Indexes are shown in the TileSet editor too:

image

Here, 0 and 1 for the tiles in this example.

Ok now i understand, hope it will work, i will try soon.

1 Like

Thank you very much it worked, i appreciate it ! :+1: :+1: :+1: :+1:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.