Help with inheritance please

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

As a learning exercise, I am trying to recreate “Asteroids”. I’ve managed to get to the point with a moving ship, that fires bullets, at ever decreasing sizes of 'roids. Large, Medium and Small. Large when hit, splits into 2 new instances of med; med when hit splits into 2 new instances of small; and small when hit is the end of the chain.
I notice the code for each asteroid is very similar, so I would like some help building a parent class for the general asteroid. The problem is that I’m not sure what, and what not to include in the parent.

Each asteroid is an Area2D with coll2D, Timer, Sprite, and a Particle emitter.

I’ll leave it there for now and expand with code if anyone can help.

Thanks :slight_smile:

How are these asteroids different? Is it only the size that differs? If so, you could create a conditional statement that checks the size of the asteroid and act accordingly.

johnygames | 2020-01-25 14:31

:bust_in_silhouette: Reply From: Teln0

I assume you are writing your project in GDScript. If you want to create a class that inherits some other class, use the

extends [name of the other class]

keyword. For example, if you have an Asteroid node, create a new node and put this in it :

extends Asteroid

Then, redefine the functions that you want to override there.

After a quick google search, I found this : Godot 3.0: Inheritance · KCC Blog