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

custom properties are not being saved to world files. #4025

Open
BertoGz opened this issue Aug 4, 2024 · 2 comments · May be fixed by #4055
Open

custom properties are not being saved to world files. #4025

BertoGz opened this issue Aug 4, 2024 · 2 comments · May be fixed by #4055
Labels
bug Broken behavior.

Comments

@BertoGz
Copy link

BertoGz commented Aug 4, 2024

I am attempting to write a custom property to a world file using asset.setProperty and asset.save() methods.
after doing so asset.property('propName') resolves with the correct value but it is ultimately not being written to the
actual .world file
i've attach a script to reproduce bug.

function findWorldOfMap(map) {
  return tiled.worlds.find((world) => {
    if (world.containsMap(map.fileName)) {
      return world;
    }
  });
}
function onAssetAboutToBeSaved(asset) {
  switch (asset.assetType) {
    case AssetType.TileMap:
      const world = findWorldOfMap(asset);
      if (world) {
        tiled.log(`world ${world.fileName}`);
        // Define the new property
        let newPropertyName = "foo";
        let newPropertyValue = "bar";
        world.setProperty(newPropertyName, newPropertyValue);
        world.save();
      }

      break;
    case AssetType.World:
      tiled.log(`saved world ${asset.assetType}`);
      const val = asset.property('foo')
      tiled.log(`val ${val}`);
      break;
  }
}

// Register an event handler
tiled.assetAboutToBeSaved.connect(onAssetAboutToBeSaved);

the script finds the associated world asset and then attempts to write a property/value to it.

My environment:
MacOS - 14.3.1
Tiled - 1.11.0

@BertoGz BertoGz added the bug Broken behavior. label Aug 4, 2024
@eishiya
Copy link
Contributor

eishiya commented Aug 4, 2024

Minimal reproducible example, via the console:
Set a property on the first (and hopefully only) loaded world:
tiled.worlds[0].setProperty("foo", "bar");
Verify the property exists on the world:
JSON.stringify(tiled.worlds[0].properties())
World >Save World >(the world)
World > Unload World > (the world)
World > Load World... (the same world)
Check for the properties again, and they won't be there:
JSON.stringify(tiled.worlds[0].properties())

I imagine this issue is due to Worlds having no way to view/edit custom properties via the GUI, so custom properties were overlooked in World::save() (in src/libtiled/world.cpp). But, since Worlds are Assets now, they do inherit that capability from Assets, and therefore probably should also write those properties out.

bjorn added a commit to bjorn/tiled-dev that referenced this issue Sep 11, 2024
Custom properties on worlds can only be accessed and modified through
the scripting API at the moment, but since this is possible we better
also save them.

Closes mapeditor#4025
@bjorn
Copy link
Member

bjorn commented Sep 11, 2024

Yeah, this was somewhat of an oversight when we decided to derive World from Object as part of #3859. I had even made a note of this in 08d39f9... Anyway, implemented in #4055 now. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Broken behavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants