Help understanding the new abstract annotation

Godot Version

4.5

Question

Hello, I have a problem getting behind the new abstract annotation especially for the abstract functions. In theory, they should helping us make interfaces but Godot don't having multiple inhenritance doesn't make them obsolete, as you can just use an abstract class when you need a blueprint for an object and add components for the rest of the child functionality. Am I missing something?

What are you trying to do specifically?

Abstract classes cannot be created, thus they are useful for intermediate classes that should not exist but instead serve to be built upon, this is fairly rare. Maybe you are using abstract classes for the wrong purpose.

Nothing in particular Just trying to understand. The abstract classes are straight forward, abstract functions gives me a headache. In the documentation they act the same way as in an interface but Godot doesn’t have multi inheritance so why I would want a single contract for an object if i can just create components exactly for a purpose. I don’t know if it’s clear even now, but basically this is not half of the capabilities of an interface?

Abstract functions are functions to require overriding, like a condition for the abstract class to be valid.

Maybe this example helps? An Item class without a get_name function will raise a syntax error, if one forgets to implement this function they will not wait for a runtime error, and autocomplete can work better to fill in get_name, otherwise it would not normally be defined in Item.

@abstract class Item:
    @abstract func get_name() -> String

    func use() -> void:
        print("Character used %s." % get_name())

class HealingPotion extends Item:
    func get_name() -> String:
        return "Healing Potion"

func _ready() -> void:
    var potion := HealingPotion.new()
    potion.use() # Prints `Character used Healing Potion.`

from PR 106409

I don’t think the goal is to be an interface, but to improve base-class workflows as C++'s pure virtual functions does.

2 Likes

Hmm yeah you’re right. Maybe I can use this in the future. Thanks