Tilemap corners are not working as intended (advice please)

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?

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.

1 Like

Thank you! I’ll try it out now!

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

		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

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():

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

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

My bottom left tile is on 1x1

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:

Whoops! Forget about that! But one more thing? 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:

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

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!

2 Likes

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!

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

		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

It should be:

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

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

@britzl?

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

1 Like

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

1 Like

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

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

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

1 Like

I’ve created and example where you can get and set tiles: Get and set tiles

2 Likes

Thank you! I’ll check it out now!

1 Like