The title pretty much says everything, I need an instance (that spawns at the position of a sprite which follows the mouse and snaps to a tile map's tiles) to delete another instance if there is another one in the exact position. The instances aren't stored in the tilemap at all, they are stored in a Node2D ( which is separate to the Main Node2D of the scene) with a Y-Sort in it. I have asked this before but I'm trying to take a different approach to it. My current idea is for the newly instanced object to detect if another instanced object is in the exact same position, and if there is another instanced object, the newly instanced object gets the old instance's id and delete said old instance.
Possibly something like this? :
(In the Main scenes script which handles placing the buildings)
func place_building():
if BuildingValue.buildingToPlace == 0:
var A = preload("path/A.tscn")
var A_spawn = A.instance()
A_spawn.position = $HighlightBox.position
$Node2D.add_child(A_spawn)
if BuildingValue.buildingToPlace == 1:
var B = preload("path/B.tscn")
var B_spawn = B.instance()
B_spawn.position = $HighlightBox.position
$Node2D.add_child(B_spawn)
func _input(event):
if Input.is_action_just_released("place") && GameManager.can_place == true:
place_building()
else:
if ExampleNewInstance.position == ExampleOtherInstance.position:
var ExampleVar = ExampleOtherInstance.get_instance_id()
$Node2D.remove_child(ExampleOtherInstance)
The top function is just an example of the place_building() function.
I don't know if this even remotely close, my guess is it's not but I'm just sharing my thinking process.
Thanks in advance :D