0 votes

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?

in Engine by (21 points)

1 Answer

+1 vote

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:

https://www.youtube.com/watch?v=AySSPPkBIqk

https://godothub.wordpress.com/2021/02/08/how-to-make-smooth-top-down-movement-in-godot-gdnative-c/

https://www.youtube.com/watch?v=XPcSfXsoArQ

by (210 points)
edited by

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.

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.