r/rust_gamedev • • 7d ago

question Should I add support for Rust?

Hi, I am the developer of Leadwerks Game Engine. I'm looking for a mid-difficulty language, somewhere in between C++ and Lua. C# would work nicely, but the lag spikes caused by the garbage collection seem like it ruins the language for realtime 3D games, especially for VR. I'm not that familiar with Rust but I hear a lot of good things about it, and it appears to use shared pointers, which eliminates the performance problems full garbage collection would introduce.

Support for Rust can be added by piping all the commands through a C interface and writing some structure/class code on the Rust side that wraps everything up nicely for the end user.

I'm aware of the open-source Bevy 3D engine, but it's lacking a visual editor. Leadwerks 5 has a very capable level editor / visual designer that's been refined over years with lots of user feedback. To be clear, Leadwerks is closed-source commercial software (with a low price point and no royalties).

Do you think Rust would be a good match for what we are looking for in a language? Would adding support for the Rust programming language would be of interest to developers here? Please let me know your thoughts and if you have any questions.

4 Upvotes

30 comments sorted by

15

u/OptimisticLucio 7d ago

but the lag spikes caused by the garbage collection seem like it ruins the language for realtime 3D games, especially for VR.

Unity works for VR and it only hooks up to C#, I really don't think it's that bad.

Like I am a big fan of rust, but if you want a mid difficulty language, I don't think this is the language to look at. It's very hard at the entry point, even if using it later feels great and convenient.

5

u/TetrisMcKenna 7d ago edited 7d ago

Worth noting that Unity also uses a custom Boehm-Demers-Weiser garbage collector for its Mono and IL2CPP (.NET to C++ transpiler) runtimes which is noticeably better than the default GC for games as it allows it to spread GC work over several frames rather than halt the main thread until done.

Been a while since I used Unity so not sure if that's also implemented in the beta CoreCLR implementation or if it's already less of an issue in modern .NET

1

u/Leadwerks 7d ago

What do you use now, if anything?

3

u/NotQuiteLoona 7d ago

I agree, Rust is not mid-difficulty - it's a nice language, but the learning curve is steep. A game engine developer would be much better to ask the game developers to not allocate stuff in the local analogue of update loops. Plus, it seems to be a commercial product - and C# is the de facto standard in gamedev, with two largest engines using it.

Rust would definitely be nice as an alternative to C++ programming, but not as a language between C++ and Lua.

3

u/Leadwerks 7d ago

Thanks for the feedback. I am actually looking at Rust more now as a SC thing for aerospace work.

2

u/thekwoka 6d ago

but the learning curve is steep

I would disagree.

Mainly because it just shifts how you hit those challenges and when.

Like you are forced to write correct (and often quit performant) code right out of the gate.

As opposed to others that will let you write unsafe incorrect garbage that will explode all the time with little guidance to how to solve it that you will have to figure out how to solve anyway.

2

u/NotQuiteLoona 6d ago

Yep! And that's why I've decided to replace "difficult" with "the learning curve is steep" while writing this comment. Rust isn't difficult to write once you learn it, but it completely changes the memory management paradigm that was kept in programming pretty much unchanged since C, and it has to be learned again.

5

u/bcamanitu 7d ago

If you write the engine then why are you worried about c# garbage collection? You should be fully in control of when and how GC runs. And as someone else mentioned, getting devs to not allocate in hot paths is where you'll get wins. If it were me, id be designing some sort of system that let's developers allocate and reuse memory easily in hot paths without making them necessarily manage that memory manually. Like if you could make an attribute like

[PlzDontAllocateBadly] List<Enemy> enemy List;

In which rhe developer interface still behaves like a normal c# List yet the underlying storage is a native array that doesn't get garbage collected and is instead reused - that would create massive performance gains.

2

u/Leadwerks 7d ago edited 7d ago

The engine is written in C++ and unapologetically uses shared pointers heavily. The rendering thread can be set to run at a rate independently from the game thread where the user's code lives, but the game thread could still experience periodic lag spikes when the GC is run. This would not slow the framerate, but motion would be extrapolated erroneously since no new data is being received by the rendering thread. It's not theoretical, it feels horrible when things aren't lightning fast in VR.

I have not worked extensively with C# in Leadwerks yet, so I don't know exactly how bad the problem is yet, but I can say that most of the VR games I have run on Steam suffer from catostrophic performance degradation for no good reason and I want to fix that.

3

u/Standard-Cap-4455 7d ago

Minecraft is using Java of all things and runs decent and there is a reason Godot and Unity use it.
C# has explicit references and value types, so if you just choose the right GC and let the engine manage what it can, it will get very close to C++.
Rust on the other hand is complex to use and if you don't do it right you might even get worse code than in C#.

1

u/Leadwerks 7d ago

Thanks, I appreciate the honest assessment.

1

u/Positive_Total_4414 6d ago edited 6d ago

Any GC-based language requires proper use when performance in a game is a priority. Any non-GC-ed language will take you farther away from your stated job to sell easy-to-learn software. Another consideration is the available ecosystem of libraries. From these points of view, C#, Java or TypeScript would be the best options, in the order of decreasing performance.

Otherwise, try AngelScript or daScript. But for the latest version of daScript they started using AI for development, so... Take a look at least.

Or maybe https://c3-lang.org/ ?

Regarding Wasm, I wouldn't dismiss that comment so easily. Take a look at https://wasm4.org/ for learning, and that's just one example. Wasm might not be the answer you thought about, but it's definitely a player in this field. Using it as a scripting environment for Rust applications themselves is something you see quite some. And you are talking about using Rust as a, in fact, scripting language, ha.

I would also suggest asking in r/ProgrammingLanguages, you might get various interesting suggestions.

2

u/Leadwerks 6d ago

Thanks for the detailed explaination. I started this thread with virtually no knowledge of Rust and just assumed it was another mid-level language, just because that's what most new languages tend to be. The memory safety aspect of it is something I was vaguely aware of, but as I am learning more about that it's very interesting to me.

C# is probably the best candidate at this time.

2

u/garrett_d_null_ptr 5d ago

The C interface approach is the standard way to do this and it works fine for most things. The tricky part isn't the FFI overhead; it's the async boundary. Rust's async runtime (like Tokio) is thread-local by design, but game engines usually run on a dedicated main thread or a small pool. If you try to `.await` across the C boundary, you'll either block the thread or need to spawn tasks in a way that doesn't desync with the engine's frame timing.

I've wrapped Web3 identity checks in similar systems and found it easier to keep the Rust side sync for the hot path and offload any heavy lifting to a background thread. That way you avoid the GC stalls you mentioned in C# without fighting the borrow checker in your render loop.

0

u/RonJonBoviAkaRonJovi 7d ago

Why would someone use your engine over a good one?

1

u/Leadwerks 7d ago

Because you want an engine written by a developer instead of a private equity firm, and have the ability to evaluate technology on its own merit?

1

u/RonJonBoviAkaRonJovi 6d ago

I’ve been around since you were in the dark basic forums, your engine has always been scraped together by whatever public code you could use and resold to the same community you stole it from.. snake oil

2

u/Leadwerks 6d ago

I don't understand your statement. I wrote Leadwerks in C++ one line at a time. We use some third-party libraries for physics, pathfinding, and various things, but that's true of most any engine?

0

u/Substantial-Sir6528 7d ago

Where do you think the good engines come from?

0

u/dicyclometh 7d ago

Why choose any language? Add WASM support, let developers choose whatever they want to write. Plus free VM

2

u/Leadwerks 7d ago edited 7d ago

Because my job is to sell easy-to-learn software and people don't want to craft their own user experience.

2

u/dicyclometh 7d ago

Where did I say they should craft their experience? They simply get to bring whatever tool they are used to, but still within limits of your API.

And rust is definitely not easy. It’s very popular right now because it’s convenient to use with LLMs. But if you specifically want an easy to use tool, there’s possibly nothing better than JS. That is, if you want to stick with one language. It’s also faster than C#, so there’s that.

1

u/Still_Explorer 7d ago

This would be nice in terms of giving it as an option (along side with C++ , LUA, C#).

And so in this case, it might be the point to attract a certain percentage of users, who are knowledgeable or interested in learning Rust as a language through your engine's environment and stack.

However it all boils down to statistics and investment, how many users are working  with the engine and use some language, how many would be interested in joining using Rust. Very tough bet.

One thing is that Unity has invested heavily in their own IL2CPP over multiple years and this is why they can achieve good performance.

However for others non-Unity users who use CoreCLR , might have to deal with the limitations of GC.

Supposedly there could be various techniques writing C# code that can improve performance, but I'm not sure exactly if they have been explored thoroughly. Such as more or less it would be a case of reducing allocations as much as possible. Such as creating memory pools (not creating - removing from lists), or using structs (that do not belong in the heap), or passing objects by reference to modify instead of copying, or even using unsafe pointers in places. More or less trying as best as possible to break the established paradigm of C# and revert to as close to C as possible. This would be something that users might not appreciate, but still if you have any codebase behind the scenes might be a good case to consider.

Also I am not sure exactly how data oriented programming sticks into the picture. eg: if you consider that both Unity and Unreal by the next versions will switch to ECS architecture entirely, through their own custom compilers. Then this  means that you gotta follow the trends and see if  ECS based C# code is noticeably better.

(Probably having to revamp the engine to the core to become more data oriented. At the same time to investigate how to minimize or eliminate GC spikes by sticking the scripting components directly to the the ECS infrastructure. Nobody knows if this actually solves the problem but is an experiment).

1

u/thekwoka 6d ago

Rust is great.

And visual editors aren't as important as you might think, especially with tooling that exists to author scenes in Blender, Trenchboom and others.

Authoring scenes it about the only thing a visual editor really helps you with.

Other kinds of things (like tweaking values) can be done in code with some fancy crates, or by just making a simple bevy_equi overlay for dev.

0

u/Rythim 7d ago

As I understand it, even though C# is garbage collected it can be extremely fast when used right (also, isn't Lua garbage collected?). It's also very popular among game developers. Rust isn't really a language most game developers use, as far as I know anyway. Can't see any reason why anyone would choose to use it unless they happen to already know and like Rust. Plus it has a reputation of having a steep learning curve at the beginning. I could be wrong but that's my opinion.

That being said. If you want my unhinged opinion you should use Go. It's not a good language for game development or anything. I just like the language and like the idea of a game engine using Go. Writing Go makes you feel good.

0

u/Leadwerks 7d ago

"When done right"...Why must we live with this?

Thanks, now I must investigate Go. 😵

1

u/Rythim 7d ago

Haha. Have fun! Go is ... Different. Honestly probably objectively a bad pick. But sometimes you wanna do something not because it's the best way to do it but because you can.

-1

u/ykafia 7d ago

D for scripting can be good maybe?

2

u/Leadwerks 7d ago

I will look into this just because someone downvoted you for having thoughts of you own.

1

u/ykafia 7d ago

Thanks :D Just FYI d has an optional GC you can disable easily