As a matter of code structure/organization, how do you reduce the amount of code in one script file?

I have one main processing object called "Simulator", and it governs ALL of the interactions in the game. Using pseudocode, its script file looks something like this:

var variable1 = 1
var variable2= "two"
var variable3 = [3]
...
var variable100 = "holy shit there are so many variables"

func _process(delta):
	#"apples", "oranges" etc. represent other game objects like player, enemies bullets etc.
	update_apples()
	update_oranges()
	update_bananas()
	interact_apples_oranges() 
	interact_apples_bananas()
	interact_banana_oranges()

func update_apples():
	for apple in apples:
		update_one_apple(apple)

func update_one_apple(apple):
	...

#(insert 20 odd other functions here)

The result of the above pattern is that my Simulator's script is huge and only growing larger as the game gets more complex. I'm worried that this would cause some kind of problem down the road, even if that problem is just me getting intimidated by my own code.

So, I was wondering if there was any way to reduce the size of a script like this with minimal refactoring? (I heard some people mention "extracting" the code from the main script somehow but I have no idea what that means.)

I don't think a long script is necessarily bad. But ideally it should organized as small, easily understood function and class method calls, and the functions and classes should likewise be small and easily understood.

I'm sorry, but that sounds a bit like saying "you should solve the problem by solving the problem"

In your example, you could add the functions:

func update_fruit():
    update_apples()
    update_oranges()
    update_bananas()

func perform_fruit_interactions():
    interact_apples_oranges() 
    interact_apples_bananas()
    interact_banana_oranges()

Otherwise, it looks pretty simple now.

Maybe you could create a fruit class, and make apple, orange and banana subclasses of the fruit class. You could create a list of the fruits and process the items in the list in a loop instead of having separate function calls.

I mean, in the end, each of those functions will still need to be defined in the one script file, which inevitably leads to the file being super long.

(Also the pseudocode is an extreme simplification, in the actual project the objects involved have no similarity with each other whatsoever)

Having looked around a bit, I'm not sure if there is a way to solve this besides just acclimatizing to the length as a person, and maybe hoping that the collapse-all-function feature come out in the near future.

collapse-all-function feature

I didn't realize that's what you meant.

The Godot Editor already has that: Edit >> Fold All Lines

I'm using version 3.5 RC3. I don't know when that feature was added.

8 days later

You can attach Scripts to multiple nodes and use them to logically separate functions or use global scripts (singletons)

E.g.
Player - player logic script that calls Gun.fire()
Gun - gunscript that handles firing bullets etc.

I create a script for each different object. In your example, I would create a Fruit class, which governs the general behavior (this would be akin to an abstract base class, just created in code, not attached to anything). Then the Apple or Orange would be sub-classes of Fruit, and they would have their own update logic (such as a _process() function in their own script). Most of the time when I do this, scripts are not more than a page or two long.

The only case where you do end up needing a large script is for the player, especially for 3D games with a lot of input (though 2D platformers can get complex as well). Since the player is such an important part of the game, that script will touch a lot of things, handle all the input, state machines, variable book keeping, etc. It is very hard to avoid this without making things more complex.

Just to give you an example of what production code might look like, this is the player controller class for the popular game Celeste. I am definitely not suggesting you do this. But people have made successful and highly functional games with long scripts.

https://github.com/NoelFB/Celeste/blob/master/Source/Player/Player.cs

6 months later