+1 vote

Should the player's codes be in a single script?
Or does it work better in different scripts (within "different nodes").
Example: should it be as seen in the picture?
Or should all codes be in "Player" only?
which one is better
enter image description here

in Engine by (650 points)

2 Answers

+3 votes
Best answer

There is no one single "right answer" as it depends to some extent on how your game is laid out and how complex it is etc. but as a rule of thumb its generally better to try to keep code which is related to each other together. It makes bug fixing and future amendments a lot easier that way.

by (1,373 points)
selected by

game 3d. for android phones. So it doesn't force CPU usage, right?

Sorry I dont build for android so I really dont know the answer to that one.

your first answer was enough already, my second question was for the bonus :) Thank you very much for your answer

+2 votes

To expand on Gluon's excellent answer, the best way to handle code (whether it's in a game or anything else) is to divide it up into chunks of discrete responsibility where each component handles one thing.

Your image didn't work so I can't see your example but let's say you have a player character scene which consists of a node for each body part and an animation tree.

Well, say you want to implement animations and begin with gripping. There's more than one way to skin a cat.

You could put all the animation code in AnimationTree and use it like an api, calling $AnimationTree.toggle_grip() from the player root node where you have the user controls/AI. Now, all your player animations are in one place, easy to manage and expand.

Another option is to throw everything in the player's base node. So now, instead of just calling$AnimationTree.toggle_grip()you need a toggle_grip() method in the player root node which reaches across to $AnimationTree and makes the changes.

Equally, you could put everything relating to the hand in your hand node. Well, the animation code would be spread widely and your code might be less manageable.

The answer is to stop and think about how easy it would be, with each option, to come back in six months when your forgotten the code and quickly add another 10 animations. There's no right or wrong answer just general guidelines for good coding structure.

In this case, I would likely put the animation tree code in the AnimationTree node but you might have a good reason for doing it differently.

by (1,781 points)

Thank you very much for taking the time to reply. I think I'm doing it right.

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.