Making a game in Bevy: An open-source 2D and 3D engine built on Rust
Tags: programming, rust, gamedev, todayilearned
Published: 2021-04-16 04:28:57

<div class="modal-body">
<p>I earlier wrote about all of the promises that <a href="https://unity.com/solutions/instant-games">Unity’s Project Tiny </a>brings to the table with the ability to render 2D and 3D games with remarkable speed and small size. But for all the good that the ECS brings to the table, there are a lot of complications that it brings to the table. Enter <a href="https://bevyengine.org/">Bevy</a>, an Open Source data-driven game engine that is built in&nbsp;Rust.</p>
<p>Bevy, which code is available in <a href="https://github.com/bevyengine/bevy">Github</a>, contains a number of examples in the <em>Examples</em> folder. Aftering getting the project, navigate to the examples folder and run <em>cargo run — example breakout.</em></p>
<p>Taking a closer look at the examples, you can see that the Bevy engine and game logic is all built on a custom Entity Component System (ECS). ECS is a software paradigm that involves separating data into Components. All of the entities are assigned unique ids. An Entity makes up a group of Components.</p>
<p>The benefit to Bevy is that it uses normal Rust data types for all of these core elements. For example, Components are Rust structs, Systems are Rust functions, and Entities are a type that has a unique integer property. Because Components can be packed tightly together in memory, their read time is blazing fast. Systems have a read/write dependency tree that allows for a lock-free parallel scheduling.</p>
<h3><strong>Features</strong></h3>
<p>So let’s look at some of the examples of the features:</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YKFl_FAManYD8f3k"><figcaption>For Each System Source:&nbsp;<a href="https://bevyengine.org/news/introducing-bevy/">Bevy</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*110Gck3WDIp6AcS_"><figcaption>Multiple Queries Source:&nbsp;<a href="https://bevyengine.org/news/introducing-bevy/">Bevy</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TzU_DaxREyVedf4G"><figcaption>Spawn Commands Source:&nbsp;<a href="https://bevyengine.org/news/introducing-bevy/">Bevy</a></figcaption></figure><h3>Scenes</h3>
<p>Similar to Unity, scenes are a way to compose objects of your game ahead of time into a structure. In essence, it is a collection of entities and components. A scene can be “spawned” into the world (even multiple times!). Scene files can be written out to disk and are stored flat as&nbsp;JSON:</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/934/0*j0lY5EQ_MYrdEqEh"><figcaption>Sample Scene json Source:&nbsp;<a href="https://bevyengine.org/news/introducing-bevy/">Bevy</a></figcaption></figure><p>The SceneSpawner resource is what you can use to call to load a scene into the world space. Once in the space, the objects preserve their entity IDs and can be manipulated at&nbsp;runtime.</p>
<h3><strong>Events</strong></h3>
<p>Bevy offers a zero-allocation event consumer system when trying to interact with things happening in the scene. This is used for things like window resizing, assets, and&nbsp;input.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*v6vOBTgl3-bA-YGF"><figcaption>Collider Detection Source:&nbsp;<a href="https://bevyengine.org/news/introducing-bevy/">Bevy</a></figcaption></figure><p>So for all the hype does Bevy live up to the claims of being a Capable and Simple system? I’ll leave that up to you to&nbsp;decide.</p>
<p><strong>Thank you for reading this far. If you enjoyed this post, please share, comment, and press that ♥️&nbsp;!</strong></p>
<p><strong>Follow me on </strong><a href="https://twitter.com/maurerpwer"><strong>Twitter</strong></a><strong> and DEV if you’re interested in more in-depth and informative write-ups like these in the&nbsp;future!</strong></p>
<img src="https://medium.com/_/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=20fecd090352" width="1" height="1" alt="">
</div>