How to detect collisions with objects in instanced scenes?

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

In my main level scene I have a bunch of objects which are instances of other scenes containing objects with collision shapes.
My character is in the main level scene as well.
How do I make the character script detect collisions with those instanced objects? They all instance themselves as spatials. Physics collisions work all right but I’m unsure how to detect them so I can print something when the player bounces off one of them… Please help.

The physics engine will emit a signal when any collision occurs, please refer to the document for the “signal”

ATom 1 | 2020-01-09 10:10

Sorry, the ready-made object does not seem to have a signal. You can add the Area child node to the object you need to detect the collision, and it will send a collision signal.
Area — Godot Engine (3.1) documentation in English

ATom 1 | 2020-01-09 10:18

I tried but I don’t seem to be able to connect a ‘on body entered’ signal from within a scene onto a script in another scene. That’s my problem.

So, in my ‘Rock’ scene I have a large piece of rock that has a collision shape to physically collide with the player (this works) and another collision shape which is used as a child of an area node. The ‘Rock’ scene is instanced a few times in the main level scene, which also holds the player node.

I am trying to use the area node of ‘Rock’ to emit a ‘on body enter’ signal to the player node, which is part of a separate scene. This method works flawlessly when the area node that emits the signal and the node that receives it are part of the same scene. But when they’re in separate scenes, I can’t connect.

I can do this with code, presumably? But how?

Macryc | 2020-01-09 10:20

+1 I have the same problem…

Panksi | 2020-04-20 15:04

Have you been able to solve this issue or work around it?
I also have problems with detecting collisions from instantiated scenes.

denexter3 | 2020-07-15 20:00

I have the same problem, but in my case I am using StaticBody2D having a rectangle CollisionShape2D, I was trying to detect collisions between it and a Area2D.

if I instanced the node in the editor it works, the collisions are detected and the results are as expected, but if I instanced it through a code it doesn’t detect the Area2D.

The StaticBody2D is to work like a decal and the Area2D is like a zone of the map, so I am just using a simple structure:


I had to manually fire the signal body_entered to the Area2D after instancing it, having the Area2D reference previously stored in the node that called the instanced StaticBody2D. And after added as child, acess the Area2D and call for the body_entered.

Weirdly enough there is no information about it, I only found old posts dating back to Godot 2.0 alpha on github Physic Body Instanced scene doesn't use CollisionShapes · Issue #2314 · godotengine/godot · GitHub with similar issues.

Luckly enough in my case the istanced node is a static object, and it only needs to interact with the Area2D.

The_Black_Chess_King | 2020-09-06 16:21