399 points · by ibobev · 4y ago · 122 comments
It's also nice it uses bevy's ECS. It's made to fit Rust's type system and borrow checking model, and is highly parallel. It's really awesome how easy it is to distribute work across cores with it, without risk of heisenbugs thanks to Rust's data-race-safety guarantees.
I know game development sees traditional software best practices as not applicable to them, but then games struggle with showstopping bugs and single-core performance bottlenecks. I hope someone will bet that Rust, despite being stricter and seemingly slower to iterate with, can be a net benefit from lower defect rates. Rust users outside of game dev praise the "if it compiles, it's usually correct", and that junior devs can be told not to write unsafe code, so they can't cause UB and memory corruption.
The kind of bugs that games mostly struggle with these days have nothing to do with the stuff Rust is targeting, like memory safety. They are “business logic” problems with many interacting systems resulting in unpredictable behavior, buggy level geometry, or poorly scripted missions not accounting for all player behavior. How exactly does Rust help with any of that?
Gamedev isn't some place of rainbows and unicorns that makes it a unique snowflake. I spent more than my fair share of time chasing down heap corruption, invalid iterators and all sorts of other fun issues.
If anything working on a console where the compile, cook, upload, play cycle can be long having something that removes a whole class of errors would have been great back then. I don't think anyone is saying Rust is a silver bullet but it certainly has aspects that are beneficial in certain contexts.
But I mainly take issue with the cockiness of my parent post. Rust is a reasonable evolution which, as you point out, eliminates one class of bugs. But my parent post took it for granted that things should be rewritten in it whenever possible, and that the gamedev community is ignorant of best practices.
The tech industry's demographic pyramid has a really wide base, which is possibly why it seems to build cults around new programming languages so much. Last time it was Ruby, and if we all listened to the equally self-assured Ruby crowd, everything would now be message passing up the wazoo.
Besides, syntax and complexity wise I think Rust is even worse than C++.
As a gamedev I am not interested in Rust. Zig, Jai and Odin seems much cooler/interesting.
Jeezus.
As an outsider to both game dev and the general Rust ecosystem, but with a more than passing interest in the ECS data model (entirely apart from games!) and the prospect of using it safely with parallelism (entirely apart from memory safety concerns!), I found GP’s comment insightful and interesting. I find yours unnecessarily defensive.
I get that “in Rust” is a HN lightning rod, but maybe sometimes it’s okay for people to just appreciate someone’s personal project for the reasons they appreciate it, without it being an instant conflict?
Even in single player games, a game crashing and losing your progress sucks.
Rust’s non-borrow checker features help with other kinds of things. Rust isn’t only an ownership system.
It's turn based, so I don't need AAA game multi-threaded performance. It's single-player, so cheating doesn't really hurt anyone.
I use sanitizers to catch memory problems, but there have only been a few of those over the years. Maybe ECS just isn't that vulnerable to memory-safety problems?
The borrow checker prevents iterator invalidation problems, but most of my functions are read-only so there's not really a chance for those anyway, or many other single-threaded race conditions for that matter.
After this long using Rust, I'm noticing more and more that the borrow checker is imposing a lot of artificial complexity, when I compare it to my C++ version. We're often told that Rust is hard because the underlying problem is hard, but I've encountered many cases where that's not true, it really is just the borrow checker. And unfortunately, most of them are problems that Polonius won't fix.
Rust has a lot of great features (enums and cargo!) but I think one should think carefully about their domain and architecture before choosing a language, lest they pay too much cost for too little benefit.
Just my two cents!
Rust is a great C and C++ replacement for low level stuff, I am still not convinced it is worthy versus those languages for distributed computing in terms of the productivity automatic memory management brings to the table.
> many interacting systems resulting in unpredictable behavior[0], buggy level geometry[1], or poorly scripted missions not accounting for all player behavior[2].
[0]: Rust borrowing system (you know exactly who is allowed to touch each ressource at a given point in time) is great at pushing you away from architecture with too much mutable interaction. That is a side effect of the system rather than its design purpose but, for my point of view, it is one of the main draw to write large projects in rust rather than C++.
[1]: probably not something Rust will help you with.
[2]: Rust type system (and overall coding conventions) is really good at building things such that it will only compile if you check for all cases or, at the very least, leave some explicitly unchecked cases.
These types of interactions exist at a layer where Rust can't help you. Even outside of gamedev, I would claim that most software bugs we run into every day are in this category: too high-level for the borrow checker. (Button not connected to anything. Scrollbar hidden, but the page is too long. Microservices that upgrade independently without API stability.)
> [2]: Rust type system (and overall coding conventions) is really good at building things such that it will only compile if you check for all cases or, at the very least, leave some explicitly unchecked cases.
I invite you to try to teach Rust's type system to level designers.
Other types of software projects also have their own complexity, budgets, changing requirements, and deadlines (admittedly not as major as a game release), but these things are managed differently.
Without telling all of gamedev that they're doing it all wrong, I'd like to say that perhaps there's another methodology that would also work. Especially that some games nowadays aren't just one-offs with a big-bang launch, but are maintained for years after initial release, which makes them look more like other software projects.
Games aren't struggling because they're written in Unreal's flavor of C++ (or whatever other currently existing engines and their respective languages), there are insane deadlines always being pushed. There is never enough time and never enough money. Neither of these problems are solved with Rust, at least not to a substantial degree.
Higher compilations times aren't all bad https://xkcd.com/303/
Why do people keep echoing this sentiment? I don't find this to be particularly true, in my sense of "correct". Yes, it does what the code says it'll do, but rarely the problem I actually get stuck at are language/syntax/semantic errors, it's usually domain/logic errors.
Same can be said for PHP, JavaScript and a bunch of languages, as every program that is possible to run without error, is correct, at least in the eyes of the interpreter. But again, the difficult errors are not syntax/language errors.
After four years of Rust (and 25+ of C++) I find this very true for the former and rarely for the latter except in extremely simple programs.
And yes, this says nothing about correctness as far as what the code is intended to in the first place.
But you are wrong when saying this, too, applies to e.g. PHP or JS in this context. For the simple reason that they are dynamically typed alone. The class of errors/side effects that this enables is a whole different league.
Exactly my case. I've forgotten last time I've had memory alloc / free/ overrun /free after use etc. related bug in my C++ projects. I am using modern C++ which in combination with STL containers makes me forget about all this C++ is scary FUD. I am however not being "turn everything into template" maniac and in general trying for code not to be fancy but rather readable.
Having written a fair amount in both languages as well as many others I find this to hold true. The phrase isn't, primarily anyway, about Rust's memory safety guarantees in my mind.
The main benefit of Rust towards safety is not memory safety or data race safety or undefined behavior. The main benefit of Rust towards safety is a strong type system with affine typing. When used properly, this eliminates huge classes of bugs. Most of the bugs that I need to fight when I'm writing in Python or JS, for instance, are simply impossible in Rust.
Of course, when I'm writing C++ (which I don't do a lot these days), I really miss all the features that you mentioned.
The problem with C++ is the slow compile times, and if you make mistakes the engine crashes. In general you produce stuff slower by using C++.
Blueprint is a visual scripting tool, it compiles fast. But it stores code as a binary blob in source control. And I'm personally not a fan of visual scripting, I would prefer scripting by just writing code.
So generally I prototype in Blueprint and then rewrite in C++ later on. I would love an intermediate language that has fast compile times and saves as a source file instead of a binary blob.
The problem is most of the projects that integrate with another language get abandoned after a while. MonoUE was abandoned, USharp was abandoned, now the newest attempt to integrate UE with C# UnrealCLR looks abandoned. SkookumScript doesn't support UE5. Haxe support for UE was abandoned.
Do I really want to risk writing a ton of code in a language that might not support future engine versions? To be honest, I don't want to take that risk.
True. Although rewriting Nanite in Rust would be useful. Nanite is a very clever data structure for meshes. It's brittle, and full of internal relative addresses within the data item, so it's very vulnerable to buffer overflows. If it becomes widely used outside of UE, it might become something that gets supported in GPU hardware.
I am not sure how much you would gain by doing this.
Internal references are one of the things where it is very hard to express in safe Rust what you want to do (see doubly linked lists and graphs). You can get around that by using vectors and indexes, but that is bypassing the borrow checker, and with it, much of your safety and mutability checks.
Given we're talking video games, you could also not worry and let speedrunners take advantage of it... ;)
This demonstrates the flexibility of GC'd languages, where you don't paint yourself into a corner so easily.
Good summary. Most of the headaches I have in Rust come from needing something that's not a simple tree. Parts that are simple trees are really easy.
Nanite is a DAG, by the way. The whole point of Nanite is that instances are combined, recursively. A mile of chain link fence would be highly detailed sections of wire, combined into full wires, combined into fence panels... But not just all simple repeats. If there's a hole in the fence somewhere, that can be represented.
Just use unsafe! That’s what it’s there for. You can use internally-unsafe semantics to expose an externally-safe API and nobody needs to care.
You get the Rust’s safety benefits everywhere else (not to mention all the other nice parts) and your unsafe code is limited only to the areas it’s truly necessary.
Using bounds checking is likely to kill performance on such data structures, while using `get_unchecked` or raw pointers will keep the issues.
But even then, I would love to see Nanite implemented in Rust.
This is not true - bounds checks are always enforced: https://play.rust-lang.org/?version=stable&mode=release&edit...
Maybe you're confusing it with signed overflow behaviour in Rust or bounds checks in Zig?
I write a lot of C++ plugins that are used in both Unreal and Unity. I like to think of them as "side loading". They can do whatever they want, however they want. The API surface that interacts with either engine is pretty minimal.
I think "use Unreal as a renderer" is underrated. You could imagine writing your full simulation logic independent of the AActor framework and then using Unreal just for rendering. For a client/server game the server might not use Unreal at all!
I'm not sure how valuable it is to use Rust but also use Unreal's framework and blueprints. Bevy + Unreal is a pretty fascinating combo and how much lifting is done by each side can be done in lots of ways.
Some "lispy goodness" like what? Is it s-expressions? Or do you mean like it has things lisp invented: things like conditionals ("if/else" statements), garbage collection, recursion and first-class functions?
If the first, that'd be super interesting! If it's the latter, tons of programming language have implemented features like that before :)
From smalltalk I would like the message system so that all messages to an object only have one entry point. From lisp I would like the macro system and that it all looks like the same language and not the regexp-frankeinstein thing that Rust has.
What I really really want (and maybe I can have this easily!) is to be able to inline some C++/Rust inside a blueprint. Sometimes there are expressions that would be way easier to type out, or some tricky state logic that I can express with prose more nicely than with arrows.
I imagine the "serious" thing to do is to build out the C++ in some other file and then do the thing with my blueprint, but imagine if we had a completely smooth gradient for upping the tooling from blueprint to C++ when needed!
https://www.unrealengine.com/marketplace/en-US/product/magic...
UFUNCTION(BlueprintCallable) // This is a purely C++ function that is exposed to Blueprints and can be called from blueprints
UFUNCTION(BlueprintNativeEvent) // This can be implemented in C++, Blueprint, or both (in the Blueprint implementation, call the Parent method and it will run the C++ function and then run the Blueprint function)
UFUNCTION(BlueprintImplementableEvent) // This is a purely Blueprint function (though it is defined in the C++ header) that can be called from C++.
I believe there may also be some funky reflection code you can create to invoke Blueprint functions from C++, but there's very little reason for that as BlueprintImplementableEvent does that in a cleaner way.
Source:
https://docs.unrealengine.com/4.26/en-US/ProgrammingAndScrip...
I looked into using this approach when I was playing around with the idea. Writing lots and lots of C wrappers seemed … wasteful.
We need something that transpiles to native Unreal code, which has the majority of the verbosity and productivity aspects of Python.
Yes, Verse, if i'm not mistaken.
GDScript is another good example of this.
Unreal already supports full Python for some stuff. They do also have a language called Verse in the works but I don’t believe that it has any relationship to Python
I believe Verse is made by a small team who previously worked on a game-specific scripting language called Skookum script. I used it maybe a decade ago so my memory is hazy but I think you are correct, it didn't necessarily have much relation to Python. I remember it having some unique game-specific async constructs. I'm certainly looking forward to trying it out!
I haven’t used rust, but I sympathize with his problems with pointers and learned a lot from the various ways people try to solve them
My understanding is his priorities are C level control over memory, fast compiles, and ergonomics for the kinds of operations he commonly does.
Rust solves for memory/ergonomics, though the strictness it holds you to is in opposition to what (I understand) he considers ergonomic access & control of memory.
Therefore rust solves ~1 of his priorities, so it's not really a good fit. To extrapolate that to "doesn't solve the hard problems" is another logic step that I probably don't agree with.
Because C++ already is way too slow to compile wich hurts iteration time, wich is why most people fall back to plain simple blueprints, and even that can become quite heavy
Epic is working on a new scripting language that will improve this
What Rust brings to game development and how it's better than C/C++ in that regard?
Because you don't develop a game the same way, with the same constraints as a driver for example
TBH Unreal C++ is faster to compile than Unity C#. It is slower than an interpreted language. But I’ve also worked pipelines that chain Python executions which compound into ~5 second boot time.
Faster is always better of course. But I often think people overstate compile times. The upside is it means people work hard to improve things from good to great!
Hot reload is great for small edits, but anything that require substantial changes or refactors and you got to recompile everything, that's what's slow
My Unreal C++ modules are pretty small. But on a P620 I’d say my time from editor -> compile -> relaunch editor in under a minute? I think I can compile and link the full engine in 15 minutes?
But I will admit my current Unreal projects are quite small.
I respect that making FFI bindings to Rust would be very difficult or impractical, but that's what I think would be interesting.