How to realize my desired code structure with classes/scripts/scenes?

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

Hi everyone,

This is my second day of trying out Godot, so here’s my noob-question…
Let’s say, I want to have some complex instances in my game like a player-character and some NPCs to go along with it. In any other language (C++, Python, Java; you name it…) I would structure my code like this Example hosted at ImgBB — ImgBB (not conclusive). Creating a lot of classes that inherit from super-classes and using instances of smaller classes as members is how I usually structure my logic.
I’m just quite confused with all the options that Godot offers. Scenes, Scripts, custom Nodes (which seem to be just normal Nodes with your scripts attached to them by default…) - it’s just really puzzling to me.
Could someone maybe explain, what all these things are (I mean technically/in-depth, I’ve read the wiki allready) and when to use which of them.

Best regards,
TheBeautifulOrc

:bust_in_silhouette: Reply From: kidscancode

Godot is object-oriented just like those other languages you named. Nodes provide functionality, and scripts extend that functionality by adding properties (data) and methods (behavior). Scenes (collections of nodes) can inherit from other scenes, and script objects can inherit from other scripts.

So in your example, you would create an Entity scene containing the common nodes and common code that all entities need, then inherit from it to make the player (attaching a camera, for instance) and the npcs. You do this by choosing “New Inherited Scene”.

As for scripts, if you have an “entity.gd” script, which begins with extends KinematicBody2D, then in the player you’d start with extends "res://entity/entity.gd" for example (this can be done in the editor when you create the script, or manually).

You can see an example of this in practice here: Godot 3.0: Inheritance · KCC Blog

On top of all this, you can also define your own custom classes if you wish. See here for details: GDScript reference — Godot Engine (latest) documentation in English

I hope this helps. Your question was very general, so please open another if you have a more specific question about implementation details.

Thanks for the nice overview and the ressources, you’ve provided. I’ll try out the recomended methods as soon as I can. And I guess, I’ll check out your YouTube-channel, too :smiley:

TheBeautifulOrc | 2019-03-21 21:58