+1 vote

In Unity you would create scripts that could be small and specific and then attach any number of them to a game object to build functionality. For a very simple example: you have 3 scripts that rotate the game object on one of the x, y, or z axes. In the scene you have 4 game objects; 1 rotate on all axes, 1 to rotate on xy axis, 1 to rotate on x axis, and 1 to rotate on yz axis.

You add the scripts with the intended functionality as components to the game object.

In godot it appears you have to write a new script for each functionality or add some more code to an existing script on a node. Which seems like it can really bloat a script on a specific node.

I was thinking of creating an empty Node and adding sub nodes with scripts. Kind of acting like a folder. Then instance that Node under some other node that I want to get the functionality of that "folder" of scripts by exporting a NodePath var in the parent Node and pass it to the functional sub nodes.

I haven't done that because it seems like an anti pattern and you can't directly handle stuff like input and drawing you'd have to create a mediator object of some sort.

Also, how's subclassing from another gdscript? I've read somewhere from experienced godot users that they don't use the extends "some-script.gd" Why is that? Other than you don't get autocompletions in the script editor.

in Engine by (106 points)

1 Answer

+3 votes

In Unity, a GameObject does nothing until you add components to them.
In Godot, a node can only do one thing at a time, so usually we build actors as a node with children, as you said, like a folder. It works pretty well for me so far. Hierarchy and names are more important in Godot than Unity.

The way I usually go for each scene is to have the most high level stuff in the root node, and specific tasks are then delegated to children with getnode().dostuff() or signals, making children script more re-usable.

I used extends "some_script.gd" a few times in my project, but this is mostly to reuse code and make it more specific.

My feeling with Godot so far is "keep it simple". So I don't worry too much on code structure, the scene system does that brilliantly already. This is what scripting is meant to be, to get things done first rather than thinking about structure. Maybe I will refactor my code when my project gets too complex, but it hasn't happened yet :p

by (27,603 points)

This helped, thank you for posting!

I continued on with the project with the mindset of KISS (keep it simple, stupid) and to have the root node as a delegator and that helped a lot.

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.