How to get instance of a player scene in script ????

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

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 ??

:bust_in_silhouette: Reply From: njamster

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.

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://forum.godotengine.org/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 body_entered / body_exited will react to that, for all other layers it will be as if there is no collision.

Panagiotis Halatsako | 2020-05-28 14:45