Is it possible to initialise a script during build time

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


I have a script that is attached to a single node in multiple scenes, that appears to cause a slow down on the browser (html5) build of the game.

As this script currently initialises during run time, I was wondering if it was possible in godot to make this occur during build time which would improve overall performance and if so how I could implement it.

The initialise function of this script is largely static, which it only being different on different scenes due to what is within the node. The node itself will never have it’s children modified via script.

I’ve attempted to search regarding this, but haven’t found anything overly helpful for me.

:bust_in_silhouette: Reply From: Bernard Cloutier

Compile-time (or “build-time”) object initialization is not a thing, in any language. Though if you want to take advantage of some compile-time optimization, the best thing would be to rewrite your script in C++.

What does your node’s _ready method do exactly? Could you post your code?

When does the slow down happen? Only once when the scene is first loaded? If not, then I don’t think the slowdown comes from the initialization. Please post the whole script.

In general the script traverses all tiles within a 3d gridmap and generates an astar graph.

Further examination makes it appear that the slowdown is due to either rendering or environment file as the above _ready function logs fairly quickly when the scene starts.

In terms of compile time initialisation, I was wondering if as an optimisation step it was possible able to compute this astar graph on build as a static array and then utilise that static data instead of looping through the tiles.

Will further look into the exact cause of the slowdown, I just assumed the above would be a good optimisation step if it was possible.

silens | 2020-10-15 09:45

I see. Well if the gridmap is static you could always “bake” the a-star graph. By that I mean make a tool script that writes the graph to a json file or something, and when loading the scene instead of computing the graph you read it from the file.

Have you tried using the profiler? That could help you determine if slowdowns happen in one of your own functions.

Bernard Cloutier | 2020-10-15 12:28