How can I add a script to "blocks" in a tileset?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Hagelsnow

I am wondering if I can write a custom script for each “block” of my tileset so my character can react differently to each block.

And if I cannot do this, how can I write the custom properties of the blocks?

:bust_in_silhouette: Reply From: Inces

If it is character that is supposed to react differently depending on neighbouring blocks - than he is the one that should be scripted for this. Easiest option is to make him child of TileMap and detect what are surrounding tiles each move, using TileMaps methods ( get_cell, world_to_map ). Finally, when block is detected make one MATCH statement and code chosen behavior under every detected tilemap cell, like in this pseudocode :

var pm = world_to_map(Player_position)
 for tile in [pm + vec2(1,0),pm + vec2(0,1),pm + vec2(-1,0 .... and so on :
        match get_cell(tile) :
               0 :
                      Player.defense += 10
               1:
                      Player.health -= 25

and so on, numbers are indexes of your blocks in tilemap

:bust_in_silhouette: Reply From: exuin

Another option is to use the tilemap to plan out the level design, and then on runtime, replace all the tiles with objects with their own scripts.