# createFromObjects does not take Tiled object id as argument

**URL:** https://phaser.discourse.group/t/createfromobjects-does-not-take-tiled-object-id-as-argument/9783
**Category:** Phaser 2 / CE
**Created:** [June 15, 2021, 5:19pm UTC](https://phaser.discourse.group/t/createfromobjects-does-not-take-tiled-object-id-as-argument/9783 "2021-06-15T17:19:20Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![SHG42](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/shg42/32/2231_2.png) [@SHG42](https://phaser.discourse.group/u/SHG42)
#### Post date: [June 15, 2021, 5:19pm UTC](https://phaser.discourse.group/t/createfromobjects-does-not-take-tiled-object-id-as-argument/9783/1 "2021-06-15T17:19:20Z")

</div>

The docs for tilemap.createFromObjects() say this:

> The `gid` argument is matched against:
> 
> 1. For a tile object, the tile identifier ( `gid` ); or
> 2. The object’s unique ID ( `id` ); or
> 3. The object’s `name` (a string)

I have a use case where it would be ideal to locate map objects by their id, not by their name or GID. However, when I try to do this:

```javascript
this.endpoints = this.map.objects.endpoints;

        this.ledgesGroup.endsgroup = this.game.add.group(this.ledgesGroup, 'endsgroup');

        this.endpoints.forEach((end)=>{

            this.end = this.map.createFromObjects('endpoints', end.id, 'utility', 'point', true, false, this.ledgesGroup.endsgroup);

  });

```

I get nothing. Console logging each this.end returns undefined, and logging each end in the forEach reveals that Phaser is receiving all of the object’s properties EXCEPT the object id:

```javascript
{ 
1. height: 0
2. name: "end"
3. rectangle: true
4. rotation: 0
5. type: "end"
6. visible: true
7. width: 0
8. x: 983
9. y: 832
}

```

Logging this.endpoints shows the same thing: Phaser isn’t even seeing the object ids, even though the json has them:

```javascript
{

                 "height":0,

                 "id":37, <=== THIS!

                 "name":"end",

                 "point":true,

                 "rotation":0,

                 "type":"end",

                 "visible":true,

                 "width":0,

                 "x":624,

                 "y":592

                },

```

Am I missing something? Am I making a mistake somewhere? Are the docs wrong? I’m using phaser-ce@2.18.0 and Tiled 1.7, and exporting my Tiled json files under the option of JSON map files [Tiled 1.1]. I tried exporting the tilemap with the regular JSON option and the result was the same, plus a console warning advising me to switch to the Tiled 1.1 exporter.  
EDIT: if it makes any difference, the endpoint objects are Point objects in Tiled.

---

<div class="post-metadata">

### Author: ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)
#### Post date: [June 15, 2021, 7:59pm UTC](https://phaser.discourse.group/t/createfromobjects-does-not-take-tiled-object-id-as-argument/9783/2 "2021-06-15T19:59:45Z")

</div>

The JSON id field seems to be completely ignored during parsing.  
Looking at the code for createFromObjects there would be a problem if there were GID’s _and_ ID’s, If they were in the same range, GID’s would take precedence.  
You could just assign id’s yourself:

```javascript
for (i = 0; i < this.endpoints.length; i++) {
    this.endpoints[i].id = i + 1;
}

```

But then make sure there are no GID’s in the same range,

---

<div class="post-metadata">

### Author: ![SHG42](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/shg42/32/2231_2.png) [@SHG42](https://phaser.discourse.group/u/SHG42)
#### Post date: [June 15, 2021, 9:06pm UTC](https://phaser.discourse.group/t/createfromobjects-does-not-take-tiled-object-id-as-argument/9783/3 "2021-06-15T21:06:15Z")

</div>

Well, good to know it wasn’t just my code, then. LOL. I decided to grab the id’s from the level json file in preload, but thanks for providing another possibility! In case anyone else finds this thread while encountering the same issue, here’s what I did:  
//in Preload class:

```javascript
this.assetData.tilemaps.forEach((tilemap)=>{

            this.game.load.tilemap(tilemap, `./assets/tilemaps/${tilemap}.json`, null, Phaser.Tilemap.TILED_JSON);

            this.game.load.json(tilemap, `./assets/tilemaps/${tilemap}.json`);

})

```

//In Game class when creating map:

```javascript
this.map = this.game.add.tilemap(this._LEVELS[this._LEVEL]);

        

        this.jsonfile = this.cache.getJSON(this.map.key);

        this.jsonfile.layers.forEach((layer) => {

            if(layer.name === "endpoints") {

                this.map.endpoints = layer.objects;

            }

 });

this.map.endpoints.forEach((end)=>{

            this.endPt = this.ledgesGroup.endsgroup.create(end.x, end.y, 'point');

            this.endPt.id = end.id;

});

```

---

<div class="post-metadata">

### Author: ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)
#### Post date: [June 16, 2021, 3:20pm UTC](https://phaser.discourse.group/t/createfromobjects-does-not-take-tiled-object-id-as-argument/9783/4 "2021-06-16T15:20:04Z")

</div>

Probably the Phaser method shouldn’t be “fixed” as it would introduce ambiguity.

The argument could be changed to something like `{ id: 37 }` vs. `{ gid: 37 }`.

I do have a branch where the 11th(!) argument of `createFromObjects()` is a filter function.

@SHG42 could you post your map JSON if possible?

---

<div class="post-metadata">

### Author: ![SHG42](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/shg42/32/2231_2.png) [@SHG42](https://phaser.discourse.group/u/SHG42)
#### Post date: [July 9, 2021, 6:38pm UTC](https://phaser.discourse.group/t/createfromobjects-does-not-take-tiled-object-id-as-argument/9783/6 "2021-07-09T18:38:58Z")

</div>

Here it is. Well, one of them, anyway! This is the map JSON for the level I was using for testing.  
[cave.json](https://phaser.discourse.group/uploads/short-url/aKynyOMhv8oQiwVb0NcDyTlFlit.json) (239.1 KB)

---

<div class="post-metadata">

### Author: ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)
#### Post date: [August 25, 2021, 4:43pm UTC](https://phaser.discourse.group/t/createfromobjects-does-not-take-tiled-object-id-as-argument/9783/7 "2021-08-25T16:43:37Z")

</div>

New in [Phaser CE v2.19.0](https://phaser.discourse.group/t/phaser-ce-v2-19-0/10154), you can do:

```javascript
const tiledObject = tilemap.getObject(37);

```

or

```javascript
tilemap.createFromObjects('objectLayerName', ['id', 37]);

```
