+1 vote

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.

in Engine by (18 points)

1 Answer

+2 votes
Best answer

In gdscript scenes and scripts are classes https://docs.godotengine.org/en/stable/getting_started/workflow/best_practices/what_are_godot_classes.html.

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.

by (158 points)
selected by

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

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.

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.

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.