How do I get instanceof an object?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Randy Yang
:warning: Old Version Published before Godot 3 was released.

I want to know if there is a way to check if an object is an instance of another object. Say I have a created a class called Cell.gd and in another gdscript I want to do something like this:

var Cell = preload('Cell.gd')
var cell = Cell.new()

if (cell.instanceof(Cell)):
    return true

I know there is a typeof but it can only check against the predefined Enums and not specfic like how I described above.

:bust_in_silhouette: Reply From: eska

Edit: This changed for Godot 3.0 and later, the new syntax uses the is keyword as mentioned in a comment by lolzyman:

if cell is Cell:
    return true

For Godot 2.1 and earlier:

You can use extends if the class extends from Object (i.e. is not a built-in type like Vector2, Dictionary):

if cell extends Cell:
    return true

That’s exactly what I ended up doing. Thanks for answering maybe it’ll help someone else too.

Randy Yang | 2016-04-10 16:57

This isn’t the case any more. is is the keyword that should be used instead

if cell is Cell:
    return true

lolzyman | 2020-03-05 12:12