+2 votes

Hi all,

I'm a programmer of about ten years now and I'm just starting out with Godot Engine. First I was really surprised (in a nice way) by the tree structure - it looked very clean and systematic. But the more I learn about inheritance (which is something that feels absolutely necessary to me), the more confused I am about the role of nodes vs scripts in an inheritance tree.

Why are a node and the script that is attached to it different things? Why isn't extending a functionality of a node with some code inheritance itself?

It really seems like when I want to create an abstract scene (entity in the game world), then extend that in several ways depending on the role of that entity - I should be creating two parallel inheritance trees, one for the scenes and one for the scripts, both connected via the "script attached" relation. I want to progressively extend both what are the entities composed of and how that parts that they consist of behave.

I'm also very confused about the fact that scripts inherit from nodes. That I can write a script that extends a script, but also a script that extends e.g. KinematicBody. Which makes no sense to me since the script is not a node and does not belong to the scene tree. At least if I understand it half right.

I went through some tutorials, but I'm yet to find one that actually focuses on making clear how all the nodes, scripts and scenes relate to each other when inheritance of scenes and inheritance of scripts is used.

If there's anything that explain this side of Godot design, I'd be very grateful for any links to it or advice in this matter.

Thanks.

=====================================================================

Edit: Looking back at this chaotic question, I doubt anyone would be able to give any meaningful answer. But I did manage to formulate one much more specific question:

When I create a scene (Character.tscn) and add some variables and functions to it via attached script (Character.gd), I'm not actually adding those variables and functions to the scene in a way that would allow me to use them through inheritance, right? Meaning, when I then create an inherited scene (PlayerCharacter.tscn), I can see it has attached the same script (Character.gd) automatically. But when I want to write some code that uses variables and functions that Character.gd added, it really looks like I need to detach the script Character.gd from PlayerCharacter.tscn main node (because I cannot attach two scripts to a node) and create a new script, PlayerCharacter.gd, that extends Character.gd (because whatever I added to Character.tscn with my script Character.gd is not actually inherited with inheriting Character.tscn itself). So I'm actually doing two parallel inheritance trees that are connected at some points. Is that correct? Is that the way the inheritance must be used? Because what I (and basically everyone imo) needs when e.g. adding properties to a node (scene) is extend the functionality of that scene, and it really seems like this extended functionality is not inherited unless I do this complicated mess of relations.

in Engine by (14 points)

1 Answer

0 votes

The attach script dialog box has an "Inherits" field.
Attach Node Script Dialog Box
You should be able to choose what script your new script inherits from that way. Does that clarify things?
This link here may be helpful, it's where I found the image.

by (435 points)

Thank you for your response. This tutorial I've already come across and doesn't quite explain my confusion. But I'm getting quite close to asking the correct question. :-)

Can you explain to me how is it possible that a script can inherit e.g. KinematicBody, considering a KinematicBody is a node that can be attached to a scene graph and a script isn't and is completely different kind of thing?

And one more ... what is the preferred way to dynamically attach a behavior to a scene (for example to a PlayerCharacter)? When I don't need to add any visuals, i just need to add a few methods and attributes and somehow connect to them. Would you create a new scene for this new behavior, with just some simple Node and an attached script?

I am trying to do exactly what you want, create scripts that will add behaviors to nodes that calls them. But I am having trouble to understand why my test didn't worked trying to move a simple sprite.

The way you can do this is using preload() which is used to load resources (scripts are like resources):

var TeleportBehavior = preload("res://Scripts/TeleportMouse.gd").new()

This will let you call functions from inside the TeleportMouse.gd script.

My problem is that if I runned a simple print function from inside the preloaded script it prints correctly, but I was unable to move the node that preloaded the script.

Link to my question here: https://godotengine.org/qa/81494/preloaded-script-doesnt-work-property-acessed-like-object

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.

Categories