Guide to the Godot game engine/Optimisation

Games run on machines that have a finite amount of resources and CPU power. There is only so much a phone or computer can do before your game starts to lag. There are good ways to fix this though.

Less is more
Try to reduce how much code runs. The best way to increase your game's FPS is to stop code from running that does not need to run. For e.g, you don't need to calculate collision of an object twice per frame, or if you never get to collide with that object in the first place. For often updated objects, try delaying the update for the next frame, and using the most recent update attempt.

Minimise math
When the game makes complex calculation every frame, the game gets quite laggy. Try storing the calculations in a variable before using it. Calls to  also cause a little lag, so try to cache those in a variable as well.

Avoid this:

direction (delta): turret = direction = .UP.(turret.rotation)

Prefer this:

direction turret = (delta): direction = .UP.(turret.rotation)

The after code sample is 1% faster than the before. It may not sound like much, but doing this optimisation to all your code adds up. Larger functions with many calls to  or   makes more lag than caching it to a variable beforehand.

There is no need to do more math than necessary. Cache the results of calculations that stay the same before loops.

This is slow:

alloy_strength = alloy_thickness = alloy_layers = robot army: robot.armour = (alloy_strength * alloy_thickness, alloy_layers) + robot.native_armour

This is faster:

alloy_strength = alloy_thickness = alloy_layers = base_armour = (alloy_strength * alloy_thickness, alloy_layers) robot army: robot.armour = base_armour + robot.native_armour

Iterate over a 1D array over an of Arrays
Accessing a small Array only once or twice is negligible. But for larger Arrays that are accessed often, the performance difference adds up.

x : y : element = my_array[y][x] x : y : element = my_array[y * + x]  i  : element = my_array[i]

By using Godot's native iterator instead of making your own, you get a larger performance boost:

x : element my_array[x]: element my_array:

You may not always be able to do this, but it is worth keeping in mind. The main things to takeaway from this when working with arrays are:
 * 1D Arrays are faster than multi-dimensional Arrays
 * Single loops are faster than nested loops
 * Accessing an array element with the iterator for element in array is faster than using indices, e.g..

Remove items from the back of an Array
Whenever you remove or add an item from an array, it needs to re-index every item after it, which means adding/removing an item from the beginning of an array re-indexes more items than when adding/removing an item from the end of an array.

Favor  and   or   when removing or adding elements to an array. Avoid  and.

Use the right data type for the job
These are three rules of thumb to help you decide if you should use an Array or a Dictionary.
 * If you want to remove elements from a collection at any location and at any time, use a.
 * If you will access elements from a collection by key randomly, use a Dictionary.
 * If you lay your elements in order, use an.

Don't use
The  function is slow. Try to not use it when you export the game. Instead make your own logging function and autoload it. An example:

log =. :  log.(, .WRITE) :  log. (message, source): current_time =. log.( % [source, current_time.hour, current_time.minute, current_time.second, message])

Use statically typed GDScript in Godot 4.x
The gains of using static typing are very high, between 50% to 150% gains. The reasoning of this, in short, is that Godot is putting less effort into finding out what type everything is to run the correct logic.

Use, not
If trying to get a node's grandparent (or an upper node), don't use, since it is 37% slower than just using. It looks more messy, sure, but if you need faster code, this is how you will want to do it.

Use the latest stable version
Newer stable Godot versions have more optimizations and tend to be more performant than older ones.