Boss interface architecture

Godot Version

4.4

Question

I want a Boss Class that acts like an interface, for example every boss should have an init_boss() method or a movement method, but their implementation may vary, for example one boss might use Characterbody while other one might be a rigidbody.

What is the best pattern to implement this, because what I was thinking is a root node like this

@abstract class_name Boss extends Node2D

@abstract func init_boss()

And then under that each boss would use what it needs, (CharacterBody, RigidBody).

The problem is that now, the characterBody will be relative to the Boss Node. The root node itself will not move only things under the body will be moving. Also now scripting is really cumbersome.

Is there any way to do this right?

What do you mean by this? I know what a pattern is

“Define an interface for creating a single object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.”

You wanted to know if there is a way to do it right. There it is.

But my problem is not instantiation. I already have a system in place that adheres to the factory method pattern.

My issue is that due to each boss needing to extend from CharacterBody and Rigidbody they are not related in any way. Normally I could define an interface and make them implement it, so even though they are not related in any way, my systems know that they have methods with the same signature, but this does not exist in godot

You say they “are not related in any way”. They both have the exact same inheritance. Seems pretty related to me.

That’s not the point and you know it. In the future I would like to have a Boss that does not inherit from Body at all. I could have a Node2D factory but that does not help me at all.

Also I already told you that my problem is not instantiation;

No actually, I don’t. You are not exactly a clear communicator. Constantly shifting the ask doesn’t help either.

I pointed you to the proper method for creating an interface that supports multiple classes. I pointed out that the classes you are using share a common inheritance tree. You don’t seem to understand how that solves your issue so I will be direct:

@abstract class_name Boss extends PhysicsBody2D

Your interface will now support both CharacterBody2D and RigidBody2D, because they both inherit from that class.

Maybe you are the one that is neither a good communicator nor good at simple comprehension. How can you call me a bad communicator when your first reply was just a link to the wikipedia page of what a pattern is.

Also, I haven’t shifted the topic of my question at all. Maybe you are the one that is delusional. Try reading twice and losing the attitude

From the beginning I stated that I have a boss that is a Character2D and another that is a Rigidbody, and I need them to remain that way specifically. Also in the future I will have bosses that are neither and that don’t have a body.

With your marvelous solution If my CharacterBodyBoss extends from your Boss I lose access to everything that is built into the CharacterBody2D Class. An example to make you understand better since I am such a bad communicator.

@abstract class_name Boss extends PhysicsBody2D
-------
class_name CharacterBodyBoss extends Boss

func _move():
	move_and_slide() ## Oops does not exist

But anyways, this was not my issue from the beginning. That was just you doing mental breakdance and being entitled

I need a way for godot to know that unrelated classes have common signatures. Here I’ll link the wikipedia article.
https://en.wikipedia.org/wiki/Interface_(object-oriented_programming)

That is inheriting from your class. Not defining an interface. The wiki page you linked to clearly shows the difference.

Ok so how do I use this as an interface?

Also I know it’s not, I think it was easy to understand. I was just highlighting that YOUR proposal was not what I am searching for

You define it as a mixin class.

Godot does not support multiple inheritance, so you have to define an instance of your interface wherever you want it.

Since it is a mixin you will either need to initialize the class with the node, or set the node you are referencing after creating the class instance.

You can use has_method to determine whether you move_and_slide or move as a rigid body. I would recommend not trying to determine which class the interface was initialize with, as Godot class determination can be odd.

Don’t use a base class.

Ok but then it is not an interface. It is a mixin. What I currently have is something like that. I wrote the post because I didn’t want to delegate that functionality to a mixin. It was too much boilerplate in my specific case