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)
if BuildingValue.buildingToPlace == 0:
var A = preload("path/A.tscn")
var A_spawn = A.instance()
A_spawn.position = $HighlightBox.position
if BuildingValue.buildingToPlace == 1:
var B = preload("path/B.tscn")
var B_spawn = B.instance()
B_spawn.position = $HighlightBox.position
if Input.is_action_just_released("place") && GameManager.can_place == true:
if ExampleNewInstance.position == ExampleOtherInstance.position:
var ExampleVar = ExampleOtherInstance.get_instance_id()
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