0 votes

So, i want my character(KinematicBody2D) to interact with tiles when colliding with them. They are located in a Tilemap. There are 6 tiles but i want to interact with only one. How i can get that tile in code? I tried getnode and getnodeandresource but i get an error.
Sorry for my bad english

in Engine by (406 points)

Oh, i forgot to change the title xD

1 Answer

+2 votes
Best answer

Individual tiles in the tilemap can be accessed using get_cell()

To convert from world coordinates (player's pos) to tile coordinates, use world_to_map()

For example:

if is_colliding():
    var tile_pos = map.world_to_map(get_pos())
    var cell = map.get_cell(tile_pos.x, tile_pos.y)

See the TileMap docs for details:

by (21,920 points)
selected by

When i do this, my tiles disappper. Don't know why... I used
var tilepos = getnode('TileMap').map.worldtomap(getpos()) and
var cell = get
node('TileMap').map.getcell(tilepos.x, tile_pos.y)
in KinematicBody2D but my tiles just disappear.

To be clear, your tiles disappear when you do get_node()? That doesn't seem possible, as that code doesn't alter anything - are you sure you're not doing something else as well?

Also, is your TileMap a child of KinematicBody2D? That seems strange...

Yes, my tiles just dissapear. And if i delete these two variables (tilepos and cell) they re-appear.
TileMap is not a child of KinematicBody2D, is a child of Node2D

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.