# Get the X/Y from objects in a layer

**URL:** <https://discourse.mapeditor.org/t/get-the-x-y-from-objects-in-a-layer/5754>\
**Category:** Questions\
**Created:** [August 7, 2022, 8:04am UTC](https://discourse.mapeditor.org/t/get-the-x-y-from-objects-in-a-layer/5754 "2022-08-07T08:04:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![naknode](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/naknode/32/1593_2.png) [@naknode](https://discourse.mapeditor.org/u/naknode)\
**Post date:** [August 7, 2022, 8:04am UTC](https://discourse.mapeditor.org/t/get-the-x-y-from-objects-in-a-layer/5754/1 "2022-08-07T08:04:25Z")

</div>

Let’s say I have a `JSON` export of this map:

```json
{
	"compressionlevel": -1,
	"height": 200,
	"infinite": false,
	"layers": [{
			"comment": "// snippet out for clarity"
		},
		{
			"draworder": "index",
			"id": 4,
			"name": "NPCs",
			"objects": [{
					"class": "",
					"gid": 568,
					"height": 64,
					"id": 21,
					"name": "Fred the Town Bum",
					"properties": [{
							"name": "actions",
							"type": "string",
							"value": "examine,talk"
						},
						{
							"name": "examine",
							"type": "string",
							"value": "He stinks."
						}
					],
					"rotation": 0,
					"visible": true,
					"width": 64,
					"x": 4672,
					"y": 7680
				}
			],
			"opacity": 1,
			"type": "objectgroup",
			"visible": true,
			"x": 0,
			"y": 0
		},
		{
			"draworder": "topdown",
			"id": 5,
			"name": "Monsters",
			"objects": [],
			"opacity": 1,
			"type": "objectgroup",
			"visible": true,
			"x": 0,
			"y": 0
		}
	],
	"nextlayerid": 6,
	"nextobjectid": 29,
	"orientation": "orthogonal",
	"renderorder": "right-down",
	"tiledversion": "1.9.0",
	"tileheight": 64,
	"tilesets": [{
			"firstgid": 1,
			"source": "background.tsx"
		},
		{
			"firstgid": 253,
			"source": "foreground.tsx"
		},
		{
			"firstgid": 550,
			"source": "objects\/Monsters.tsx"
		},
		{
			"firstgid": 568,
			"source": "objects\/NPCs.tsx"
		}
	],
	"tilewidth": 64,
	"type": "map",
	"version": "1.9",
	"width": 200
}

```

As I’m writing my game (in TypeScript), how would I get the X/Y coordinate to snip from the Tileset to paint “Fred the Town Bum” from the NPC tileset. I know it because the `gid` is 568 which has the source of `"source": "objects\/NPCs.tsx"`. But would it then realize of… get this tileset… clip it at X=n and Y=n.

Basically… how would I get the information from the `gid` of the tile in the tileset to clip?

---

<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:** [August 7, 2022, 2:02pm UTC](https://discourse.mapeditor.org/t/get-the-x-y-from-objects-in-a-layer/5754/2 "2022-08-07T14:02:20Z")

</div>

First, you’d need to make sure you read and clear any flip flags on the tile. The [documentation about global IDs](https://doc.mapeditor.org/en/stable/reference/global-tile-ids/) has info about this.

Then, for a “Based on Tileset Image” tileset, you can calculate the x, y position in terms of tiles based on the tileset’s `columns`, and then you’d multiply x and y by the tile size and `spacing` and add the tileset’s `margin` to get the pixel location.

I’ve written about this in detail in my tip sheet: [Tiled Tip Sheet](http://eishiya.com/articles/tiled/#rendering-tilesets)

---

<div class="post-metadata">

**Author:** ![naknode](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/naknode/32/1593_2.png) [@naknode](https://discourse.mapeditor.org/u/naknode)\
**Post date:** [August 7, 2022, 9:05pm UTC](https://discourse.mapeditor.org/t/get-the-x-y-from-objects-in-a-layer/5754/3 "2022-08-07T21:05:02Z")

</div>

Thank you. Where are you getting the tileset’s `columns` and `margin`? When I go the referenced tileset. for example `objects/NPCS.tsx`, I get:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.5" tiledversion="1.7.2" name="NPCs" tilewidth="64" tileheight="64" tilecount="8" columns="8" objectalignment="topleft">
 <image source="../../../client/src/assets/graphics/actors/npcs.png" width="512" height="64"/>
</tileset>

```

As for the flag flipping, I have cleared that:

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/8/8991e362f59f3c607308eb709610a6479794dbb8.png)

---

<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:** [August 7, 2022, 11:01pm UTC](https://discourse.mapeditor.org/t/get-the-x-y-from-objects-in-a-layer/5754/4 "2022-08-07T23:01:20Z")

</div>

You get those values from the tileset. Your example tileset has a `columns` property, looks like its value is `8`. If `spacing` or `margin` are missing, that means the value is `0` - trivial defaults are not written out.

And by clearing the flags, I mean **in your code**. You shouldn’t rely on the incoming map files to be devoid of these flags. Even if you don’t want to implement support for flipped tiles, you should at least clear the flags so that you don’t run the risk of trying to use a GID with flip flags to get tile index or position - it’ll give you results way beyond the tileset’s bounds.

---

<div class="post-metadata">

**Author:** ![naknode](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/naknode/32/1593_2.png) [@naknode](https://discourse.mapeditor.org/u/naknode)\
**Post date:** [August 8, 2022, 12:16am UTC](https://discourse.mapeditor.org/t/get-the-x-y-from-objects-in-a-layer/5754/5 "2022-08-08T00:16:15Z")

</div>

Got it. I’m not using any framework or engine so I’m just reading the `data` property from the `layers` property in the `json` export so I’m good. Thank you
