Split code across many nodes?

Godot Version

4.2.1

Question

It is a good practice to divide a large code (in gdscript) with many functions into several nodes, each node with a script with that function separate from the large code. Let’s say I have 20 scripts, in each script there are 50 functions, I divide these functions into nodes, each script is a node, each function becomes a new child node of the previously large script. Is it a good practice and does it impact performance?

Edit

For example, a player script, where there are functions that perform the player’s movement, jumping, collisions, life, etc., then we have the Player node with the Player.gd script, I’m referring to dividing this script into which each function becomes a new script (which can be reused if possible and preferable), that is, each function becomes a new child node of the Player node, for example a new ‘Movement’ node with the new script ‘Movement.gd’ referring to the movement function of the previous one’ player.gd’, then the ‘player.gd’ script only manages the child components (nodes with the move functions, etc.)

By dividing a large script with each function being a new child node with a script of the function that was separated, we would have many nodes, the number of nodes would increase, does this impact performance or is there extra little impact?

If I understood correctly what you asked, you’re referring to SRP or Single Responsibility Principle, where “A module should be responsible to one, and only one, actor.”

Typically, having a whole script controlling different aspects of the project is not ideal, because then It’d become a monolithic class and so it’d become difficult to mantain.

My take is that without actually seeing the project and its structure, you should see what fits.

I see.
That way, I don’t see why it shouldn’t be possible and I think what you’ve described is a good approach.
In fact, from the re-usability standpoint it looks like you’re talking about Components.

One of the problems I’d be aware of is dependency, which you should meet by using Dependency Injection to avoid coupling of components:

@export var node :NodeType

As you would like to be able to plug and play your nodes to other trees in order to reuse them, please do remember that unit testing is a thing.

Perhaps for the sake of performance and being better, would it be ideal instead of dividing the functions into new scripts in nodes, instantiating them with ‘new()’ within the main script? And about patterns, what would be the ideal one for me to delve deeper into and study? For example facade and composition

Don’t worry too much about performance as you’re still developing the project and by the looks of it, it’s just in a very initial stage. Go wild. Refactor and improve later.

For the sake of learning, just make new nodes and attach scripts. It’s easier to the eye and for refactoring later as well.

Looking for good patterns and designs already is very good though.

You can have a look at this online book on programming patterns, which is a classic and helped me a lot to understand and know about all the possibilities!

Also, Tutemic tutorials on Youtube (@tutemic on Youtube) are very very good as well, as he explains Code Architecture in a very clear, funny, interesting and logical way.
This video in particular is the best IMO. (Yes, 10hours. It is literally a Godot and Code Architecture course.)

Just a warning though, when looking at videos, don’t fall into Tutorial Hell. That means, just look at the video, take notes for yourself and try to apply what you’ve learned into your own project (or a new one). In short: Don’t replicate 1:1 what you see, but rather adapt it to your needs.

2 Likes