how to check if something at top of tile?

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

how to check if something at top of tile?
i make one type cell can spawn tower/scene, but i dont want it keep spawn at same position after i spawn once. how should i stop it?

this my video

this my code

export (PackedScene) var tower_1 

func _unhandled_input(event: InputEvent) -> void:
	var mouse_pos = get_viewport().get_mouse_position()
	var tile_pos = world_to_map(mouse_pos)
	var tile_cell_at_mouse_pos = get_cellv(tile_pos)
	if event.is_action_pressed("Lclick"):
		if tile_cell_at_mouse_pos != INVALID_CELL:  # if tile cell is full with tile
			if tile_cell_at_mouse_pos == 0:# transform normal grass  tile to dirt tile
				set_cellv(tile_pos,randi() % 2 + 1)
			if tile_cell_at_mouse_pos == 1 :  # buy tower ballista
				var tower_instance = tower_1.instance()
				tower_instance.global_position = map_to_world(tile_pos) + cell_size/2
:bust_in_silhouette: Reply From: jgodfrey

So, you want to know if a given tile has already had something spawned into it? As you spawn items, just keep track of what cell they’re associated with. Then, prior to spawning the next item, just ensure the selected location isn’t already used.

Depending on details that aren’t clear, you could track the spawned items in a number of ways…

If you only care that a cell has had something spawned onto it (but don’t really care what that something is), you could simply track the cell id’s that you’ve spawned onto (so, for example, a simple Array of ints).

If you do care what was spawned there, you could create a map between a spawned item and its cell id using a Dictionary

Or, you could decide to simply create an array of all cell id’s and just remove a given element once you’ve spawned something onto it. That way, your array would always contain a list of cells that didn’t yet have a spawned item.

There are lots of straight-forward ways to do this, but I think you’ll need to somehow track what/where you’ve spawned items while you’re adding them…

thanks, remove a given element is the easy way.

potatobanana | 2020-03-15 15:19