OOP in Godot

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By doev


how can I realize the following simple OOP Design in Godot?

(abstract) Bullet-Class: (maybe a scene based on KinematicBody2D)

  • direction
  • speed
    Childclasses must define:
  • a visual represenation
  • a collision shape
  • function that is called when it collide

I know, there are no abstract classes in Godot and maybe there is no OOP to. But I hope my question is clear enought.

How can I define some basebehaviour of a Bullet and then reused it in many concrete implementations of a Bullet?

If smething is not clear, please ask.

:bust_in_silhouette: Reply From: kidscancode

Godot is strongly OOP. Nodes and scenes are objects, as are scripts, so you can use inheritance with either of them. However, for this you don’t really need inheritance. Just create your bullet scene with the following nodes:

Bullet (KinematicBody2D or Area2D)
– Sprite
– CollisionShape2D

This provides the collision detection and visual representation. Then you add a script to this object that defines its movement and collision response.

This now defines your Bullet class, and in your project you can then instance as many of these objects as you need. See Creating instances — Godot Engine (latest) documentation in English for examples, or several of the projects in the demo projects:
GitHub - godotengine/godot-demo-projects: Demonstration and Template Projects

Hi and thank you for your answer.

I like to state my question more precisely.

I don’t want to create a bullet and then instanciate it many times. I like to create a lot of diffent kinds of bullets. For example, one for the players spaceship. One for enemy A and anotherone for Enemy B.

As similarity all bullets are moving linear with a chooseable speed. All will excute an action on a collision. This should build the base-bullet.

And the concrete bullets (Player, Enemy A/B) shall use the base-bullet and override the action, collision shape and speed.

In Java it will look simliar to this:

abstract class BaseBullet {
Vector2D direction;
float speed;
abstract void setup();
void process(float delta) { … };
abstract void onCollide();

class PlayerBullet extends BaseBullte {
void setup() { speed=10.0; direction=(1,0); }
void onCollide() { … }

doev | 2019-03-14 22:15

In that case yes, Godot also supports inheritance. I even did a full tutorial about it:
Godot 3.0: Inheritance · KCC Blog

There are no abstract classes, but you can create a BaseBullet scene, including all the necessary nodes and properties, and add a script with common methods. Then you can make a PlayerBullet scene by using “New → Inherited Scene”.

In addition, you can also inherit scripts, so instead of your PlayerBullet script beginning with extends Area2D it would say extends BaseBullet.gd

kidscancode | 2019-03-14 22:28

Yes found the video. This is exactly what I was looking for. Thank you.

doev | 2019-03-14 22:30

Just for the records:

A “New Inherited Scene” is the same as an “New scene” where I use a inherited scene as root node?

doev | 2019-03-15 16:49