-- examples/tilemap.luau: read a Tiled map (TMX), walk its tile layer with the flip flags, find an object, edit the map -- and convert it to TMJ. Run from the repository root: luau examples/tilemap.luau (docs/guide/tilemap.md). local F = require("@rosetta") local tmx = [[ 1,2,2,2,3, 5,0,0,0,7, 9,10,2147483658,10,11 ]] local map = assert(F.load(tmx)) print(string.format("%dx%d tiles of %dx%d px, %d layers", map.width, map.height, map.tileWidth, map.tileHeight, #map.layers)) local FLIP_H, FLAGS = 0x80000000, 0xF0000000 local ground = map.layers[1] for y = 0, ground.height - 1 do local row = {} for x = 0, ground.width - 1 do local raw = ground.data[1 + x + y * ground.width] local gid = bit32.band(raw, bit32.bnot(FLAGS)) row[#row + 1] = if gid == 0 then " ." elseif bit32.btest(raw, FLIP_H) then string.format("%2dh", gid) else string.format("%2d", gid) end print(table.concat(row, " ")) end local door for _, o in map.layers[2].objects do if o.class == "Door" then door = o end end assert(door and door.properties.locked == true) print(string.format("door at tile (%d, %d), locked", door.x // map.tileWidth, door.y // map.tileHeight)) -- edit: fill the hole in the middle row, then write TMJ and read it back for x = 1, 3 do ground.data[1 + x + 1 * ground.width] = 6 end local tmj = assert(F.saveString(map, { format = "tmj" })) local back = assert(F.load(tmj, { format = "tiled" })) assert(back.layers[1].data[8] == 6 and back.layers[1].data[13] == 0x8000000A) assert(back.layers[2].objects[1].properties.locked == true) print("as TMJ: " .. #tmj .. " characters; tileset image " .. back.tilesets[1].imageSource)