mapeditor/tiled

Do you want to work on this issue?

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

tiled.activeAsset returns null when providing a tmx file as argument #3530

AnotherPillow posted onGitHub

Describe the bug When running a .js script from the CLI with --evaluate and a path to a .tmx file as arguments, values like tiled.activeAsset (and others that should return a map object).

To Reproduce Steps to reproduce the behavior:

  1. Create a script referencing tiled.activeAsset with a console.log() (or something similar).
  2. Run it, when I was making my script I used "C:\Program Files\Tiled\Tiled.exe" --evaluate formatConv.js ../CP/assets/gv_diner_interior.tmx in the folder I had the script.
  3. Will return qml: null or similar.

Expected behavior Would return something that isn't null.

Media image

Specifications:

  • OS: Windows 10
  • Tiled Version: 1.92

I did ask about this in the Discord server, and nobody seemed to know and bjorn suggested I make an issue here.


tiled.activeAsset refers to the asset currently selected in Tiled, but when evaluating a script from the command-line, the Tiled editor actually does not get created, so there is also no active asset.

Instead, your script will need to handle the command-line parameter itself, using tiled.scriptArguments. For a command-line evaluated script to load a map, it currently will have to load the map using the appropriate format, tiled.mapFormat("tmx").read(fileName) in your case, or using tiled.mapFormatForFile in case various formats are possible.

I'll change the tag on this issue since I think the documentation could use some more information about evaluating scripts on the command-line.

posted by bjorn over 2 years ago

It appears that map.layerDataFormat is an int (map is defined using map = tiled.mapFormat('tmx').read(mapPath);). The documentation seems to say it's a string, but if I change the value to an int (from 3 to 2) it seems to not change the map after if I save it using tiled.mapFormat('tmx').write(map, 'e.tmx');.

This is probably just me not reading docs but still.

posted by AnotherPillow over 2 years ago

The documentation seems to say it's a string

Where does it seem like that? map.layerDataFormat is documented to have one of a specific set of values, which can be accessed using TileMap.Base64 for example. These map to a C++ enum so the values are indeed actually ints.

Changing this property and re-saving the map is expected to change the format used to store the layer data.

posted by bjorn over 2 years ago

Where does it seem like that?

https://www.mapeditor.org/docs/scripting/classes/TileMap.html#layerDataFormat

I solved it by searching the repo for CSV and found this line and it seems to convert correctly when set to 5, thank you for your help!

posted by AnotherPillow over 2 years ago

Also, I just noticed that when I open the new map in notepad, the tileset paths are relative, as in ../path/to/tileset.png, is it possible to disable that? It's not like that originally.

posted by AnotherPillow over 2 years ago

Where does it seem like that?

https://www.mapeditor.org/docs/scripting/classes/TileMap.html#layerDataFormat

I solved it by searching the repo for CSV and found this line and it seems to convert correctly when set to 5, thank you for your help!

You should not be using the numeric values directly! They can change, and they make your code harder to read. Use the values as intended, e.g. map.layerDataFormat = TileMap.CSV

Paths exported by Tiled's native formats are always relative when possible, this allows maps to be portable. Tiled only uses absolute paths when relative paths are impossible, such as if the assets are on different drives. If you need to force absolute paths, I think you'd have to implement a custom format.

posted by eishiya over 2 years ago

Where does it seem like that?

https://www.mapeditor.org/docs/scripting/classes/TileMap.html#layerDataFormat I solved it by searching the repo for CSV and found this line and it seems to convert correctly when set to 5, thank you for your help!

You should not be using the numeric values directly! They can change, and they make your code harder to read. Use the values as intended, e.g. map.layerDataFormat = TileMap.CSV

Paths exported by Tiled's native formats are always relative when possible, this allows maps to be portable. Tiled only uses absolute paths when relative paths are impossible, such as if the assets are on different drives. If you need to force absolute paths, I think you'd have to implement a custom format.

Ah, thank you for your answer. I might just go through the xml manually to remove the paths.

posted by AnotherPillow over 2 years ago

Please don't mark this as completed yet, the documentation still needs to be updated (see bjorn's first reply).

posted by eishiya over 2 years ago

Fund this Issue

$0.00
Funded
Only logged in users can fund an issue

Pull requests