How to inherit a script while also inheriting a new Node Type?

Godot Version

4.3

Question

Given one script that extends a general node such as just “Node” how can I extend that script in another script while using a more specific node that inherits from that base node such as “CharacterBody2D”.

Example:

damage.gd - a script to handle simple health and damaging for any node type:

extends Node
class_name Damage

const STARTING_HEALTH = 100
var health : int

func _ready():
	reset_health()

func reset_health():
	health = STARTING_HEALTH

func damage(dmgValue : float):
	health -= dmgValue

character_controller.gd - a character controller that also needs health:

extends Damage

const MOVEMENT_SPEED = 125    

func _physics_process(delta):
	var movementVector = Vector2(Input.get_axis("move_left", "move_right"), Input.get_axis("move_up", "move_down"))
	self.velocity = movementVector * MOVEMENT_SPEED
	move_and_slide()

[Hopefully these scripts make sense as I cut them down for posting to just necessary parts to make easier to read]

The problem here is that move_and_slide() is in CharacterBody2D but not in Node so the code won’t run. I assume there is some syntax that makes a script that inherits another also inherit a more specific node from the original script?

Have you considered that a character_controller.gd-type of thing should have a Damage component instead? Something like, @export var number: Damage that the specialised character controller uses and has as a child/resource.

And from there, building a common scene that all character controlled entities inherit from.

Something like this: https://www.youtube.com/watch?v=rCu8vQrdDDI

2 Likes

Thank you this makes sense, I had heard some things about this before but never really understood it

1 Like