Topic was automatically imported from the old Question2Answer platform.
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?
extends TileMap
onready var RNG: RandomNumberGenerator = RandomNumberGenerator.new()
var change_tiles = [] #Contains valid indices in tileset to change to
func _ready():
RNG.randomize()
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
else:
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.