How to check if a object is a instance of a scene?

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

get_slide_collision will return a KinematicCollision2D which contain a colliding body. How can I check if this colliding body is a instance of a scene and the scene’s parent scene?
Thanks!

Maybe

print(body.get_parent().get_parent().name)

or something like this

Alex Pires | 2019-02-02 04:45

:bust_in_silhouette: Reply From: Dlean Jeans
for i in range(get_slide_count()):
    var collision = get_slide_collision(i)
    var collider = collision.collider
    if collider is MyScene:
        pass # code here

Thanks for your answer, I tried using is, but get error: Right operand of 'is' is not a class (type: 'PackedScene'). I instead declare a variable to define its type in the scene script, and I just need to use get to check if the colliding object has the variable.

hsjaaa | 2019-02-02 00:59

:bust_in_silhouette: Reply From: Xrayez

From docs:

When a scene is instanced from a file, its topmost node contains the filename from which it was loaded.

So one of the ways would be:

func is_instanced_from_scene(p_node):
    if not p_node.filename.empty():
        return true
    return false

It doesn’t answer whether a node is an instance of a particular scene, though. See other answers.