What's the easiest way to run c++ code?

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

I have a script that works independently of godot: I compile it in my computer and run it, and it generates a random maze for the game that is saved on a .txt file which godot then can load and read. The script doesn’t need to interact with godot at all.

I wrote it in c++ because it is a long algorithm, takes around 20 seconds, so there is no chance to coding it in GDScript (would be way too slow).

What’d be the easiest way to run this c++ script every time the game starts?

:bust_in_silhouette: Reply From: gnumaru

Use GDNative (on godot 3.x) or GDExtension (on godot 4.x).

You could convert your c++ “script” into an Object (or Node) subclass. Then you create a gdscript that, on _ready, creates an object instance for that type and call some methods. You could even create a default implementation for _ready on your node class that does the things you want, so just attaching that script to a node will execute the logic when the scene starts.

Take a look at these:

If you want this only when the game starts for the first time, you can just attach your script to your game main scene or create an autoload singleton.

gnumaru | 2021-10-19 15:36