Unreal Rust
maikklein.github.io
maikklein.github.io
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.
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/
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?
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!
Out of curiosity, what are your favorite examples?
Personally, I've only struggled a bit with the borrow checker in a couples of cases: trying to have self/circular references when trying to model OOP, and when fiddling/interacting with some low level async code. Both were unnecessary in my case, and each situation was caused by a lack of understanding.
To expand on this a bit. In games, objects generally fit into two categories:
1. Short-lived temporary objects, usually stack-allocated or arena-allocated, etc. e.g. a point vector, or matrix.
2. Long-lived objects with indeterminate lifetime. e.g. a character or item.
In the case of long-lived objects, there are various strategies you could use to make it safe e.g. using object IDs or custom smart pointers to refer to other objects that potentially might go missing instead of pointers which are bound to be corrupted.
Generally you want to register all of your long-lived objects in one central location that will handle memory management. Manual, reference counting or GC are all viable strategies depending on requirements.
I like Rust and would use it for games but mainly because of the package management.
Then you could just use a GC'd language. For example, you could use C# in Unity.
I don't understand this, just because it's turn based it doesn't mean you can't squeeze more juice out of your CPU if you want to have all kinds of good graphics, fancy animations, fancy complex environments (lots of moving pieces, etc), physics simulation, etc.
Turn based is just one style of play but there's plenty of turn (or pseudo-turn) based games (like JRPGs) which struggle with platform limitations. One could even argue that games like Final Fantasy XIV are turn based.
Do you use async closures? That's the only case where I felt the borrow checker is a bit behind. Also I haven't checked in a few months and I know there was some work ongoing.
That forced me to start organising the code in a different way which is less functional but easier to understand, so it's not crystal clear what's happening.
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.
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?
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.
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.
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.
The compiler’s error messages are fantastically helpful these days. Might be worth a tinker some rainy day, but understand being completely turned off of something as well.
https://play.rust-lang.org/?version=stable&mode=debug&editio...
If that code ever gets more use than that you expect, you can trivially find all the uses of “unwrap()”, and add the proper error handling. And yes there are quite a few places where things can go wrong.
But this explicit “signing for technical debt” when it relates to error handling is a really convenient productivity feature in practice.
A simple throwaway CLI for your own use is absolutely fine to use unwrap(), and you can fix it later on if it becomes a problem.
Some stuff that places Rust going into the same kind of complexity as C++.
Figuring out how to turn DAG algorithms into tree based ones, two macro systems, async infrastructure, Pin and PhantomData special fields, GAT (when they arrive).
Lets see how it looks like with 40 years of production code, on the other hand C++ is indeed reaching PL/I levels of complexity for those that care about ISO legalese.
I don't think anyone here is saying that, at least in this thread the parent(and myself) were both careful to qualify there might be contexts that it could be useful in. If you see anyone saying that Rust solves every single problem then feel free to call them out for being full of shit :).
I'm not going to be giving my designers a Rust tutorial the same way we did with scripting Lua, you're still going to have a FFI somewhere which will require unsafe and a bit more care. However having had maintained pretty significant codebases in quite a few languages I will say that Rust code is more stable, crashes less and still runs in the same footprints as C++(C gets the codesize edge by having a much smaller stdlib and surface area). It's about knowing where your tools do well and where they have rough edges and a better tool is appropriate.
Having to switch between C, C++ and Rust codebases on a regular basis I think you're underselling the complexity of C++ and overselling how difficult Rust is. C++ has a huge surface area, you have to figure out what subset you want to use(exceptions: y/n?, RTTI: ditto, stdlib or custom containers, etc). Then you get into Rule-of-5, move semantics, template metaprogramming, undefined behaviors(don't use global constructors/destructors). I could easily list 20 years of various ways I've seen C++ explode in spectacular ways(volatile semantics anyone?).
If you want to use C, Zig, or C++ more power to ya. No one is forcing you to use Rust but for those of us who are interested in a language that isn't dragged along with decades of baggage Rust offers a compelling development story and largely delivers on the areas it aims to hit(accessible systems development, memory safety and first-class tooling in my experience).
Next question.
There is no substitute for engineering, if you care. So, engineer.
Avoid triggering exceptions too hard to recover from. That might involve work, but less work than fielding error codes would have been. There is no free lunch.
I really hope this sort of strawmanning doesn’t become common here. In response to any reasonable discussion about Rust the response seems to be “you’re fine, but the other enthusiasts are so unreasonable”. Where are these enthusiasts? Why bring them up when you can just respond to the person you’re talking to?
It’s super annoying because even on irrelevant threads such haters will call people’s attention to these imagined enthusiasts. Like this comment from 6 days ago calling people jihadis if they like a language - https://news.ycombinator.com/item?id=32640154. Note that the jihadis didn’t exist on the thread, only in the commenter’s mind.
> I will say that I've never written a line of Rust in my life and certainly can't be described as representing the Rust community in any way.
If this is the best example out there then it does feel like GP was arguing against a strawman.
But even otherwise, please let’s acknowledge that in any large community there’s going to be a minority who are loud and rude. It doesn’t advance the conversation much to point it out. They don’t represent the whole community any more than the comment I linked to represents the whole HN community.
All I can say is that whenever I’ve seen such rudeness it’s moderated out pretty quick, whether here or elsewhere.
I can't go along with this at all
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.
Jeezus.
> 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.
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.
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.
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.
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.
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 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.
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.
> It's brittle, and full of internal relative addresses within the data item, so it's very vulnerable to buffer overflows.
I think the hope was that rewriting it in Rust would make it more robust. But if the only way to write it efficiently is to unsafe your way to writing C++ in disguise, it's not clear it will be any more robust.
Just because some part of your program needs to use features unavailable in safe rust doesn't mean your entire program might as well be written in asm.
Rust itself is written using plenty of unsafe code, all of which runs on top of fundamentally-unsafe CPU architectures. And yet it manages to expose a safe API to its users who somehow to write memory-safe Rust programs every day.
> You can't always hide the problem behind an API, because for instance an API may call back into your code and access data.
This is quite literally all that Cell and RefCell do and they hide this behavior behind a safe API.
Do memory-safety leaks in supposedly memory-safe abstractions crop up occasionally? Sure, nobody's perfect. But this idea that unsafety is uncontainable and spreads out everywhere until your whole program is infected and riddled with memory-safety bugs is completely detached from reality and regularly disproven by Rust developers on a daily basis.
In my view it is silly to do very high-level stuff in a systems language that was never intended for that purpose, and when you can clearly see the downsides of that approach; you are thinking of memory issues a large part of the time instead of being able to focus on your high-level functionality or even high-level security issues. Of course you can try to do it, but then it is research, not practical or necessarily sustainable programming.
Are Golang, Ruby, Python et al still in the "research phase"? After all, none of them have native GUI frameworks written in them yet.
That said, you're moving the goalposts. The entire premise of my original reply was objecting to your claim that Rust paints you into a corner should you need a non-tree-like data structure. Which is absolutely ridiculous, there are countless examples of self-referential datastructures written in Rust. They use unsafe, which is what it's there for.
No, no. "Unsafe" should not be used because you can't figure out how to do some data structure in Rust. You'll get it wrong.
I have 27,000 lines of Rust in one complicated program with no "unsafe".
Second, Rust itself is implemented with unsafe. Self-referential data structures are necessary for some problem domains, and those must be implemented with unsafe. Unsafe exists and is a tool that is sometimes necessary. When it is necessary, it should be used carefully.
Just because you haven't needed unsafe personally doesn't mean others don't. And the Rust developers aren't superintelligences capable of using it correctly while the rest of us mere mortals aren't.
Given we're talking video games, you could also not worry and let speedrunners take advantage of it... ;)
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!
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...
https://www.unrealengine.com/marketplace/en-US/product/magic...
I looked into using this approach when I was playing around with the idea. Writing lots and lots of C wrappers seemed … wasteful.
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!
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.
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.
> the productivity gained by fast iteration times is honestly exponential
Preaching to the choir! Iteration speed is GodKing. But I’ve never seen a large game engine that had sub-second compile and startup times. C++ is a rather miserable language and just including any STL header can break that target.
I respect that making FFI bindings to Rust would be very difficult or impractical, but that's what I think would be interesting.