Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | gogodot77 |
I have a scene called Area2DTest with an Area2D
as the root node. I would like to be able to place several of these Area2DTest scenes under the root node of the project and have them tell me what is overlapping so that each node can do some internal set up depending on what they overlap. The scene has an area_entered
signal that runs a function called _on_Area2DTest_area_entered
.
Area2DTest has code as follows:
extends Area2D
func _ready():
print(self, " _ready(), ", get_overlapping_areas())
#if there's any overlapping areas, do some stuff here.
#but get_overlapping_areas() always returns an empty array, so we can't. :(
func _process(_delta):
print(self, " _process() ", get_overlapping_areas())
func _physics_process(_delta):
print(self, " _physics_process() ", get_overlapping_areas())
func _on_Area2DTest_area_entered(_area):
print(self, " _on_Area2DTest_area_entered() ", get_overlapping_areas())
#get_overlapping_areas returns an array of overlapping areas - woohoo!
#But _ready and the first _physics_process have already run.
#Too late for doing various initialisations. :(
I place two of the Area2DTest scenes in the root scene like so:
- Node
- Area2DTest
- Area2DTest2
I make sure their collision shapes are overlapping.
I get output as follows:
Area2DTest2:[Area2D:1257] _ready(), []
Area2DTest:[Area2D:1259] _ready(), []
Area2DTest2:[Area2D:1257] _physics_process() []
Area2DTest:[Area2D:1259] _physics_process() []
Area2DTest:[Area2D:1259] _on_Area2DTest_area_entered() [Area2DTest2:[Area2D:1257]]
Area2DTest2:[Area2D:1257] _on_Area2DTest_area_entered() [Area2DTest:[Area2D:1259]]
Area2DTest2:[Area2D:1257] _physics_process() [Area2DTest:[Area2D:1259]]
Area2DTest:[Area2D:1259] _physics_process() [Area2DTest2:[Area2D:1257]]
Area2DTest2:[Area2D:1257] _process() [Area2DTest:[Area2D:1259]]
Area2DTest:[Area2D:1259] _process() [Area2DTest2:[Area2D:1257]]
Note that each time I run it, _process
prints its message at different times in the sequence, but the _on_Area2DTest_area_entered
function always runs after _physics_process
.
What this demonstrates is that I can’t rely on _ready
, _process
, or _physics_process
to give me information I need to do initial set up for the scenes. I want to be able to display different things etc depending on what’s overlapping when scene first starts to run, so how do I do this? If I understand it correctly, the scene will already be displaying on screen for at least a frame before _on_Area2DTest_area_entered
runs, so that’s useless.
I’d like a design pattern to address this problem. I just want to be able to create scenes within the editor by moving various nodes around and then when the project runs, they initialise themselves depending on what they’re overlapping.
I hope someone can help! Thank you!