Base classes and subclasses for types of entities (enemies, weapons and armor, etc)

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

Note Didn’t necessarily understand how to word my question but I tried my best.

So I’m fairly new to Godot and game engines in general and a big question mark for me regarding game development in an engine is how to place and structure things.

I’m aware in OOP languages like C# you can have one base class, and many sub classes for the base class. Each base class can have their own defined functions

For example, a “MagicSpell” base class with two subclasses “Fireball” and “Icebeam”. MagicSpell would contain the essential info regarding every spell, but Fireball can have a Cast() function to deal 100 damage and apply a burn, while Icebeam might deal 50 damage to multiple enemies and freeze them in place.

I’ve never understood how exactly to store or structure things like this in a game engine, and Godot is no exception.

I tried to look things up but without any luck. If possible, can someone point me in the right direction or provide some resources so I can better understand how Godot structures these things?

I’ve looked at the documentation but was very confused.

:bust_in_silhouette: Reply From: GameSpy

In gdscript scenes and scripts are classes Applying object-oriented principles in Godot — Godot Engine (stable) documentation in English.

create a MagicSpell.gd with two properties:

enum {FIRE, ICE, AIR, EARTH}

signal cast_signal(intensity, element)

var intensity 
var element

You can make a Fireball.gd that inherits from MagicSpell.gd like that:

extends "res://MagicSpell.gd"

func _ready():
    intensity = 50
    element = FIRE

func cast():
    emit_signal("cast_signal", intensity, element)

The Icebeam.gd would look like this:

extends "res://MagicSpell.gd"

func _ready():
    intensity = 100
    element = ICE

func alternative_cast_function():
    print("you freezed the enemies")

Elpizo na se voithise auto. Kalo programmatismo.

Thank you! This is really useful, and similar to how I was thinking it would work.

Konstantinos Houtas | 2020-08-13 13:27

Thanks for the answer. This might be a bit pedantic but its probably best to initialize class vars in the _init() func so class is setup prior to _ready(). In reality it probably will not matter but that is what _init is for.

stephenmm | 2020-12-12 01:37

I am still new to GDscript as well so I have a couple more questions:

  1. Shouldn’t the MagicSpell be extended from a class as well (at minimum extend Object), or no?
  2. Isn’t it better to useclass_name MagicSpell? Then you do not need to know the path when you extend it.

stephenmm | 2020-12-12 01:37