# Tiled Script API failing intermittently

**URL:** https://discourse.mapeditor.org/t/tiled-script-api-failing-intermittently/6400
**Category:** Uncategorized
**Created:** [November 12, 2023, 1:42am UTC](https://discourse.mapeditor.org/t/tiled-script-api-failing-intermittently/6400 "2023-11-12T01:42:21Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)
#### Post date: [November 12, 2023, 1:42am UTC](https://discourse.mapeditor.org/t/tiled-script-api-failing-intermittently/6400/1 "2023-11-12T01:42:21Z")

</div>

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.

```auto
        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.

---

<div class="post-metadata">

### Author: ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)
#### Post date: [November 12, 2023, 1:53am UTC](https://discourse.mapeditor.org/t/tiled-script-api-failing-intermittently/6400/2 "2023-11-12T01:53:06Z")

</div>

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`?

---

<div class="post-metadata">

### Author: ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)
#### Post date: [November 12, 2023, 3:09am UTC](https://discourse.mapeditor.org/t/tiled-script-api-failing-intermittently/6400/3 "2023-11-12T03:09:00Z")

</div>

The error I get is

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

```

Update: Sorry about the bad word. 😇

---

<div class="post-metadata">

### Author: ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)
#### Post date: [November 12, 2023, 3:09am UTC](https://discourse.mapeditor.org/t/tiled-script-api-failing-intermittently/6400/4 "2023-11-12T03:09:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)
#### Post date: [November 12, 2023, 3:17am UTC](https://discourse.mapeditor.org/t/tiled-script-api-failing-intermittently/6400/5 "2023-11-12T03:17:05Z")

</div>

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](https://github.com/mapeditor/tiled/issues/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.

---

<div class="post-metadata">

### Author: ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)
#### Post date: [November 12, 2023, 3:19am UTC](https://discourse.mapeditor.org/t/tiled-script-api-failing-intermittently/6400/6 "2023-11-12T03:19:41Z")

</div>

> [@eishiya](#):
>
> tiled.qtVersion

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

---

<div class="post-metadata">

### Author: ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)
#### Post date: [November 12, 2023, 11:11pm UTC](https://discourse.mapeditor.org/t/tiled-script-api-failing-intermittently/6400/7 "2023-11-12T23:11:30Z")

</div>

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. ☹

---

<div class="post-metadata">

### Author: ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)
#### Post date: [November 13, 2023, 7:28pm UTC](https://discourse.mapeditor.org/t/tiled-script-api-failing-intermittently/6400/8 "2023-11-13T19:28:01Z")

</div>

@Bongo Please try the [builds available here](https://github.com/mapeditor/tiled/actions/runs/6852847248).

While the [related PR](https://github.com/mapeditor/tiled/pull/3843) 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.

---

<div class="post-metadata">

### Author: ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)
#### Post date: [November 13, 2023, 10:15pm UTC](https://discourse.mapeditor.org/t/tiled-script-api-failing-intermittently/6400/9 "2023-11-13T22:15:33Z")

</div>

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

---

<div class="post-metadata">

### Author: ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)
#### Post date: [November 13, 2023, 10:18pm UTC](https://discourse.mapeditor.org/t/tiled-script-api-failing-intermittently/6400/10 "2023-11-13T22:18:22Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)
#### Post date: [November 13, 2023, 10:26pm UTC](https://discourse.mapeditor.org/t/tiled-script-api-failing-intermittently/6400/11 "2023-11-13T22:26:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)
#### Post date: [November 14, 2023, 12:08am UTC](https://discourse.mapeditor.org/t/tiled-script-api-failing-intermittently/6400/12 "2023-11-14T00:08:54Z")

</div>

OMG! You are a life saver!!!
