How to get position of an instanced scene and replace that instanced scene with another?

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

I’m making a tile based building game, the tiles are based on a tile map, and the buildings are sprites with an area2D and collision shape in their own scenes, the buildings are instanced in the main scene at another sprite that follows the tilemap’s tiles (acting as the grid snap) when the button is clicked and it will place down the building that corresponds to a value. When placing down buildings they can overlap and it’s obvious they do because they are animated sprites.

I first tried the area2D and when another area2D entered it, it would delete it’s self but it wouldn’t replace the previous building and it was also very unreliable, it would work sometimes and not other times. I tried an input event, on self clicked delete self, but that didn’t work.
I tried loading the scene into a seperate value and did replace_by(BuildingData.buildingToPlace), but that returned a null or a nill or something else.

What I need to figure out is if another instanced scene is in the position that I’m placing the new instanced scene, and how to delete or replace the old instanced scene for the new instanced scene to be placed.

Thanks in advance! :smiley:

:bust_in_silhouette: Reply From: Inces

You need to keep track of Your tilemap contents with dictionary. When You create map I would advise You to iterate through all tiles and create dictionary with keys for all tiles like this one :

var tiledata = { (0,0) : null, (0,1) : null ........

and every time You try to place building You first check if there is something already built in this place using this tiledata, and if it is empty You place the building and update tiledata with the node You just build, like :

var w = building.instance()
tiledata[(2,3)] = w

Thanks to this You have stored references of real nodes for every place on map. Now when You build node on another node You can use this reference to queue_free older node.

Would this still work with my current approach? The buildings aren’t stored within the tile map, but are spawned at the position of a box which follows the mouse and grid snaps to the tile map, so the buildings are spawned into the scene at the box’s position and not stored in the tile map, they currently are spawned in a Node2D (separate to the Main Node2D of the scene) which has a Y-Sort Node in it. If this doesn’t work, then how would I make the buildings spawn as children of the tile map, and spawn at the correct position, the correct position being at the position of the box that indicates where you are place which is the box I talked about earlier.

Lazulily | 2021-11-25 00:28

Sorry I forgot to mention something: The tiles in the tilemap are all one tile, they are all just a black square.

Lazulily | 2021-11-25 00:32