it posible to make tile as button??

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

i want all box/grid with number can be press and randomly change texture, i can do it with button(node) but can it be done with tile/tilemap?
if posible,
how should i do it?
what key word i should type in google?
there any tutorial?

sorry for my broken english.
image here

Check out this post and tell me if it helps you so I can turn it into a proper answer.

johnygames | 2019-12-31 15:04

what i try to is something close to first comment say, but i don’t know how to put the logic on the TileMap itself.

potatobanana | 2019-12-31 16:11

:bust_in_silhouette: Reply From: 2D

Answered this based on this link.

extends TileMap

onready var RNG: RandomNumberGenerator =
var change_tiles = [] #Contains valid indices in tileset to change to

func _ready():

func _unhandled_input(event):
	if event.is_action_pressed("clicked"):
		var mouse_pos = get_viewport().get_mouse_position()
		var tile_pos = world_to_map(mouse_pos)
		if can_tile_change(tile_pos):
			var random_tile = RNG.randi_range(0, change_tiles.size() - 1)
			set_cellv(tile_pos, random_tile)

func can_tile_change(tile: Vector2) -> bool: #Add more logic here
	if get_cellv(tile) != INVALID_CELL:
		return true
		return false

You will need to populate the change_tiles array with valid indices from the tileset that the tile can change to. So, all of your possible textures it can change to will need to be in the tileset.

I created an Input Map valued named “clicked” that maps to the left mouse button. You may want to handle this differently.

The cantilechange function will need to make sure it is a valid click. Here I just check that the tile actually has an index in the tileset, but you will want to add other stuff as well.

thanks you so much, i try your code its incredible,
i want all cell can do like this.
this video of my project video
thi my project file project file

can you tell me how can make my buy button appear back, as you can see in my project i dont know how to make it appear back, i try autoload but still cant do it TT.TT.

potatobanana | 2020-01-01 12:42