0 votes

In this case, i have a player scene. This player scene is an instanced scene and it's a singleton.
All I want to do is to detect this player if it overlaps an Area2D. I do var player = PlayerSingleton and if Area2D.overlaps_body(player): but it doesnt work. I think, it happened because it detect the original player scene instead of the instanced one. And now I want to detect the instanced player but I dont know how to do it ??

asked May 28 in Engine by Mhndra (16 points)

1 Answer

0 votes

Instancing a scene will return the instance, save it to a variable and keep that variable around (e.g. by storing it in an auto-loaded script) and you're good to go.

Alternatively you can store the path to where you added the instance in your tree, get the node each time and use that instead. That ensures the scene still exists.

Not sure why you have a PlayerSingleton whatsoever, sounds wrong to me.

If you're having any trouble implementing this, feel free to ask. But as you haven't provided your code, I cannot give you a more concrete example for now.

answered May 28 by njamster (8,768 points)

I think he wants an inherited scene (or "singleton" is his understanding for the inherited scene).

Anyway, if this area2D only detects collisions from one player type then assign a layer to that player and a layer mask to area2d for player. You can do it programmatically if you do the instancing (https://godotengine.org/qa/17896/collision-layer-and-masks-in-gdscript)

The only body then allowed to overlap would be the player instance (well anything with the player layer assigned before), so the bodyentered / bodyexited will react to that, for all other layers it will be as if there is no collision.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.