Tiled Script API failing intermittently

I am using Tiled’s export feature vie CLI. For the most part it has been working. But as my maps become larger and I add more layers with even more objects, some of the information that should be exported is not being exported. For instance, here is a code snippet:

objectLayer is a layer passed as an argument to a function.

        var fName = "";
        if (objectLayer.objects[c].tile != null && objectLayer.objects[c].tile.tileset != null) {

            try {
                fName = "          img_src = \"" + FileInfo.fileName(FileInfo.completeBaseName(objectLayer.objects[c].tile.tileset.name)) + "\",\n";
            }
            catch (err) {
                console.log(objectLayer.objects[c].id + " -> Failed with: " + err);
            }
        }

Just about ever object is assigned a tile from a tileset. But some objects will have tileset info and some won’t. But the object’s tile is from the same tileset.

Anyone have a clue what could be causing this? I use loops to process each layer and a loop for object processing.

What is the actual error you’re getting? Is it random (i.e. different objects throw the error every time), or consistent (the same objects always cause the error)? Also, in recent versions of Tiled, you have to use tiled.log, console.log does not work.
You’re using tileset.name to construct a path, but tileset.name is the string name of the tileset, which might not be set and might not be related to the file name. Did you mean to use tileset.fileName?

1 Like

The error I get is

qml: 314 -> Failed sh*t: TypeError: Cannot read property 'name' of null

Update: Sorry about the bad word. :innocent:

I can’t recall if I have tried fileName. Will give that a try.

Scratch that, I need name of the tilesets. Most of the sets are embedded

So tile.tileset is null sometimes. This can happen if the tile becomes removed from its tileset after being used somewhere, which is very uncommon and isn’t very likely to be something you do in an export script.

Is it always the same objects affected, or is it random? If it’s random, it might be issue #3565, where values that should not be null are null sometimes, at random. I haven’t seen it affect Tile.tileset before, but maybe it’s possible.
What version of Tiled are you using, and what is the Qt version (type tiled.qtVersion into the Tiled console to check)? This particular issue affects some Qt versions more than others. The fact that console.log apparently works for you suggests you’re using a rather old version of Tiled, which would be affected by this bug.

I am using qt version 6.4.3 and tiled version 1.10.2

The error happens randomly. Not always the same objects.

Seems issue #3565 might be my problem. From the post in that issue, I see a ref to my code which is actually what is causing my problem. Lol

Crud! I rewrote the code to be inline and it still fails, even with accessing the arrays directly. I cannot figure out a work around and my map format is blown to bits now. This puts my game on hold. :frowning:

@Bongo Please try the builds available here.

While the related PR is still WIP since there appears to be some leaking issue causing tilesets to not get deleted on shutdown, it should already work around the issues with the garbage collector, because now the script API wrapper objects are owned by C++ rather than the JS engine.

2 Likes

Umm, I am a dummy. Lol. What am I looking for? What do I need to D/L or install? :face_with_peeking_eye:

You have to be logged in on GitHub, and you should be able to scroll down on the first page bjorn linked and download the build appropriate for your OS.

1 Like

Ugh! I have to be too tired from work. I can’t believe I am this slow right now. Lol, Thanks.

OMG! You are a life saver!!!

1 Like