Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A PyxelEdit Tilemap Importer For GodotScript
tool
extends EditorScript
const TILEMAP_JSON_PATH = "res://Data/Maps/GraveyardTilemap.json" # path to PyxelEdit exported json file
const TILESET_PATH = "res://Tiles/GraveyardTileset.tres" # path to Godot Tileset Object
func load_json(filepath):
var file = File.new()
file.open(filepath, file.READ)
var file_text = file.get_as_text()
var result_json = JSON.parse(file_text)
file.close()
if result_json.error == OK:
return result_json.result
else:
print("Error reading JSON at " + filepath)
return {}
func draw_tilemap_from_json(json):
var tileset = load(TILESET_PATH)
var mapMeta = {
'num_layers': len(json.layers),
'cell_size': Vector2(json.tilewidth, json.tileheight),
'map_size': Vector2(json.tileswide, json.tileshigh)
}
print("Loading Tilemap: ")
print(mapMeta)
var maps = []
var tilemapsParent = Node2D.new()
tilemapsParent.set_name("PyxelEditTilemaps")
get_scene().add_child(tilemapsParent)
tilemapsParent.set_owner(get_scene())
for l in json.layers:
var tm = TileMap.new() # Instantiate tilemap node
tm.tile_set = tileset
tm.set_name(l.name) # name node after PyxelEdit Layer
tm.cell_size = mapMeta.cell_size
for pos in l.tiles:
tm.set_cell(pos.x, pos.y, pos.tile)
maps.append(tm)
while len(maps) > 0:
var tm = maps.pop_back()
tilemapsParent.add_child(tm)
tm.set_owner(get_scene())
# Called when the node enters the scene tree for the first time.
func _run():
var json = load_json(TILEMAP_JSON_PATH)
draw_tilemap_from_json(json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.