Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.tmx file - read, modify at runtime and save it back ? #1341

Open
JavaGitHubber opened this issue Feb 12, 2024 · 3 comments
Open

.tmx file - read, modify at runtime and save it back ? #1341

JavaGitHubber opened this issue Feb 12, 2024 · 3 comments
Labels
type:new feature Adding a feature that isn't currently present

Comments

@JavaGitHubber
Copy link

JavaGitHubber commented Feb 12, 2024

Hi Almas, hi everybody,

my son had an idea for a 2D game, a kind like RPG, where we can create a world scenario (~1600x900px) by using Tiled editor, tiled layers and tiled maps. So far so good.

Now the idea is to modify the scenario during the game, not only some pixel or small areas which could be done by objects and object layers, but in wide areas, e.g. an animated change of the basic ground floor with 10.000s of pixels on the tiled layer and, save it back to the tmx-file (or another format to load it back later again).

Game situation:
Players can occupy and take over the opponent's lands; this should be indicated in the first step by changing the color of the occupied tiled elements. The development of the lands is represented by other surfaces, hurricans might destroy land and turn into ocean, etc.

How can I access directly to the Tiled Layer and its corresponding Tile Set?

Instead I first tried to create 1600 x 900 single objects to spawn each of them individually, but this is in my opinion not to handle, isn't it?

In other words,

  • I want to load a world which is generated by using Tiled editor and render it to the gameworld (this can be easily done right now)
  • Modify the tiled layers (modify the corresponding tilemap element and modify colors and tilemap image at runtime) and draw it back to screen
  • Save this modification back to disk into a new tmx file

Is this too crazy or to complicated? Or do I missed a simple way to achieve this right now?

Thanks so much.
clas

@JavaGitHubber JavaGitHubber added the type:new feature Adding a feature that isn't currently present label Feb 12, 2024
@AlmasB
Copy link
Owner

AlmasB commented Feb 14, 2024

Hi @JavaGitHubber

This sounds like more of a serialization question, rather than an asset loading one.

Once a .tmx is loaded as entities (game objects), each entity possesses a set of default properties, for example the player to which the tile belongs, the unit placed on that tile, etc. As the game progresses these properties get modified. At the end of the game, these properties can be saved to disk and reloaded as needed.

The algorithm overview might look like this:

  1. load the game level from .tmx
  2. generate entities with default properties
  3. overwrite properties from a game save file if present
  4. save properties to a game save file on exit

Hope this makes sense

@JavaGitHubber
Copy link
Author

Hi Almas, hi everybody,

thank you for your answer and suggestions.

Yes I agree fully with yout aproach and I am familliar with handling and creating entities, assigning entities to objects that has been placed on the object layers in Tiled and handling and expanding their properties.

What I now want or beeter need to do is to manipulate the pixels or their tile set that is on the 'tilemap Layer'.

Do I have access directly to the Tiled Layer pixels and its corresponding Tile Set?

Thanks so much.
clas

@AlmasB
Copy link
Owner

AlmasB commented Feb 19, 2024

Hi, when a .tmx is loaded, it provides a Level object, which stores entities. Each tiled layer that is not an object layer will have an entity associated with it. These entities have their type set to "TiledMapLayer", so you should be able to grab them once loaded.

Their ViewComponent should have only 1 object, which is ImageView, whose image stores your pixels. ImagesKt class gives you the ability to manipulate pixels of any image as needed.

I don't think the tile set information is stored anywhere at runtime after parsing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:new feature Adding a feature that isn't currently present
Projects
None yet
Development

No branches or pull requests

2 participants