Tiled: Save to json format : tilesets included as tsx

Created on 21 Sep 2015  Â·  20Comments  Â·  Source: mapeditor/tiled

Hi,

Just upgraded to latest stable release (from 0.13.0) and I noticed that the json format now include the path to .tsx file for tileset instead of image and properties.
this does not make sens because the user of json format is not supposed to parse an external XML file (.tsx) to be able to read the map correctly.

expected export

        {
         "firstgid":1,
         "image":"path/to/tileset.png",
         "imageheight":1024,
         "imagewidth":1024,
         "margin":0,
         "name":"tileset",
         "tilewidth":128,
         .....
        }, 

current export

        {
         "firstgid":1,
         "source":"path/to/tileset.tsx"
        }, 

Note. this only happen with exported tilesets

urgent

All 20 comments

Yeah, the problem is that Tiled can't suddenly change your external tileset to JSON as well. With Tiled 0.14, you have two options:

  • Import the tileset into the map, if you want it to be embedded.
  • Temporarily import the tileset into the map, and then export it again, choosing the JSON format when saving the tileset. Now you have an external tileset in jSON format.

The previous behavior, having an external tileset in XML but getting it embedded in the JSON format, is currently not an option. This is because I considered this bad behavior in the first place, and had only done this because there was no JSON format for external tilesets.

Would either of the above solutions work for you?

Actually I tried the first option (which I thought will resolve the issue) but still had the tilesets not embedded.
I think that once you export the tilesets, they are considered as exported even if you reimport them.

I think that once you export the tilesets, they are considered as exported even if you reimport them.

I just tried it and it's certainly working fine for me. Do you have multiple maps open at the same time? In that case, I think exporting the tileset in one map and then saving another may cause that other map to also write an external tileset reference rather than embedded one.

yes I'm editing multiple maps ...
once I finish my current work I'll try to isolate the issue and reproduce it in different scenarios.
I presently downgraded to v 0.13.0 to finish my project .

Is there any simple way to override the json export behaviour ? (without compiling an add-on ?)

Is there any simple way to override the json export behaviour ? (without compiling an add-on ?)

If you mean to use Tiled 0.14 with the JSON behavior of Tiled 0.13, then you'd need to compile Tiled yourself after removing the following block of code from MapToVariantConverter::toVariant:

    const QString &fileName = tileset->fileName();
    if (!fileName.isEmpty()) {
        QString source = mMapDir.relativeFilePath(fileName);
        tilesetVariant[QLatin1String("source")] = source;

        // Tileset is external, so no need to write any of the stuff below
        return tilesetVariant;
    }

In src/libtiled/maptovariantconverter.cpp.

I was actually looking for a way to create custom JSON exporter without modifying Tiled source code :)

Ah, well I think the easiest way is to modify Tiled's source code. It's not that hard to get it to compile (see https://github.com/bjorn/tiled/wiki/Contributing-to-Tiled).

Without compiling, your only option is to write a Python script that exports to JSON, but this will be a lot more work.

yeah I already compiled Tiled but this will lead to a fork and it'll be hard later to follow next updates.
It's not a good idea for long term usage.
I think I'll write a small script which will parse TMX format and export custom json format.
I'll also take a look at python interface as this can be a viable option.

I think I'll write a small script which will parse TMX format and export custom json format.

Why not have Tiled use JSON for both maps and tilesets in the first place? Then your small script would only have to create "baked" maps by embedding the tileset documents, if you really need them embedded. Alternatively, what would be the problem with supporting external tilesets in JSON format in your game?

yeah I already compiled Tiled but this will lead to a fork and it'll be hard later to follow next updates.
It's not a good idea for long term usage.

Right, this suggestion was meant as a temporary workaround, which I thought was what you were looking for. :-)

the reson I want to make such script is that my engine already use a combined format of maps (I combine multiple json maps in a single big json file).
this reduce the server requests when loading maps of an area.
the json parsing is also simpler when everything is embedded in the same file, otherwise you need to load the external tilesets dynamically and deal with asynchronous code .
it's not blocking, just make the code a little more complex in a place it can be very simple (flat JSON is directly usable as is)

I don't know if I should piggy back on this issue, but I came here with similar oddities. I'm using JSON and ideally I could do everything in JSON that I do with the default XML. I would love to create JSON tilemaps that have external tileset references. (I plan to make 100+ tilemaps and I add new tiles to the tileset over time!)

All the other in-betweens are confusing. Now that I've done a few experiments, there are a few things I understand and some suggestions to the design:

  • I'd suggest documenting the "import tileset" and "export tileset" commands. I'd include why they are disabled sometimes. It is a bit awkward that you have to edit your tileset in some map's "internal" tileset and then export it from that map. And that map will never be able to reference the external version. But at least documenting that would be nice. Like I said, I want to edit a tileset and reference it from 100s of maps. I only figured out the way it works by testing all morning after living with it for months. (I'd go to each JSON map's text file and edit it by hand!)
  • What about having the "export tileset" command ask what format to use? Then it is independent of the map format. Of course, if the tileset is internal, it uses the format of the map. (As an idea, the "export tileset" command could be switched to menu commands like "save tileset" and "save tileset as...")
  • For super bonus points, you wouldn't be blocked from editing an "external tileset" while editing a map that uses it. You would be asked to save changes separately.

I'd suggest documenting the "import tileset" and "export tileset" commands. I'd include why they are disabled sometimes. It is a bit awkward that you have to edit your tileset in some map's "internal" tileset and then export it from that map.

I agree it should have been documented, but at this point I think it's really not worth it anymore because I'm instead working on improving this workflow on the wip/tilesetdocument branch. There you can just open the external tileset files separately to edit them.

What about having the "export tileset" command ask what format to use?

It does exactly that, right? It does so since Tiled 0.14.

For super bonus points, you wouldn't be blocked from editing an "external tileset" while editing a map that uses it. You would be asked to save changes separately.

Yep, that too is part of the work on the wip/tilesetdocument branch. Though, to make changes to the tileset it currently will expect you to actually open that file separately (but there will be a convenient button for this below the tileset view). This is actually only necessary because it would otherwise be unclear to which file the undo and redo commands apply.

Sweet! wip/tilesetdocument sounds like it will be great!

What about having the "export tileset" command ask what format to use?

It does exactly that, right? It does so since Tiled 0.14.

Oh. Maybe so. I have 0.11 but I read the changelog up to 0.17 but did not see this so did not upgrade yet. I'm going to assume that JSON maps still force the tilesets to be internalized? Unless I hear differently. My ultimate goal is to read the maps and tilesets separately and in JSON. Otherwise, I might move all my parsing to XML. Icky. ;)

I'm going to assume that JSON maps still force the tilesets to be internalized? Unless I hear differently.

No, that is what changed in Tiled 0.14 (and what caused this issue to be opened):

  • Added support for custom external tileset formats (JSON format added)

I'm going to assume that JSON maps still force the tilesets to be internalized? Unless I hear differently.

No, that is what changed in Tiled 0.14 (and what caused this issue to be opened):

Nice! Downloaded it and experimented. It's working!

(This bug made me think it was XML tilesets only.)

It's a neat new feature how, if you export a tileset from a map, the map changes to using that tileset externally. This is another one of those features that will take a while to figure out without docs, but you have the new branch that will change it all anyway.

In the meantime, I see my workflow should be: if I want a tileset change, "import tileset" to my current map just long enough to change it, then "export tileset" from that map. This will make the tileset external again and, voila, the tileset is changed for all maps that reference it.

Thanks!

The wip/tilesetdocument branch was now merged to master, which should improve the workflow with external tilesets considerably. The snapshot builds are already updated. If anybody here is interested in checking it out, your feedback is appreciated!

Still, if you save the map as JSON and the external tileset you saved was using the TSX format, the JSON map will contain a reference to the TSX file. I don't really see a way around this, apart from adding some option to embed the external tilesets when exporting. Would that be useful?

That would be extremely helpful! 😃
It'd mean I'd be able to work with external tilesets but export to a json format that's compatible with the engine I use(Phaser).

I think it is good idea to have tsx files containing tilesets so it can be shared between maps easily. However i consider the export to json as sort of “compilation output“ i dont want to request two different json files from server. It makes things complicated as i have to wait for two different async calls to complete. I think an option to chose if the tileset should be embedded into json map export would be nice :)

New options added in fe13589e7eaa8d5564df5397ffa5755d106cc47c, which are now also available on the command-line (72789ab0e1a42c87f196f027f2cb6169675f5e48).

selection_259

With that I consider this issue closed, just note that the options only apply to _Export_ actions, not to the regular _Save_ action.

Was this page helpful?
5 / 5 - 1 ratings

Related issues

IcemarkUK picture IcemarkUK  Â·  28Comments

nobody-special666 picture nobody-special666  Â·  15Comments

bjorn picture bjorn  Â·  23Comments

bjorn picture bjorn  Â·  16Comments

iJ0N45 picture iJ0N45  Â·  42Comments