-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to use Rust alongside C++ to create Godot #1330
Comments
Another relevant issue: godotengine/godot#8873. |
The biggest advantage C++ has over Rust is accessibility. It's infinitely easier to learn and write code in C++; also the codebase is pretty clean, that's why we have 1000+ contributors. If Godot was rewritten in Rust, I imagine the number of contributors would diminish, which would very negatively affect the speed of growth and even if it fixed all memory leaks and crashes, it's just not worth it. C++ project can be picked up by virtually any programmer with some programming experience, Rust code can be written only by Rust programmers (telling from my experience. I had a VERY HARD TIME creating any working program in Rust, even though I've been programming in various languages for 11+ years). |
Looking into the future, I think the general idea/motivation is worth it. But no, I wouldn't rewrite or allow the existing Godot codebase to work with Rust. I think there are too many dependencies/legacy/thirdparty code to properly do that. But what I like are ideas behind Godot, so I'd concentrate the effort on taking the best from both worlds instead. A new engine would need to have a different name as well, because why wait? 😉
I wouldn't be so fast saying this: godotengine/godot#8873 (comment) 😃 |
Since I'm pinged here, I'll add that I don't think this is a good idea on the technical ground. As much as I like Rust, it isn't a good fit for implementing Godot's inheritance-heavy architecture. You might be able to implement a few self-contained modules in Rust with some setup, but anything involving the scene tree will likely be somewhat painful at least. It should also be noted that Rust isn't a magical silver bullet. Even in the case that you manage to get some Rust code into Godot, the C++ parts of it are still going to be unsafe by default and susceptible to the usual C++ pitfalls. About logistics, I'll also agree with the other comments here that Rust isn't a particularly easy language to learn for contributors, since most popular programming languages today do not require users to think about data ownership. The strictness can be quite shocking if one isn't already used to it. It's certainly much easier to create code that compiles (albeit with dubious safety) in C++, which does matter for growth. |
Using Rust (in 2020) may impact the possibilities of compiling Godot for consoles. |
It is unlikely that Godot will be fully rewritten in future into Rust because it will require a lot of amount work because some things like enums or macros works differently in these languages, and Godot uses them almost everywhere. A project that you may like is the recently announced Bevy - https://github.com/bevyengine/bevy/, which is written in Rust and uses it to create games. |
This would mean having to support two different toolchains in the buildsystem, which gets even worse when porting to other platforms. This would only be worth the effort if Godot was slowly migrating all the code base to Rust. |
Closing due to lack of support – see the above discussion. In any case, you can still use the godot-rust GDNative binding in your own projects. |
Rust has more maturity now, maybe it's time to open for discussion/reconsider this? |
More maturity does not obviate the "two toolchains" problem. |
@Zireael07 What about Zig, Zig compiles C and C++. |
Zig still requires installing an additional toolchain, which isn't readily available on some Linux distributions (especially LTS ones). Also, its C++ support may change in various ways if their proposal of dropping LLVM goes through. Zig is also still in 0.x stage and not considered production-ready by its developers yet. |
We don't need for everyone to have both rust and c++ installed. It's also not that complex, IMO. |
How do you imagine making Rust optional? |
Like this: import subprocess
# config.py
def can_build(env, platform):
try:
subprocess.run(["cargo"], capture_output=True)
return True
except FileNotFoundError:
print("WARNING: rust wasn't found")
return False and this: /* register_types.cpp */
#include "register_types.h"
#ifndef RUST_BUILD_FAILED
#include "core/object/class_db.h"
#include "summator.h"
void initialize_example_rust_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
ClassDB::register_class<Summator>();
}
void uninitialize_example_rust_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
// Nothing to do here in this example.
}
#else
void initialize_example_rust_module(ModuleInitializationLevel p_level) {}
void uninitialize_example_rust_module(ModuleInitializationLevel p_level) {}
#endif // RUST_BUILD_FAILED Some classes that use rust won't be available, and can't be relied on. |
Which means it's not optional |
WDYM If you have rust installed, the module will be compiled. Let's say we have a class called Summator. To be 100% clear, by optional, I mean you can compile Godot without it. |
In that case you are not using it to create Godot but optional modules, and this is already possible via GDExtension |
I was the binaries from godotengine.org will be built with rust, so they will be included by default. And rust modules can be used by other parts of the engine after checking if rust is available |
For example, I am working on a type system for GDScript in rust. If rust is allowed to be used in Godot then with this type system a lot of types can be resolved making execution faster without having to specify a type, and better error reporting in cases like this func mul2(number):
return number * 2
mul2("string") # this will be reportead as an error at compile time |
Describe the project you are working on:
Currently I'm creating game which can change view from 2D to 3D(each view use different types of nodes).
Describe the problem or limitation you are having in your project:
Enabling multi-thread rendering and physics, sometimes cause crashes and very rarely unexpected movements/results in release(more bugs) and debug(less bugs) builds(described by other people in godotengine/godot#29641 or godotengine/godot#27406).
C++ as a language allows its users to easily create many kinds of bugs, which as the code grows, become more and more difficult to find and fix.
A good example is the bug - godotengine/godot#31159, which remained unnoticed for two years (from the time the const& was added until the bug was reported) - that just couldn't exist in Rust.
Apart from creating code in C/C++ languages, you should also take time to check if there is no use of an uninitialized variable (compiler flags like -Wunitialized or -Wmaybe-unitialized do not work as they should), incorrect array index (special programs like Dr. Memory or std::array - an array with a markup containing its size) or memory leakage (you have to run e.g. Valgrind).
Additionally, as in C# or Java, you have to watch out for "race conditions" in which several threads can read/write data to common variables incorrectly e.g. - godotengine/godot#34752.
Looking through the issues tab on Github, I've noticed that there are over 500 issues (both open and closed) with the "Crash" label, most of which are in my opinion the result of incorrect usage of C++ by using null pointers etc.
Godot, as far as I can see, uses quite advanced build flags when compiling, and some of the users use various software to check for errors in it, but still there are many bugs, leaks, etc.
Describe the feature / enhancement and how it helps to overcome the problem or limitation:
The solution to some of these problems can be to use Rust to create Godot that can eliminate entire error groups (double-free, memory leaks(most of it) or usage unitialized variables) at the compilation stage.
Godot now has (according to the
cloc
program) about 2 million lines of pure code(one million without the code in thirdparty/), whose every major change can add new bugs, which can be unnoticed for a long time.Thanks to a specific style of writing in Rust, recently changes like adding Vulkan, could be done with much less complications(crashes etc.).
Being reasonably realistic, I know that the chances of rewriting all Godot codebase to Rust are almost zero, so in my opinion it's better to do it gradually by rewriting and adding modules to the engine one by one.
I know that @reduz will be completely opposite to this, because it's quite a big change and will cause more problems in the short term than it will fix, but in the long run it will allow programmers to write much safer code.
Every day, with every new line of code, it will be more and more difficult, so it is worth to start thinking about it now and slowly start this operation.
Advantages:
Disadvantages:
Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:
Access to the Rust code from C++ and vice versa should be done with this manual - https://firefox-source-docs.mozilla.org/writing-rust-code/ffi.html
Tool to generate rust and c++ bindings will be helpful - https://github.com/rust-lang/rust-bindgen and https://github.com/eqrion/cbindgen
I'm not sure how it should look technically, that's why I'm calling the godot-rust creators, because they know best what it could look like.
@karroffel
@toasteater
If this enhancement will not be used often, can it be worked around with a few lines of script?:
No
Is there a reason why this should be core and not an add-on in the asset library?:
Yes, it can't be done with add-on
Why Rust, not V or something else
Rust like C doesn't have garbage collector and is compiled to machine code, its advantage over other languages is that it is already used by several major projects, led by Servo.
The text was updated successfully, but these errors were encountered: