# Tilemap corners are not working as intended (advice please)

**URL:** <https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983>\
**Category:** Questions\
**Created:** [July 30, 2021, 5:04pm UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983 "2021-07-30T17:04:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexusha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alexusha/32/25816_2.png) [@Alexusha](https://forum.defold.com/u/Alexusha)\
**Post date:** [July 30, 2021, 5:04pm UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/1 "2021-07-30T17:04:10Z")

</div>

Hello all! So, I’m been trying to make a platformer using a tilemap now, but my corner pieces aren’t working. I need it to act as a wall when the player is next to that _specific_ tile, but as ground when the player is above it. Does anyone have any advice on how to get around this problem, and if so, could you please send it? To put it simple, how can you check the position of a specific tile on a 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:** [July 31, 2021, 9:10pm UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/2 "2021-07-31T21:10:04Z")

</div>

> [@Alexusha](#):
>
> how can you check the position of a specific tile on a tilemap?

There is a tilemap.get\_tile() function which you can use. You need to convert your world coordinates to tilemap positions by diving with your tile dimensions.

---

<div class="post-metadata">

**Author:** ![Alexusha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alexusha/32/25816_2.png) [@Alexusha](https://forum.defold.com/u/Alexusha)\
**Post date:** [August 1, 2021, 4:56am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/3 "2021-08-01T04:56:39Z")

</div>

Thank you! I’ll try it out now!

---

<div class="post-metadata">

**Author:** ![Alexusha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alexusha/32/25816_2.png) [@Alexusha](https://forum.defold.com/u/Alexusha)\
**Post date:** [August 1, 2021, 5:18am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/4 "2021-08-01T05:18:39Z")

</div>

I just tried it out but I got an error message saying “ERROR:GAMESYS: Could not get the tile since the supplied tile was out of range.” Apparently, the part that’s wrong is the tilemap.get\_tile part, because in the next part, it said something about tilepos being a nil value. Here “ERROR:SCRIPT: /main/Player.script:132: attempt to index global ‘tilepos’ (a nil value)”

Here is my code

```pos
		tilepos = tilemap.get_tile("/level1#Level1", "layer1", pos.x, pos.y)
		tileposa = tilepos.x + 18
		tileposb = tilepos.x - 18
		if pos.x > tileposa then
			msg.post(".", "ground_contact")
		end
```

---

<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 1, 2021, 7:18am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/5 "2021-08-01T07:18:05Z")

</div>

What is pos.x and pos.y in this case?

You must also take into account the bounds of the tilemap if the bottom left tile isn’t at 1x1. You can get the bounds using tilemap.get\_bounds():

```lua
local x, y, w, h = tilemap.get_bounds("/level#tilemap")

```

---

<div class="post-metadata">

**Author:** ![Alexusha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alexusha/32/25816_2.png) [@Alexusha](https://forum.defold.com/u/Alexusha)\
**Post date:** [August 1, 2021, 7:47am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/6 "2021-08-01T07:47:35Z")

</div>

> [@britzl](#):
>
> What is pos.x and pos.y in this case?

pos is the player position, it was on the line just before the code I showed

> [@britzl](#):
>
> You must also take into account the bounds of the tilemap if the bottom left tile isn’t at 1x1. You can get the bounds using tilemap.get\_bounds():

My bottom left tile is on 1x1

---

<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 1, 2021, 7:48am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/7 "2021-08-01T07:48:58Z")

</div>

> [@Alexusha](#):
>
> pos is the player position

Ah, ok. The position you pass to tilemap.get\_tile() is the tile position, not the pixel position. This is why I recommended that you divide with your tile dimensions:

> [@britzl](#):
>
> You need to convert your world coordinates to tilemap positions by diving with your tile dimensions.

---

<div class="post-metadata">

**Author:** ![Alexusha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alexusha/32/25816_2.png) [@Alexusha](https://forum.defold.com/u/Alexusha)\
**Post date:** [August 1, 2021, 7:50am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/8 "2021-08-01T07:50:48Z")

</div>

> [@britzl](#):
>
> You need to convert your world coordinates to tilemap positions by diving with your tile dimensions.

Whoops! Forget about that! But one more thing? Isn’t it meant to be multiply by tile dimensions?

---

<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 1, 2021, 7:58am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/9 "2021-08-01T07:58:31Z")

</div>

> [@Alexusha](#):
>
> Isn’t it meant to be multiply by tile dimensions?

No. Let’s say your player character is at pixel position x: 90 and y: 50. If your tilemap tiles are 20 by 20 pixels then you’d calculate tile position like this:

```lua
local TILE_SIZE = 20
local player_pos = go.get_position("player")
local tile_x = math.floor(player_pos.x / TILE_SIZE)
local tile_y = math.floor(player_pos.y / TILE_SIZE)
local tile = tilemap.get_tile(tile_x, tile_y)

```

You may also wish to check the tilemap bounds first so you don’t call tilemap.get\_tile() on coordinates outside of the tilemap bounds (which will generate an error).

```lua
local TILE_SIZE = 20
local bx, by, bw, bt = tilemap.get_bounds("tilemap")
local player_pos = go.get_position("player")
local tile_x = math.floor(player_pos.x / TILE_SIZE)
local tile_y = math.floor(player_pos.y / TILE_SIZE)

local tile = -1
if tile_x >= bx and tile_x <= (bx + bw) - 1 and tile_y >= by and tile_y <= (by + bh) - 1 then
	tile = tilemap.get_tile("tilemap", "layer1", tile_x, tile_y)
end

if tile == -1 then
	print("out of bounds")
else
	print("tile", tile)
end

```

Typing the above without testing so there might be an error or two!

---

<div class="post-metadata">

**Author:** ![Alexusha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alexusha/32/25816_2.png) [@Alexusha](https://forum.defold.com/u/Alexusha)\
**Post date:** [August 1, 2021, 8:08am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/10 "2021-08-01T08:08:07Z")

</div>

Ohhhhhhhhhh! I though tilemap.get\_tile used world coordinates! So, then it thought to look for a tile way outside of the tilemap! That makes much more sense, thank you for everything! Will try now!

---

<div class="post-metadata">

**Author:** ![Alexusha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alexusha/32/25816_2.png) [@Alexusha](https://forum.defold.com/u/Alexusha)\
**Post date:** [August 1, 2021, 8:18am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/11 "2021-08-01T08:18:12Z")

</div>

It’s giving me the same error as before ☹, I’ll send you my code, which is based on your code, again.

```local
		local pos = go.get_position()
		local tile_x = math.floor(pos.x / tile_size)
		local tile_y = math.floor(pos.y / tile_size)
		local tile = tilemap.get_tile("/level1#Level1", "layer1", tile_x, tile_y)
		if pos.x > tile.x - 18 and pos.x < tile.x + 18 then
			msg.post(".", "wall_contact")
		else
			msg.post(".", "ground_contact")
		end
```

---

<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 1, 2021, 8:26am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/12 "2021-08-01T08:26:05Z")

</div>

> [@Alexusha](#):
>
> `if pos.x > tile.x - 18 and pos.x < tile.x + 18 then`

It should be:

```lua
if tile_x > tile.x - 18 and ...

```

---

<div class="post-metadata">

**Author:** ![Alexusha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alexusha/32/25816_2.png) [@Alexusha](https://forum.defold.com/u/Alexusha)\
**Post date:** [August 1, 2021, 8:28am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/13 "2021-08-01T08:28:20Z")

</div>

> [@Alexusha](#):
>
> I just tried it out but I got an error message saying “ERROR:GAMESYS: Could not get the tile since the supplied tile was out of range.” Apparently, the part that’s wrong is the tilemap.get\_tile part, because in the next part, it said something about tilepos being a nil value. Here “ERROR:SCRIPT: /main/Player.script:132: attempt to index global ‘tilepos’ (a nil value)”

It’s also giving me these errors, as I said in the last post but instead replacing tilepos with tile

---

<div class="post-metadata">

**Author:** ![Alexusha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alexusha/32/25816_2.png) [@Alexusha](https://forum.defold.com/u/Alexusha)\
**Post date:** [August 1, 2021, 10:01am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/14 "2021-08-01T10:01:48Z")

</div>

@britzl?

---

<div class="post-metadata">

**Author:** ![Alex\_8BitSkull](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alex_8bitskull/32/5424_2.png) [@Alex\_8BitSkull](https://forum.defold.com/u/Alex_8BitSkull)\
**Post date:** [August 1, 2021, 10:13am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/15 "2021-08-01T10:13:44Z")

</div>

It’s a Sunday, have some patience please! We’re lucky to have britzl replying to things on the weekend at all!

---

<div class="post-metadata">

**Author:** ![Alexusha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alexusha/32/25816_2.png) [@Alexusha](https://forum.defold.com/u/Alexusha)\
**Post date:** [August 1, 2021, 10:14am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/16 "2021-08-01T10:14:59Z")

</div>

OHHHHHHH, sorry completely forgot about the work days, ok, that makes sense! Thank you!

---

<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 1, 2021, 3:31pm UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/17 "2021-08-01T15:31:51Z")

</div>

No worries. I’m going to create a new example which will be available from here soon

> **[Tilemap collisions](https://defold.com/examples/tilemap/collisions/)**
>
> This example shows how to detect collisions on tilemaps

I’ll update this post when the example is ready.

---

<div class="post-metadata">

**Author:** ![Alexusha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alexusha/32/25816_2.png) [@Alexusha](https://forum.defold.com/u/Alexusha)\
**Post date:** [August 1, 2021, 5:44pm UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/18 "2021-08-01T17:44:51Z")

</div>

Thank you so much for all this help, this is why I love communities like this!

---

<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 1, 2021, 9:36pm UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/19 "2021-08-01T21:36:09Z")

</div>

> [@britzl](#):
>
> I’ll update this post when the example is ready.

I’ve created and example where you can get and set tiles: [Get and set tiles](https://defold.com/examples/tilemap/get_set_tile/)

---

<div class="post-metadata">

**Author:** ![Alexusha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alexusha/32/25816_2.png) [@Alexusha](https://forum.defold.com/u/Alexusha)\
**Post date:** [August 2, 2021, 5:54am UTC](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983/20 "2021-08-02T05:54:54Z")

</div>

Thank you! I’ll check it out now!

[Next page](https://forum.defold.com/t/tilemap-corners-are-not-working-as-intended-advice-please/68983.md?page=2)
