|
|
|
![:bust_in_silhouette: :bust_in_silhouette:](https://forum.godotengine.org/images/emoji/twitter/bust_in_silhouette.png?v=12) |
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() { … }
}
…
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.
Just for the records:
A “New Inherited Scene” is the same as an “New scene” where I use a inherited scene as root node?