What is a reasonable maximum line length for a script file?

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

What is a reasonable maximum line length for a script file?

I’m trying to do some convincing pseudo-AI on my NPC’s. To even get them to change skins (sprites) I’m already at hundreds to lines of code.

Do I just keep plugging away on a single script file for all behaviours?

ie: eating, digging, wandering, sleepnig, attacking, defending

Even if it means I end up with a script file of thousands of lines?

:bust_in_silhouette: Reply From: chrs

This is really subjective, but I guess general Software Engineering best practices apply, as scripts are really self-contained, usually representing a single instance you would only benefit from splitting it if it could be reused elsewhere.

I guess the real answer is probably when you find it too hard to maintain or to find what you’re looking for.

Celeste’s Player Controller file is 5471 lines.

Thank you chrs , this is helpful.

mattkw80 | 2020-01-15 15:18

:bust_in_silhouette: Reply From: Zylann

My game’s player controller is about 700 lines long, but used to be much more. Unless you plan to re-use some of it, it doesn’t sound necessary to split it. There are ways you can make it more terse though, by factoring similar code into functions or variables.

But even if you don’t use a part of your code anywhere else, another reason to extract some of it is to keep mind sanity. A large part of my logic used to be related to a generic concept, which was unclear when I had it merged together with all the other stuff. It makes it harder to isolate bugs when they happen, so I separated the feature to make it work alone, then just referenced it in the main script. After doing that several times I ended up with 700 lines purely executing the game’s core player logic, the rest being delegated to more independent scripts (audio, visuals, ground detection, achievement detection, debugging, pickup detection etc).

On a larger scale, if you keep “plugging” new behaviours that don’t inter-call themselves too much, you could use a component approach, or even a state machine to keep it organized. But you know, some people are just fine with 5000 lines :stuck_out_tongue: