I want to be able to export it to a text file in the same layout with the numbers and each tile saved individually.
I.E.
map.txt
3,3,3,3,3,3,3,3,3,3,3,3,1,1,2,2
3,3,3,1,1,1,1,1,1,1,3,3,3,1,2,2
… etc
then ‘3’ be like ‘water.png’, ‘1’ be ‘dirt.png’, and ‘2’ be ‘grass.png’
This may be silly, but I only ask because Pyxel does the text file thing and I’m following a SDL tutorial that uses that. I understand this likely isn’t super practical or anything, I just don’t want to purchase Pyxel and want to know if I don’t have to create a text file manually… Thank you!
I don’t know what SDL is or if you have background in coding, but Aseprite supports scripting plugins with Lua, so that would allow you to customize your export.
The scripting reference is here. Not all tile map features are documented, so for more advanced work, you have to look at Aseprite’s source code. The docs for Lua are here in case you want to learn more about file i/o.
Below is a simplified example of how you could export a tile map image to a .txt file.
local filepath = "replace_this_with_your_path"
local activeSprite = app.activeSprite
if not activeSprite then return end
local activeImage = app.activeImage
if not activeImage then return end
-- Color mode is 4 when image is a tile map
-- in case you want to verify.
-- local colorMode = activeImage.colorMode
local rowStrings = {}
local imgWidth = activeImage.width
local imgHeight = activeImage.height
for y = 0, imgHeight - 1, 1 do
local rowSample = Rectangle(0, y, imgWidth, 1)
local pixelIterator = activeImage:pixels(rowSample)
local rowPixels = {}
for pixel in pixelIterator do
local index = pixel()
local x = pixel.x
rowPixels[1 + x] = string.format("%d", index)
end
local rowString = table.concat(rowPixels, ",")
rowStrings[1 + y] = rowString
end
local totalString = table.concat(rowStrings, "\n")
local file, err = io.open(filepath, "w")
if file then
file:write(totalString)
file:close()
end
if err then
app.alert { title = "Error", text = err }
return
end
The file path string should be replaced. You might have to change the line ending from \n to whatever works for your setup.
-- Script for Aseprite to export a Tilemap to a 2D-Array of Tileset-Numbers
-- Please don't hate on me that was the first lua-Script I have ever written in my whole life I really hate interpreter languages
-- License: Bro just use it
if TilesetMode == nil then return app.alert "Use Aseprite 1.3" end
local spr = app.activeSprite
if not spr then return end
local d = Dialog("Export Tilemap as .txt File")
d:label{id="lab1",label="",text="Export Tilemap as .txt File for your own GameEngine"}
:file{id = "path", label="Export Path", filename="",open=false,filetypes={"txt"}, save=true, focus=true}
:number{id="vbegin",label="Index of first Tileset (Default 1): ", text="1",focus=true}
:number{id="Size",label="Tiles per Row (Layer-Width / Tile-Width): ",text="16"}
:entry{id="seperator",label="Seperator",text=";"}
:separator{}
:label{id="lab2", label="",text="In the last row of the tilemap-layer there has to be at least one Tile \"colored\" to fully export the whole Tilemap"}
:button{id="ok",text="&OK",focus=true}
:button{text="&Cancel" }
:show()
local data = d.data
if not data.ok then return end
local lay = app.activeLayer
if(#data.path<=0)then app.alert("No path selected") end
if not lay.isTilemap then return app.alert("Layer is not tilemap") end
pc = app.pixelColor
mapFile = io.open(data.path,"w")
for _,c in ipairs(lay.cels) do
local img = c.image
local i = 0
for p in img:pixels() do
if(p ~= nil) then
i=i+1
if(data.vbegin==1) then
mapFile:write(pc.tileI(p()))
if(i+1<=data.Size and #data.seperator > 0)then mapFile:write(data.seperator) end
else
mapFile:write(pc.tileI(p()+data.vbegin-1))
if(i+1<=data.Size and #data.seperator > 0)then mapFile:write(data.seperator) end
end
if(i==data.Size) then
mapFile:write("\n")
i=0
end
end
end
end
mapFile:close()
Thank you for such a quick reply!
I ended up using a different solution from someone else following the same tutorial. I posted their script as a reply.
I’m novice/intermediate with C++ and have zero experience with Lua other than the minimal Lua scripting I used to set up Neovim.