# Tilemap.get\_bounds seems to be returning too high values for x and y (SOLVED)

**URL:** <https://forum.defold.com/t/tilemap-get-bounds-seems-to-be-returning-too-high-values-for-x-and-y-solved/63>\
**Category:** Bugs\
**Created:** [April 25, 2015, 12:38pm UTC](https://forum.defold.com/t/tilemap-get-bounds-seems-to-be-returning-too-high-values-for-x-and-y-solved/63 "2015-04-25T12:38:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Acegikmo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/acegikmo/32/36_2.png) [@Acegikmo](https://forum.defold.com/u/Acegikmo)\
**Post date:** [April 25, 2015, 12:38pm UTC](https://forum.defold.com/t/tilemap-get-bounds-seems-to-be-returning-too-high-values-for-x-and-y-solved/63/1 "2015-04-25T12:38:51Z")

</div>

This may or may not be by design and/or luas’ fault due to 1-indexing, but I keep finding myself doing this:

`local x,y,w,h = tilemap.get_bounds("#tilemap")
x = x-1
y = y-1`

In order to get proper bounds values

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [April 27, 2015, 6:45am UTC](https://forum.defold.com/t/tilemap-get-bounds-seems-to-be-returning-too-high-values-for-x-and-y-solved/63/2 "2015-04-27T06:45:30Z")

</div>

Yes, it is by design. (If you use ipairs() to iterate 1-indexed arrays usually becomes transparent.)

---

<div class="post-metadata">

**Author:** ![benjames171](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/benjames171/32/34759_2.png) [@benjames171](https://forum.defold.com/u/benjames171)\
**Post date:** [August 4, 2016, 11:51pm UTC](https://forum.defold.com/t/tilemap-get-bounds-seems-to-be-returning-too-high-values-for-x-and-y-solved/63/3 "2016-08-04T23:51:35Z")

</div>

I’m trying to get round this same problem but can’t work out the syntax for iterating through a tilemap using ipairs().

Please can anyone enlighten me?

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [August 5, 2016, 12:17am UTC](https://forum.defold.com/t/tilemap-get-bounds-seems-to-be-returning-too-high-values-for-x-and-y-solved/63/4 "2016-08-05T00:17:44Z")

</div>

I believe the files in the zip have a working example.

> [@Big List of Defold Pro Tips!](https://forum.defold.com/t/big-list-of-defold-pro-tips/1519/42):
>
> Basic tiled importer. This is basically the example @Andreas_Jirenius made in one of his videos + sloppy parts I did with testing it (but do not worry it will be polished more later). It is rough, but should be enough to get people started, and is more accessible to the public. On my big list of todos, I plan to make a generic importer that can more easily be added to projects and then expanded based on the needs of the project. This method imports [Tiled](http://www.mapeditor.org/) data to replace what is in a Defold til…

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 5, 2016, 8:57pm UTC](https://forum.defold.com/t/tilemap-get-bounds-seems-to-be-returning-too-high-values-for-x-and-y-solved/63/5 "2016-08-05T20:57:51Z")

</div>

You can’t use ipairs() to iterate a tilemap. The get\_bounds() function will return the bounds as a starting x,y pair and a width and height. This implies iteration using a numeric for loop:

```
local x, y, w, h = tilemap.get_bounds("#mytilemap")
for xi = x, x + w - 1 do
	for yi = y, y + h - 1 do
		local tile = tilemap.get_tile("#mytilemap", "mylayer", xi, yi)
	end
end

```

---

<div class="post-metadata">

**Author:** ![benjames171](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/benjames171/32/34759_2.png) [@benjames171](https://forum.defold.com/u/benjames171)\
**Post date:** [August 5, 2016, 9:17pm UTC](https://forum.defold.com/t/tilemap-get-bounds-seems-to-be-returning-too-high-values-for-x-and-y-solved/63/6 "2016-08-05T21:17:18Z")

</div>

Thanks @britzl - that’s basically what I’m doing. It works perfectly well but throws up a bunch of errors reading:

> ERROR:GAMESYS: Could not get the tile since the supplied tile was out of range.

It seems that accessing a layer that doesn’t cover the whole tilemap will go out of range once you go past the outermost tile on that layer - even if it’s short of the width or height of the tilemap as a whole.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 5, 2016, 9:20pm UTC](https://forum.defold.com/t/tilemap-get-bounds-seems-to-be-returning-too-high-values-for-x-and-y-solved/63/7 "2016-08-05T21:20:02Z")

</div>

Hmm, I see. I’m not able to do any experiments with this currently, but I’d try to do a protected call to get\_tile to catch the error:

```
local x, y, w, h = tilemap.get_bounds("#mytilemap")
for xi = x,x+w do
	for yi = y,y+h do
		local ok, tile = pcall(tilemap.get_tile, "#mytilemap", "mylayer", xi, yi))
		if ok then
			-- do stuff with tile
		end
	end
end

```

---

<div class="post-metadata">

**Author:** ![benjames171](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/benjames171/32/34759_2.png) [@benjames171](https://forum.defold.com/u/benjames171)\
**Post date:** [August 5, 2016, 9:42pm UTC](https://forum.defold.com/t/tilemap-get-bounds-seems-to-be-returning-too-high-values-for-x-and-y-solved/63/8 "2016-08-05T21:42:28Z")

</div>

No dice, I’m afraid 😞

Doing a protected call on tilemap\_get.tile also generates the same error.

It’s not a huge issue right now as the code still works but it would be nice to get it fixed at some point.

---

<div class="post-metadata">

**Author:** ![benjames171](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/benjames171/32/34759_2.png) [@benjames171](https://forum.defold.com/u/benjames171)\
**Post date:** [August 9, 2016, 10:01pm UTC](https://forum.defold.com/t/tilemap-get-bounds-seems-to-be-returning-too-high-values-for-x-and-y-solved/63/9 "2016-08-09T22:01:18Z")

</div>

This was my mistake in the end - was going 1 over the bounds of the tilemap.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 9, 2016, 10:09pm UTC](https://forum.defold.com/t/tilemap-get-bounds-seems-to-be-returning-too-high-values-for-x-and-y-solved/63/10 "2016-08-09T22:09:08Z")

</div>

Excellent. I’m happy to hear that you found the cause of the problem!
