mapeditor / tiled

Flexible level editor
https://www.mapeditor.org/
Other
11.05k stars 1.75k forks source link

Image in tileset added by extension cannot be found until project reloaded #2841

Closed meseta closed 4 years ago

meseta commented 4 years ago

I have a JS extension that is adding a tileset to the active asset. However upon running the action, Tiled reports "some files could not be found". At this point without doing anything else or making any changes, saving the project and reloading it causes the file to now be found, despite no further changes being made.

Tiled Version: 1.4.0 OS: Windows 10 (version 2004, 19041.329)

Repro steps:

  1. Make the following extension:

    
    let newAction = tiled.registerAction('Test', function(action) {
    let tileset = new Tileset("test");
    tileset.image = "C:/test.png"
    tileset.setTileSize(24, 24);
    
    tiled.activeAsset.addTileset(tileset);

}); newAction.text = "Test Extension";

tiled.extendMenu("File", [ { action: "Test"}, ]);


2. Make sure C:/test.png exists
3. With a tmx project active, select "Test Extension" from menu

**Expected outcome**
A new tileset is created called "test" and containing the image from C:/test.png

**Actual outcome**
A new tileset is created called "test" but the image is blank, and this warning is shown:
![image](https://user-images.githubusercontent.com/930832/85227380-03b7bb00-b3d5-11ea-81a0-e99f846c3521.png)
Clicking "Save" and then "Reload" causes the error to go away, and the "test" tileset will now load the expected image

Am I doing something wrong in the tileset creation? For now I will instruct users to save and reload after running part of the extension that needs to do this.

Thanks for making Tiled!
bjorn commented 4 years ago

Hmm, I couldn't reproduce this on Linux (the missing files list only shows up when I do this with an image that does not exist), so it may be Windows-specific. I'll try it on Windows as well.

Btw, from a usability perspective it would be a good idea to add the following line to the end of the action:

    tiled.mapEditor.tilesetsView.currentTileset = tileset;

I realize Tiled doesn't do this either when you add a tileset, but I'll change that. :-)

meseta commented 4 years ago

thank you! I will add that. Please let me know if there's anything you need me to test on my end

bjorn commented 4 years ago

I've been able to reproduce the issue on Windows. It's a problem in the code, which thinks it's a URL with "C" scheme. So, the snippet would work if you had used "file://C:/test.png" to refer to the image. But I'll fix this up so that also on Windows this case is properly recognized. There are a number of places in the code that need adjustment.

meseta commented 4 years ago

nice! thanks for that, I can confrim that adding "file:///" (three slashes) before the drive letter on windows path resolves the problem

bjorn commented 4 years ago

Right, three slashes even! Well, with the above fix you can also just use the plain absolute file path. :-)