SSBMTonberry / tileson

A modern and helpful cross-platform json-parser for C++, used for parsing Tiled maps.
BSD 2-Clause "Simplified" License
193 stars 30 forks source link

Possible to modify tile data in place? #121

Closed cookie99999 closed 8 months ago

cookie99999 commented 8 months ago

Hello, hopefully this is an appropriate place to ask for some guidance with the library's usage. Basically, I was wondering if it was possible in tileson to change the tile at (x, y) to a different tile. For instance, say I have a "door" object that can be opened and closed. Using the Animation object, I have a frame for a closed door, and a frame for an open door. In tileson I can change the animation frame, but I do not know how to change the underlying tile, there is no "Tile::setGid()" or anything like that that I can see. I know I could do this if I wrote my own classes to move the data to after parsing, but your classes already have 99% of the functionality I need and are nice to work with, so I was looking first for a solution in tileson itself. Thanks in advance for any advice.

twje commented 8 months ago

Hi Cookie999999. I remember from submitting a PR that the API is designed to be read-only. However, there's a workaround to update the map during gameplay. You could use tson::Layer::getTileData() to access the necessary tile data.

To modify this data, you might need to remove the const qualifier (essentially casting away the const). Then, you can obtain the specific tile value you want to assign from Map::m_tileMap. This approach should allow you to make the desired updates to the map.

There may be some edge cases to consider, things like the tile set type and if the tile is flipped. Hopefully this gives you a starting point.

SSBMTonberry commented 8 months ago

@cookie99999 : Any questions are welcome here, so it's absolutely appropriate to post an issue here on that regard :smile:

As @twje points out the API is designed to be read-only, or rather: To present what's contained in a Tiled map so it can be used to generate your own objects that are fit for your purpose. For animations, for example, you would be better off having your own animation objects that are able to do the job using the data presented in Tileson. While the idea is that the API should be read-only, there are some parts that are not, but there is an issue about looking into them to make them follow const correctness (#118 ). Please keep in mind that Tileson does a lot of extra work under the hood to make it easier to resolve and use Tiled-data. Which means any modification of commonly used properties like Tiles may have a side-effect, so I would think twice before going on a route that involces casting away the const qualifiers :slightly_smiling_face:

Like most parsers: Tileson is intended to read the data and make them available to the user, in the most user friendly way possible. Since the parsing itself is done using json objects, no setters on each property is needed for the purpose of the library, but some parsers may have setters due to not having this alternative. This does not necessarily mean it's intended for anything else than setting the data initially.

While this may not be the answer you were hoping for: You are free to re-use any of the data from the Tileson classes for your internal classes as you see fit.

cookie99999 commented 8 months ago

Thank you for the clarification! I had suspected that that was the case, just wanted to make sure. I'll avoid const casting and just use the data for my own objects.