I’ve been using Godot for about a year now, i’ve learned a lot about it, even managed to make few game jams from start to finish
Recently i was wondering about C++ in godot, C# don’t work on web and i usually test my games on web with my friends, but C++ apparently does so i tried to learn it. Spoilers - i failed terribly :\
That’s why i wanted to ask for advice for complete beginer when it comes to GDExtension - how do i learn it in order to use it and somewhat understand what i’m doing?
Also disclaimer:
I’ve followed godot docs but they aren’t very detailed, they are more helpfull than wikipedia or google but still alone they don’t cover errors nor newest versions of godot
I use this C++ template, sets everything up nicely with only a few replacements, and uses scons.
Using GDExtensions isn’t well documented because any bindings made aren’t really officially supported and the ffi has had some decent changes.
With godot-cpp using Resources generally requires the Ref template, Ref<YourResource> and nodes are surprisingly just pointers YourNode *. Do not try to make anything from godot namespace in global-scope, for example a static godot::String will crash. Other than that you can follow along with the official documentation, the C++ looks fairly similar to GDScript with some boilerplate for getters and setters.
Oh actually thanks for this, i’ll definitively check this out later, tho i’ll also want to learn godot extension more basic way to truly understand something than to just copy, do you have any tips where to start with that?
The setup part is actually pretty basic and you will have to touch it as part of the template.
Godot looks for and reads .gdextension files
This file must contain a path to shared libraries for any supported platforms, and an entry symbol
When building you must expose a C-ABI (extern "C") function (this is the entry symbol)
the entry function initializes all of your types you want to expose to Godot
everything after this is godot-cpp specific, other languages like rust or zig may have different initialization flow
This set up is because GDExtensions use a foreign function interface, the entry symbol will be given a function that your library will call to get more function pointers, such as pointers to string constructors or even basic math like Vector2::Length.
This git repo explains it a little more, you’ll notice that the setup is almost identical until the section on “Creating a StringName” which requires getting function pointers to variant’s constructor, destructor, and string name’s from utf8 constructor.
The only reason to use C++ is performance. If your project performs adequately with scripts only, don’t even think about extensions. Use it only as a last resort performance optimization after you’ve exhausted classical code optimization, threading and compute shaders.
The tutorial in the docs (and already linked templates) show you how to set up the build environment and make a scaffold project that implements a class, a property and a method. That’s the tallest hurdle imo. From there it’s quite easy to continue. You do pretty much all the same things you would do in GDScript - inherit an existing class and add functionality to it.
Since there’s almost 1:1 relation with GDScript functions, the GDScript reference is a good reference for C++ as well, though be ready for occasional peek into engine’s source code to figure stuff out. It is the ground thruth after all.
If you get stuck ask here or on discord. LLMs also may “know” obscure stuff they sucked off of endless discord chats, but expect a hallucination here and there.
A good approach for learning, and in general, is to implement a class in GDScript first and then “port” it to extension code.
As for Ref, it’s a reference counting smart pointer template. It works almost exactly as std::shared_ptr
Actually LLMs work like 5% of times and hallucinate every message so they’re useless here :\ why can’t there be more low level code to copy…
Anyways… the biggest problem i have is actually setup, because there are many errors regarding SCons and godot that prevent me from even seeing example work
I could ask for specific help like “how to fix error X” but i really want to learn why they happened and not depend on other devs here, still docs aren’t well documented here and that’s all i’ve tried, my only guess is that they’re outdated and i should try on older godot versions, but that’s not long-term strategy, do you have any suggestions or links where i can learn about building itself?
Very very true, I pretty much only read GDScript docs and it translates directly to GDExtension, this is by design of course.
I’ve been working on a RISC-V assembler/emulator and the strong typing of C++ (and I wish I used Zig instead) helps too. Very rare cases you’d need to use GDExtension none the less.
Also! It’s going to be way harder to debug, especially when targeting the web. I have experienced platform differences in godot-cpp that posed a significant block, you may have to constantly test all your target platforms. Hard crashes on web builds late into development made me re-implement parts of my game in GDScript.
C++ build systems are horrible and I will lament forever about them. Scons certainly feels the most magic to me, if it works, it works beautifully, but if it doesn’t then it’s wildly annoying (I don’t like python). There is another gdextension C++ template using CMake but that feels like 4 steps in the wrong direction. Posting your error message would in Help would be best.
The build environment tutorial is somewhat hard to follow, especially if you have no prior experience with scons. It’s easy to miss a step. Concentrate and follow it slowly to a tee. Once you make it through the first compile, it all becomes much easier.
I hope so, if scons is truly magic then no wonder there’s not many people who fight with it, also last question - if i don’t really need breakthrough performance but really need it, how GDScript compares to C++? can it do it or not? broadly speaking for now?
Broadly speaking when translating GDScript to C++ you will only see a performance increase if you called a lot of functions you created (GDScript has a pretty high function call overhead), or did math in a huge loop. Godot comes with a lot of native functions that are backed by compiled code, there is usually a way to squeeze performance out of GDScript such as directly using Servers, I’d wager if you are using servers the gap between GDScript and GDExtensions is rather small, of course “small” adds up so watch out for loops iterating ten thousand times.
If you are concerned for performance I’d recommend this checklist, after every step check your framerate/profile
so if i used gdscript and built-ins should i be able to achieve the same or better performance broadly speaking, especially when problem isn’t related to pure math but algorithms or other stuff?
Like let’s say you want the distance from the player to a finishline, you can calculate this per-frame in both with global_position.distance_to(finish_line.global_position). Both GDScript and GDExtensions will finish this operation very fast, there isn’t a computer on earth where this one calculation is the breaking point for your game.
If you had thousands of enemies and every one of them was calculating the distance to the player, and distance to each other enemy then you will have the same problem on both GDScript and GDExtension, O(n*n). The path from the engine core to the enemy’s GDScript is not so significant that switching to GDExtension will help enough. But, using distance_squared_to instead might give you enough breathing room, because this native function skips a square root instruction and the result is still useful in most comparisons. This algorithmic change helps, not the language change.
honestly it depends. There are like hundreds of compiler optimizations that I’m aware of and cpp gives the tools to access them. Point is, if you spend 10ms in some code path, in cpp you can almost infinitely tinker with it. I have some code samples that run in 100mcs and gdscript can’t offer that. I also dont think binding a button is any different in perfomance
Each CPU core can execute a huge number of FLOPs, yet it’s surprisingly hard to keep all its execution units fully utilized. It’s so easy to end up with poor memory access patterns, inefficient multithreading, excessive branch mispredictions, or inefficient instruction selection for a given chip.
All of these issues can be addressed on top of the basic strategy of simply doing less work.
What about when you want to override built-in methods? I hit this most recently the other day while wrapping Noise to return adjustably clamped values… like all this extra code and/or changes to the code that calls the built-in all because I can’t extend Noise to override get_noise_2d in script.
I suppose the GDExtension solution is still more code/boilerplate. But, I think if I already understood it, I might reach for that tool sometimes. (But, then I remember that I should probably learn Rust and Godot-Rust instead.)
I don’t believe you can override built in methods with GDExtensions, my understand it would be less possible through GDExtensions since overrides require a vtable and/or Godot bindings, of which the compiled code may have less access to both.
Could you share your Noise extending code? I’d love to see it work
Godot-Rust is a set of GDExtension bindings for rust, just like godot-cpp is for C++
Could you share your Noise extending code? I’d love to see it work
If you mean in GDExtension, I think I wasn’t clear enough that I was asking about the possible use-case of GDExtension for a problem I have, not something I’ve used it for. I extended Node with some exports and got what I needed that way, but my programmer brain would rather have an actual Noise object instead of a Node replacement for one just to get this functionality without changing the method name.
Well, I’m glad I didn’t learn GDExtension to try to do that, then. Thanks for setting me straight! The complete inability to do this is now one of my serious frustrations with Godot. I always thought it was just a scripting limitation.
Yeah, I think that’s not doable via extension. Although Noise is a pure virtual class in C++, that doesn’t carry over to extension bindings. Funnily enough the Noise class reference says this:
Inheriting noise classes can optionally override this function to provide a more optimal algorithm.
Which is highly misleading, letting you think you could even do it from gdscript. The only way to inherit this is at the level of engine’s source code, i.e. it’d require recompiling the whole thing.
Your best bet for this is to make a classical wrapper with different function names.