mapeditor/tiled

Do you want to work on this issue?

You can request for a bounty in order to promote it!

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

BertoGz posted onGitHub

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


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.

posted by eishiya 9 months ago

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 08d39f9d962fd99f976170d632ab957504886dc9... Anyway, implemented in #4055 now. :)

posted by bjorn 8 months ago

Fund this Issue

$0.00
Funded
Only logged in users can fund an issue

Pull requests