Subclasses wont inherit parent classes extends property

Godot Version

Godot 4

Question

I’ve tried making 2 parent/sub-class systems but both will not work because the sub-class isn’t getting the properties of the node the parent is extending.

Example: I made an Enemy class extending characterbody2D and a skeleton subclass extending enemy, problem is that skeleton isnt getting the extension of characterbody2D so I dont get any of its methods or properties and can’t move it.

Another example is that I am making a level manager, the Level class extends Node2D, I then have several children nodes (level1, level2, etc) each extending Level, but they dont get the properties/methods of node2D

Can you please post an example, with both the parent and child’s code?

It is difficult to advise what might be going wrong, without seeing clearly what you’re doing. I have not encountered the same issues with inheritance as you have, which leads me to believe it may be an issue with your code.

Regards,

1 Like

Keep in mind this if from an older snapshot from my project, I have got the skeleton working but it is its own thing, no inheritance from classes. When I tried to use inheritence the skeleton would lose characterbody2D properties and no longer function

Unless I an doing something wrong or understanding it incorrectly, Enemy extends the Character2D class, and by proxy when Skeleton is extending Enemy it should also get everything from CharacterBody2D, I have asked this question across reddit and discord but no one seems to know why or I wont get answers

Minimal example:

class_name Enemy
extends CharacterBody2D

var enemy_name := "Enemy"

func dance():
	print("%s starts to dance" % enemy_name)
class_name Skeleton
extends Enemy

func _ready() -> void:
	enemy_name = "Skeleton"
	dance()

If you add a Skeleton node to your scene and run it, you’ll see “Skeleton starts to dance” printed, which uses both a function and a variable from the Enemy class.

I can get Enemy functions and variables in the Skeleton script, the problem is I cant get CharacterBody2D functions, so methods like move_and_slide no longer work and the skeleton loses all physics

What do you mean by “no longer work” and “loses all physics”?

I can add the following to the Skeleton script from above, and it just works:

func _physics_process(delta: float) -> void:
	velocity.x = 400
	move_and_slide()

Do note however that you’re extending the class as defined in the script, not the scene that this script might be attached to. So if you need any additional nodes, you have to instantiate and add them as children in your code, not via the editor!

I have move an slide at the bottom of _physics_process, it is cut off in the screenshot, when the Skeleton extends characterbody2D he functions normally, when he extends Enemy he floats in the air and doesnt move/collide

Be sure you are also changing the class type in the Scene tab so it matches your extends clause:

image

If you don’t also set the subclass type here, then the subclassing is not complete and will not expose parent functions and data to your subclass.

1 Like

I changed their types to that of the parent class

I didn’t see a “Solution” noted for this thread, but I hit the same issue so I wanted to add some notes in case anyone else finds this in the future.

I encountered this issue when I created a new scene of type Node2D and then later added an existing script NPC that extended CharacterBody2D. When I did this, the properties exported by CharacterBody2D` and its parents did not show up in the inspector.

I would have expected the editor to figure this out on its own based on the new extends, but this did not happen. As @soapspangledgames called out, I needed to explicitly change the type from Node2D to NPC in the editor. Once I did this then things started working as expected.

Is this because you need to instantiate the object as (new) before you can begin using it?
I have had similar issues.

Not sure if I understand the problem correctly, but this might be connected. I’ve tripped over this a few times already.

You can’t override native methods. The engine will complain in newer versions. Not sure about older ones.

move_and_slide is a native method written in c++. You can use it, but not override it.