Is it possible to change/animate tilemap cells?

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

Was just curious as I’m making a cool map reveal thing and using get_used_cells works great. However, the ability to change their alpha of a cell would be nice so we can transition them.
Thanks for reading!

:bust_in_silhouette: Reply From: Zylann

I don’t understand well what you do with get_used_cells, but there is a way to change cells with tilemap.set_cell(x, y, tile_id).

There is no way of changing the alpha of tiles individually, but if you want to do a tile-based map reveal thing you can do it with a second tilemap that hides the first one with tiles made of several shades of black.

:bust_in_silhouette: Reply From: eons

To answer the title, yes, just animate the region (discrete if is just a texture change), organize your tileset first to make it easier.

Example of simple animation:
https://twitter.com/_eons/status/785876343271530496

Multiple tiles changed at once:
https://twitter.com/_eons/status/786383684110737409


You can change tiles too with set_cell, x and y parameter refer to the tilemap grid.
Here is an example of a “static body” cell changed to a cell without collision:
https://twitter.com/_eons/status/786620636210663424


Now about the alpha thing, you are working over a single texture and you can’t alter a region of it, you are talking about another tilemap or a sprite over the tilemap.

It is possible to overlap many tilemaps if you want, here is another of my failed experiments:
https://twitter.com/_eons/status/807683074624516096


If you want to animate the cell transition, you can use a sprite over the cell on the map_to_world position.

http://docs.godotengine.org/en/stable/classes/class_tilemap.html?highlight=tilemap#class-tilemap-map-to-world

:bust_in_silhouette: Reply From: Jocquijoke

Hi,

I know it has been a while, but I needed this too and came up with this solution, using a simple script attached to my tilemap. Note that the texture must be a sequence of all the frames (so a 64x32 image for 2 frames of size 32x32 for example). The region associated with the tileset will be ((0, 0), cell_size) and we want to translate that in time. So we just create a timer (its wait time will be the time between two consicutive frames) and attach this script to the tilemap:

extends TileMap

var nb_frames: int = 0
var current_frame: int = 0

func _ready():
	nb_frames = tile_set.tile_get_texture(0).get_size().x / cell_size.x

func next_frame():
	current_frame = (current_frame + 1) % nb_frames
	tile_set.tile_set_region(0, Rect2(Vector2(current_frame*cell_size.x, 0), cell_size))

func _on_Timer_timeout():
	next_frame()
 

Of course don’t forget to connect the timer to your tilemap.

I hope it can help!