Godot Version
4.6
Question
Well hello fellow godot people, I’m building a game about transporting goods and I’m working on a 2d map system with tilemaps. The map will feature generated city’s, roads, … right now I’m working on the city part.
I wanted it to be pretty easy for mod implementation so I went with the jigsaw methode Minecraft uses. For these who don’t know how it works: You have different village pieces with sockets that have certain data for choosing which other village piece should connect to it.
So I wanted to create this in godot, I’m using tilemap patterns and layers to add all the metadata of the sockets into a resource (this gets done before the game is made in to a .exe file for the moment). Then I put all these resources in an array and load them in.
Resources:
extends Resource
class_name Village_piece
@export var tilemap: TileMapPattern
@export var sockets: Array\[Socket\]
@export var origin: Vector2i
extends Resource
class_name Socket
@export var position: Vector2i
@export var direction: Vector2i
@export var socket_type: int
func load():
houses.clear()
roads.clear()
tile_map_1.clear()
var houses_packed: PackedStringArray = DirAccess.get_files_at("res://Village_pieces/resources/house/")
house_size = houses_packed.size()
for file in houses_packed:
houses.append(load("res://Village_pieces/resources/house/" + file))
var road_packed: PackedStringArray = DirAccess.get_files_at("res://Village_pieces/resources/road/")
road_size = road_packed.size()
for file in road_packed:
roads.append(load("res://Village_pieces/resources/road/" + file))
place_road()
Them I place a road:
func place_road():
var random_road = roads[randi() % road_size]
var vector = Vector2i(6,0)
tile_map_1.set_pattern(vector, random_road.tilemap)
var rsocket = random_road.sockets[randi() % random_road.sockets.size()]
var temp_bool = false
while temp_bool == false:
if rsocket.socket_type == 2:
temp_bool = true
place_building(rsocket, vector, random_road.origin)
else:
rsocket = random_road.sockets[randi() % random_road.sockets.size()]
Them the annoying part comes placing a building
func place_building(socket, adj_vector, origin):
var pos = socket.position - origin + adj_vector
var rnumber = randi() % house_size
var rhouse = houses[rnumber]
var dif = rhouse.origin - rhouse.sockets[rnumber].position
## Rotation
if socket.direction == - rhouse.sockets[rnumber].direction:
tile_map_1.set_pattern(pos+ dif + rhouse.sockets[rnumber].direction , rhouse.tilemap)
else:
Console.print_line("bad rotation")
rhouse.sockets[rnumber].direction = -rhouse.sockets[rnumber].direction
rhouse.origin = Vector2i(1,1) - rhouse.sockets[rnumber].direction + rhouse
dif = rhouse.origin - rhouse.sockets[rnumber].position
tile_map_1.set_pattern(pos+ dif + rhouse.sockets[rnumber].direction , rhouse.tilemap)
tile_map_1.set_cell(pos ,0, Vector2i(8,8))
tile_map_1.set_cell(pos+ dif + rhouse.sockets[rnumber].direction ,0, Vector2i(9,9))
See that is where I’m lost right now, I check if the dir = -dir if not then I need to rotate the house.
I’ve tried a lot, but this is what I learned:
- The origin is where the pattern start building
Any way of helping me understand how to rotate a pattern would help, I’ve been coding for days on this and haven’t found a solution.
Images:
If you don’t understand something you can always ask!

