How to change a property of a tile in code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GunPoint
:warning: Old Version Published before Godot 3 was released.

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 get_node and get_node_and_resource but i get an error.
Sorry for my bad english

Oh, i forgot to change the title xD

GunPoint | 2017-08-09 14:10

:bust_in_silhouette: Reply From: kidscancode

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:

When i do this, my tiles disappper. Don’t know why… I used
var tile_pos = get_node(‘TileMap’).map.world_to_map(get_pos()) and
var cell = get_node(‘TileMap’).map.get_cell(tile_pos.x, tile_pos.y)
in KinematicBody2D but my tiles just disappear.

GunPoint | 2017-08-11 14:02

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…

kidscancode | 2017-08-13 20:55

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

GunPoint | 2017-08-14 09:01