Using Rust for Game Development [video]
I found this excellent talk to be complementary to my talk[1] on data-oriented GUI in Rust, not to plug my own work too much.<p>I found a lot of common ground:<p>* Trying to write object-oriented code in Rust doesn&#x27;t work well. Writing data-oriented code does.<p>* &quot;Fighting the borrow checker&quot; is a sign you might be doing it wrong. Try data-oriented approaches instead.<p>* Use a structure-of-arrays rather than an array-of-structures.<p>* Use what I call &quot;state splitting&quot; to borrow mutable references to just the state you need; this works well with the structure-of-arrays approach and is a powerful motivator to use it.<p>* To build graph-like structures, reach for a Vec of components, and indexes into the Vec for relationships, as your first choice. Self-references, arena allocators, and Rc are viable alternatives, but you should have a good reason to use them instead of Vec.<p>* Some form of dynamic typing is useful to keep your system loosely coupled (though we ended up with very different forms of dynamic typing, see below).<p>* Data-oriented approaches have taken root in the C++ gaming community, mostly motivated by performance, but adapt well to Rust, and some of the ideas may be useful in domains beyond gaming.<p>There were some other points that went beyond what I talked about, but could fit in well:<p>* A &quot;generational index&quot; is a good way to avoid use-after-free style errors that result from the use of a stale index. The slotmap crate can help.<p>And now for the things that are different.<p>* The exact nature of dynamic typing is different. An ECS usually uses a registry of the different component types (anymap is a useful crate) and is quite open-ended in adding new types of components. My xi-win-ui, by contrast, has two main component types, Widget and listener, and does dynamic dispatch on a Widget trait.<p>This naturally raises the question, which is better? From my perspective, both are valid. The pure ECS approach definitely makes sense when the components are interacting with each other in diverse ways (collisions, damage, etc). In a GUI, the components are mostly interacting with the framework, and have diverse behaviors within standardized interfaces (input, layout, paint).<p>Thus, one point of my talk is that you don&#x27;t have to reject all vestiges of object oriented programming, it&#x27;s perfectly reasonable to hybridize, using data-oriented approaches for state splitting and graph structure, and dynamic dispatch for the behavior variation.<p>My talk also went deeper into the ideas of &quot;data flow rather than control flow&quot; and the use of an event or command data structure rather than method calls. I&#x27;m not sure how deeply these ideas apply to games, but wouldn&#x27;t be surprised if they do.<p>[1] video: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=4YTfxresvS8" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=4YTfxresvS8</a> , slides: <a href="https:&#x2F;&#x2F;docs.google.com&#x2F;presentation&#x2F;d&#x2F;1aDTRl5R-icAF38Di-qJ4FzAl3pLlutTKVFcr3mUGgYo&#x2F;edit?usp=sharing" rel="nofollow">https:&#x2F;&#x2F;docs.google.com&#x2F;presentation&#x2F;d&#x2F;1aDTRl5R-icAF38Di-qJ4...</a>