GDScript: "paint" a street TileMapLayer

Godot Version

4.3 stable

Question

Hey guys!

New to godot :slight_smile: - my first steps in a basic 2D game to learn.

Got a Node2D, and 2 TileMapLayer child nodes: “Ground” and “Street”.

Added a script to “Street” to somehow later randomly paint streets on a map. But first i have to find out how to “paint” at all using code.

My setup is 1280x720 and tiles are 64x64.

extends TileMapLayer

@export var grid_width: int = 20
@export var grid_height: int = 12

static var TILE_TYPE_STRAIGHT_HORIZONTAL: Vector2 = Vector2(1, 0)
static var TILE_TYPE_STRAIGHT_VERTICAL: Vector2 = Vector2(2, 1)
static var TILE_TYPE_CURVE_TOP_LEFT: Vector2 = Vector2(0, 0)
static var TILE_TYPE_CURVE_TOP_RIGHT: Vector2 = Vector2(2, 0)
static var TILE_TYPE_CURVE_BOTTOM_LEFT: Vector2 = Vector2(0, 2)
static var TILE_TYPE_CURVE_BOTTOM_RIGHT: Vector2 = Vector2(2, 2)

var random: RandomNumberGenerator = RandomNumberGenerator.new()

func _ready():
	generate_random_path()

func generate_random_path():
	var random_y: int = random.randi() % grid_height
	var start_point: Vector2i = Vector2i(-64, random_y * 64)
	
	while start_point.x < (1280 + 64):
		print("start_point: ", start_point)
		set_cell(start_point, -1, TILE_TYPE_STRAIGHT_HORIZONTAL, -1)
		start_point = Vector2i(start_point.x + 64, start_point.y)
		

The coded runs without issues but wont “paint” any tiles. Consol output is:

start_point: (-64, 320)
start_point: (0, 320)
start_point: (64, 320)
start_point: (128, 320)
start_point: (192, 320)
start_point: (256, 320)
start_point: (320, 320)
start_point: (384, 320)
start_point: (448, 320)
start_point: (512, 320)
start_point: (576, 320)
start_point: (640, 320)
start_point: (704, 320)
start_point: (768, 320)
start_point: (832, 320)
start_point: (896, 320)
start_point: (960, 320)
start_point: (1024, 320)
start_point: (1088, 320)
start_point: (1152, 320)
start_point: (1216, 320)
start_point: (1280, 320)

So the coordinates should be ok. I try to start 64 pixel outside the visual map and end 64 pixel outside as well. Just a strait line from left to right from now but with a random start point at the left side.

My TileMapLayer is a 192x192 png, not using the center (is used at Ground). Also added (pinted) a navigation layer at some point, but my code won’t work with or without it.

Can anyone help? :slight_smile:

Thanks!

I’ll try this

extends TileMapLayer

@export var grid_width: int = 20
@export var grid_height: int = 12
@export var tile_set_source_id: int = 0  # The ID of the tile source in your TileSet

static var TILE_TYPE_STRAIGHT_HORIZONTAL: Vector2i = Vector2i(1, 0)

var random: RandomNumberGenerator = RandomNumberGenerator.new()

func _ready():
    generate_random_path()

func generate_random_path():
    var random_y: int = random.randi() % grid_height
    var start_point: Vector2i = Vector2i(-1, random_y)  # Using TileMap grid coordinates
    
    while start_point.x < grid_width + 1:
        print("start_point: ", start_point)
        
        # Corrected set_cell usage
        set_cell(start_point.x, start_point.y, tile_set_source_id, TILE_TYPE_STRAIGHT_HORIZONTAL)
        
        start_point = Vector2i(start_point.x + 1, start_point.y)
1 Like

Thanks!

Found my error. I had to use local_to_map(start_point) in set_cell. At least it works this way.

Next task: random streets :smiley:

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