Godot Version
4.3 stable
Question
Hey guys!
New to godot - 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?
Thanks!