0 votes

Hello,

I am looking for advices about coding in godot if not general.

If I want to make to optimize my code (for either polishing something finished or for not dragging cumbersome code all along the project) what would be the general process ?

something like (?):
- finding bottlenecks
(how ? cannot find functions into monitoring panel)

  • optimizing this bottleneck
    (what are the basic/general principle for this ?)

  • if not enough translating it from gdscript to gdnative/c#/c++

I can find some particulars answers there and there but wish to get a more global vision, any useful tutorial around here ?

thanks.

in Engine by (114 points)

for example, is it true that accessing others nodes is costly in any case, and should be avoided if possible ?

1 Answer

+3 votes
Best answer

In GDScript, the main way to do optimization is to optimize the approaches themselves, the algorithms and data structures you choose to use (dictionary vs array is most common).

Knowing which engine functions to call also helps. For example, you mentioned accessing other nodes: if you use get_node("path") or $path everytime for a node that you know is always present, sure it will have a cost (small), the engine has to decompose the path and compare node names until it finds the correct one, which can however be easily avoided by storing the node in a variable with onready, with the added advantage of failing earlier if the path is wrong.

Another point is memory: allocating memory often has a high cost. .new and .instance() may allocate memory and run construction logic. In Godot, custom allocation strategies are in place under the hood to optimize this already, but constructing and destruction objects still has a "setting-up" cost when they are many. So if you plan on having hundreds of VFX and bullets in your game, investigate how much do they take on frame time and pool them if needed.

Lazy-loading can also impact performance: a typical bottleneck in Godot 3 are shaders. They are very big internally and take a long time to compile, and that can only happen when first needed, so the first time an object is visible or an effect plays, it causes shader loading and short freeze (See https://github.com/godotengine/godot/issues/13954). It's not necessarily related to scripts, but something to know about if you optimize your game.
The same applies to sounds: if you play a sound by loading it on the spot, it might be loaded on that spot, and cause a freeze. The solution is to either set it in advance on the sample player, or preload it.

Use signals and input callbacks when suited. It can be tempting to put everything in _process, but not all things are frequent enough to need constant polling.

Optimizing script bottlenecks can be adressed in two ways:

  • Old school method, you can wrap the region you want to profile between OS.get_ticks_usec() calls and calculate how many microseconds the function took to execute. Keeping everything under 16ms is usually the goal. Try on lower-end devices if you have margin.
  • Use the profiler in the debugger tab. It has a listing of functions, but will only profile if you enable it while the game runs. I find it lacking in usability and features though, even the doc has almost no details on it https://docs.godotengine.org/en/3.0/tutorials/debug/overview_of_debugging_tools.html?highlight=profiling#profiler, which is why I often fallback on the old school method.
by (28,731 points)
selected by

Oh my lord, you have no idea how useful this is!
I’ve been using a lot of process functions to check for changes in booleans. For instance process(delta): if “var” == true: “do something” then “var” =false

What do I do to replace this code?

FYI the “var” is located in a singleton that “controls” the flow of the game

Any more help is appreciated

maybe try to use signal, its only check when the condition meet, not every frame like in the process(delta) function

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.