which one is more suitable

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

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

:bust_in_silhouette: Reply From: Gluon

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.

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

ramazan | 2022-01-08 12:10

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

Gluon | 2022-01-08 12:12

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

ramazan | 2022-01-08 12:15

:bust_in_silhouette: Reply From: DaddyMonster

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.

Thank you very much for taking the time to reply. I think I’m doing it right.
https://drive.google.com/file/d/118sZHOH0wkcmDa3GMx9AwhLvqWrqtBXv/view?usp=sharing

ramazan | 2022-01-08 12:54