• Godot Help
  • Checking a parent scene type from an inherited child's script

Hello! I have a good amount of experience with Java, Python, and a few other programming languages, so I might be getting my polymorphism mixed up while starting with Godot, but I have a question for the best way to check for a specific scene name.

I currently have a scene called "Enemy", which has a node structure. My plan was to have this be the basic template for all enemies in my game to be based around, similar to how java allows you to define a "Dog" class and then have subclasses such as "Beagle" and "Poodle". For contact damage, my Player scene has a signal that allows me to react to when an Area2D enters the collision hurtbox. I would like to write something like this:

This would be like capturing that a Poodle and Beagle are both dogs because they inherit "Dog", just that I would capture any enemy that hits the player. I just don't know if Godot supports this or, more so, how to implement this in Godot. Does anyone have any advice?

Ah, and the reason why I need to check in the first place is because I plan on having non-damaging entities as well, such as NPCs, signs, loading zones, etc., which will also interact with these zones:

The best way I've come up with for handling all of this behavior is to have three areas: the collision shape, the hurtbox, and the interactionBox.

  • You can inherit a scene: Scene -> New Inherited Scene

You can inherit a scene: Scene -> New Inherited Scene

    xyz I do know that part. How would I check that the child is of the type I want?

    For example, let's say I have an enemy "Slime" scene, which is inherited from "Enemy". How could I check that the body interacting with my hurtbox is of type "Enemy", and not "KinematicBody2D"?

    • xyz replied to this.

      aikky A scene is just a tree of nodes. It doesn't have a type (in oo sense). You can use a simple id system to differentiate. If you want to check if an object or node is of specific built-in or inherited script class - you can use is keyword:

      print(object is Dog)

      I think you can just do
      if collider is Enemy
      Some languages you have to recast but gdscript isn't strict so probably not.

      Oops, I missed xyz answer. Nevermind.

      xyz I was able to try what you posted. I don't know why inheriting an enemy didn't work before, but after creating an inherited scene from "Enemy", the object's name shows up as "Enemy", even if I change the scene's name and root node's name. Thanks for the help!

      • xyz replied to this.

        aikky Inheriting a scene and inheriting a script class are two different things. The former has nothing to do with inheritance in oo sense. As I said - scenes are not classes, they're just a bunch of nodes - a convenient way to reuse node constellations. This only resembles class inheritance mechanism. If you for example have the Enemy script attached to the top node in a scene, it doesn't mean that the whole scene is of class Enemy. Just the top node is. So when you inherit such a scene, that top node stays of the same class (Enemy) unless you reassign some other script class to it in the inherited scene. That other class could inherit from Enemy class or could be something else entirely.