Page 1 of 1

Tiled Exports Lua files

Posted: Tue Jan 29, 2013 5:34 am
by MarekkPie
During the most recent Global Game Jam, a teammate of mine noticed that if you do the "Export As" option from the File menu in Tiled, that you can export your maps directly as Lua files. I hadn't even seen this before, nor have I heard about it on the forums, so I just thought I might as well let everyone know. Here is a sample (pasted directly from the file, so no nice formatting):

Code: Select all

return { version = "1.1", luaversion = "5.1", orientation = "orthogonal", width = 40, height = 11, tilewidth = 16, tileheight = 16, properties = {}, tilesets = { { name = "MarioNES", firstgid = 1, tilewidth = 16, tileheight = 16, spacing = 0, margin = 0, image = "../CPP/testTiled/mariones.png", imagewidth = 304, imageheight = 192, properties = {}, tiles = {} } }, layers = { { type = "tilelayer", name = "Background", x = 0, y = 0, width = 40, height = 11, visible = true, opacity = 1, properties = {}, encoding = "lua", data = { 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 39, 40, 41, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 39, 40, 41, 44, 44, 44, 44, 44, 44, 44, 44, 58, 59, 60, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 39, 40, 41, 44, 44, 44, 44, 44, 44, 44, 44, 58, 59, 60, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 58, 59, 60, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 27, 44, 44, 44, 44, 44, 44, 44, 44, 44, 27, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 27, 44, 44, 44, 44, 44, 44, 44, 45, 46, 47, 48, 44, 44, 44, 44, 44, 44, 45, 46, 47, 48, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 46, 47, 48, 44, 44, 44, 44, 63, 64, 65, 66, 67, 44, 20, 21, 22, 44, 63, 64, 65, 66, 67, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 20, 21, 22, 44, 63, 64, 65, 66, 67, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44 } }, { type = "tilelayer", name = "Foreground", x = 0, y = 0, width = 40, height = 11, visible = true, opacity = 1, properties = {}, encoding = "lua", data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 } } } }
It doesn't seem to output in anything other than CSV (unlike normal .tmx files which can output using Base64 encoding and zlib/gzip compression), but beyond that, this seems like a nice alternative to often bulky XML parsers.

Re: Tiled Exports Lua files

Posted: Tue Jan 29, 2013 6:50 am
by szensk
That's very cool. Just gzip after save and gzip on load and you're good to go.

Re: Tiled Exports Lua files

Posted: Tue Jan 29, 2013 8:09 am
by Inny
I was the one who wrote the java version of the lua export plugin. The format I picked was a simplified version of what Roberto Ierusalimschy's XML parser was outputting: the xargs subtables were removed, and the the "layers" and "tilesets" elements were put in separate subtables. Keep those mind if you want to switch between the tmx and lua formats. As for why I went with CSV, it was the easiest way to go at the time. I'm not sure why Thorbjorn didn't expand upon the plugin, unless hes been waiting all this time for me to do it.

Re: Tiled Exports Lua files

Posted: Tue Jan 29, 2013 7:24 pm
by MarekkPie
I made an issue on the encoding and compression problem, and it looks like he just rips directly from GidMapper rather than run it through the same processes that the TMX exporter uses.

Code: Select all

void LuaPlugin::writeTileLayer(LuaTableWriter &writer,
                               const TileLayer *tileLayer)
{
    writer.writeStartTable();

    writer.writeKeyAndValue("type", "tilelayer");
    writer.writeKeyAndValue("name", tileLayer->name());
    writer.writeKeyAndValue("x", tileLayer->x());
    writer.writeKeyAndValue("y", tileLayer->y());
    writer.writeKeyAndValue("width", tileLayer->width());
    writer.writeKeyAndValue("height", tileLayer->height());
    writer.writeKeyAndValue("visible", tileLayer->isVisible());
    writer.writeKeyAndValue("opacity", tileLayer->opacity());
    writeProperties(writer, tileLayer->properties());

    writer.writeKeyAndValue("encoding", "lua");
    writer.writeStartTable("data");
    for (int y = 0; y < tileLayer->height(); ++y) {
        if (y > 0)
            writer.prepareNewLine();

        for (int x = 0; x < tileLayer->width(); ++x)
            writer.writeValue(mGidMapper.cellToGid(tileLayer->cellAt(x, y)));
    }
    writer.writeEndTable();

    writer.writeEndTable();
}