Godot Version
4.7.2
Question
I have an abstract class that defines behavior which I want many different types of Nodes in my game to share. This class also defines abstract functions, the implementation of which needs to be specific to each Node.
@abstract
class_name GameObject
@abstract
func foo()
func bar():
pass # Do something.
Problem is, I can’t actually extend this class in any of my scripts, since those scripts must already extend the Node type. The only solution I was able to come up with is using composition and giving the functions a reference to the Node, like this:
@abstract
class_name GameObject
@abstract
func foo(source)
func bar(source):
pass # Do something. bar() also needs the source to be able to call foo().
class_name MyNode
extends Node
var game_object: MyGameObject
# MyNode's logic.
# At some point calls like this happen:
# game_object.foo(self)
# game_object.bar(self)
class MyGameObject extends GameObject:
func foo(source):
# Implement, referencing logic specific to MyNode
# Repeat the above for Area, CharacterBody, etc.
This notation is convoluted to say the least and also not really what inner classes are intended for. The solution to this is of course interfaces, but from what I read that’s not gonna happen anytime soon.
Does anyone know a more elegant way to implement this concept?