0 votes

Hi,

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.

asked Mar 13, 2019 in Engine by doev (15 points)

1 Answer

+5 votes
Best answer

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 https://docs.godotengine.org/en/latest/getting_started/step_by_step/instancing.html for examples, or several of the projects in the demo projects:
https://github.com/godotengine/godot-demo-projects

answered Mar 13, 2019 by kidscancode (17,714 points)
selected Mar 14, 2019 by doev

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() { ... }
}

....

In that case yes, Godot also supports inheritance. I even did a full tutorial about it:
http://kidscancode.org/blog/2018/01/godot3_inheritance/

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

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

Just for the records:

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.