How to Make a Game With React

Rate this content
Bookmark

We make websites with React. We make apps with React. But how do we make games with React? In this presentation I will build up show you how to build up a React game engine tecstack using Poimandres libraries, explain the core concepts and, yes, even build a game.

This talk has been presented at React Summit US 2024, check out the latest edition of this React Conference.

FAQ

Entities in the game are managed using traits and behaviors. Traits hold data and define entity characteristics, while behaviors are small, composable functions that dictate entity actions, updated every frame using a central frame loop. This setup allows for dynamic and interactive game environments.

React 3 Fiber allows developers to build 3D scenes declaratively in React, similar to how DOM elements are managed. It integrates fully with HTML and the React ecosystem, enabling the use of familiar tools and libraries in a 3D environment. It is powered by 3JS, providing robust 3D capabilities.

Composable behaviors allow for modular and manageable game logic, where each behavior represents a small, independent function that can be combined to create complex interactions. This modularity facilitates easier debugging and iteration, making it easier to refine and expand game functionality.

The key components used are React 3 Fiber, which allows for declarative building of 3D scenes in React, and Cota, a state management library efficient for updating high volumes of data quickly. The development also employs tools like Triplex for a visual IDE experience.

The purpose of using React for game development is to leverage the tools and knowledge that web developers already have, making it easier to bring game development to a wider audience without having to learn new programming languages like C# or C++. React allows for quick iteration of ideas, which is a significant advantage in game development.

Yes, the approach can be adapted for React Native since the state library Cota is agnostic and can be used with vanilla JavaScript or React Native. However, it may not be as fast on React Native due to limitations like the absence of JIT (Just-In-Time) compilation on Apple devices.

Triplex provides a visual IDE that helps developers move and arrange elements within the 3D environment visually. It aids in saving configurations back to the code, enhancing the development workflow by providing a more intuitive interface for managing 3D objects.

Cota is a state management solution designed to efficiently update high volumes of data quickly. It acts as a local database that integrates with React to manage the dynamic and high-frequency updates required in game development, supporting thousands of objects and a dynamic structure.

Kristopher  Baumgartner
Kristopher Baumgartner
22 min
19 Nov, 2024

Comments

Sign in or register to post your comment.
Video Summary and Transcription
I'm here with the Poimandris Open Source Dev Collective, and I'm going to show you how to make a game with React using patterns you know and probably a lot you don't. We'll use React, React 3 Fiber, Kota, and Triplex. Cota is a nimble state solution that creates a local database for efficient updates. Spawn entities with specific traits and update them accordingly. Learn about composable behavior in Kota and how to update entities every frame. Enhance the visual appearance of the game by adding a HiFi view, materials, lights, and post-processing for bloom. Understand the concepts of creating games in React, dynamically spawning entities, and giving them behavior. Learn about testing strategy, compatibility with React Native, and the advantages of using React for game development, including wider accessibility and quicker iteration.
Available in Español: How to Make a Game With React

1. Introduction to React Game Development

Short description:

I'm here with the Poimandris Open Source Dev Collective, and I'm going to show you how to make a game with React using patterns you know and probably a lot you don't. We're going to do this by talking about rendering, state management, spawning, and behavior. We'll use React, React 3 Fiber, Kota, and Triplex. React 3 Fiber lets you declaratively build 3D scenes in React, fully interactive with HTML and powered by 3JS. It looks like a regular React component with a mesh, geometry, and material. We update quickly with the useFrame hook.

Hello, hello, my name is Chris Baumgartner. I'm here with the Poimandris Open Source Dev Collective, and I'm going to show you how to make a game with React using patterns you know and probably a lot you don't.

In fact, it'll be this game right here. We're going to do this by talking about rendering, how we draw things in 3D with React. Then we're going to talk about state management, what's our strategy for updating data very quickly in React. We're going to talk about spawning, what's our patterns for dynamically creating and destroying entities, and we're going to talk about behavior, how we make our entities do things. Then we're going to give the whole thing a nice glow-up. To do that, we're going to use, of course, React. We're also going to use React 3 Fiber and Kota. Both of these are libraries created by Poimandris and worked on by myself. We're going to use this tool called Triplex. It's going to give us a visual IDE for moving stuff around and saving it back to code.

Let's start with rendering. React 3 Fiber probably does not need an introduction for a lot of people, but in case you're not familiar, it lets you declaratively build 3D scenes in React, just like you do with DOM. You get to do that with 3D. It's fully interactive with HTML. It participates in React's ecosystem, which means all the same tools and libraries you use, you can use in 3D. It's powered by 3JS, so if you're wondering how the magic happens, you can look up 3.

This is what it looks like. It looks a lot like a regular React component, except we have here a mesh. Inside the mesh is a geometry and a material. The geometry tells us the shape our mesh is going to have. The material tells us what it's going to look like. Here it's going to change color from red to blue when we click on it. Then in our app, we have a canvas. That canvas here is the 3D environment that we're going to draw into. This is what it looks like. Just a little bit of code, we get a 3D interactive piece. One additional difference with React components is that we often have a need to update in a frame loop very quickly. We have a hook called useFrame.

2. State Management and Entity Creation

Short description:

It is a callback that's going to happen in a request animation frame in our central frame loop. We had three entities: player, enemies, and bullets. Our player moves with the keyboard, shoots bullets, and enemies flock towards the player. Bullets collide with enemies. We start with wireframes for functionality. State management is crucial for games as current solutions are too slow for high-frequency updates and many objects.

It is a callback that's going to happen in a request animation frame in our central frame loop. Here we're just getting the ref for the mesh and we're rotating it each tick of the frame.

Our game, we had three entities that we're trying to create. We had a player, we had enemies, and then we had bullets. We wanted our player to move around with the keys, with our keyboard. We wanted to shoot bullets. We wanted our enemies to flock towards the player, and then we want them to disappear when shot. We want our bullets to move forward fast from wherever they're spawned, and we want them to collide with enemies so we know when to destroy them. We're going to start with wireframes just to make sure we're not worried about the visuals. We're going to worry at first with just how it works.

Here's a quick little video of me doing that. Here I am just taking a player view. It's a box geometry with a wireframe mesh, and I'm creating a view for the enemy and for the bullets, a dodecahedron for the enemy. Then our bullets are just going to be a sphere for now. We're making the bullets scale down. There's a prop there just like in CSS with a transform. We're going to add that to our scene. On the right is triplex, which is a nice little visual IDE. I'm just going to go in there real quick and move them so we can see them. They're not overlapped on top of each other. There's our first scene.

But now how do we make it do something? Now we talk about probably the most important part of this talk, state management. Current state solutions are too slow for what we want to do. They assume a low frequency of updates. They assume relatively few objects and usually also a rigid structure. Just think of your typical React app where you have a hierarchy. It's like a document. You're updating on user interaction or data fetching or an API call. You typically don't have thousands and thousands of objects. By contrast, games are much more demanding.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

The Whimsical Potential of JavaScript Frameworks
React Summit US 2023React Summit US 2023
28 min
The Whimsical Potential of JavaScript Frameworks
Top Content
Watch video: The Whimsical Potential of JavaScript Frameworks
The speaker discusses the whimsical and detailed work of Stripe, particularly their interactive and dynamic pages. They explain the use of React for building whimsical details and tracking mouse position. The speaker introduces React Spring for smooth animation and React3 Fiber for creating a 3D egg model. They also mention the use of Framer Motion and React server components for animating CSS properties.
Emotional & Functional UI Animations in React
React Day Berlin 2022React Day Berlin 2022
28 min
Emotional & Functional UI Animations in React
Today's Talk discussed the importance of UI animations in React, both functional and emotional. Examples were given using CSS and Framer Motion, with a focus on user feedback and accessibility. Design guidelines and animation usage were highlighted, as well as the consideration of negative emotions in animations. The Talk also touched on designing 404 error pages and concluded with gratitude to the audience and organizers.
JavaScript Haikus: My Adventures in Tiny Coding
JS GameDev Summit 2023JS GameDev Summit 2023
27 min
JavaScript Haikus: My Adventures in Tiny Coding
This Talk is about writing super tiny JavaScript programs, known as tiny code. It explores the concept of code golf and the use of a live editor for instant feedback. The Talk discusses various ways to share tiny code, including Twitter.net. It also covers creating graphics, games, and sound in a small space. The speaker highlights inspiring tweets and showcases examples of tiny code, including asemic writing. The future of tiny code includes new techniques, better browser support, and AI-assisted programming.
Hacking an e-Reader with React
React Advanced 2023React Advanced 2023
7 min
Hacking an e-Reader with React
Watch video: Hacking an e-Reader with React
React for eBooks? Learn how to hack an eReader to display a tea menu. Create images and write e-books using React. Use EPUB format to create chapters and include CSS. Use Pandoc and Dino to simplify the process and make quick updates.
Hacking an e-Reader to Show My Tea Menu With JSX
React Day Berlin 2023React Day Berlin 2023
7 min
Hacking an e-Reader to Show My Tea Menu With JSX
Watch video: Hacking an e-Reader to Show My Tea Menu With JSX
React can be used to create custom menus for e-readers, and the process involves creating an image and e-book with React and loading them onto the e-reader. Writing an EPUB e-book for e-readers involves converting an SVG file into a PNG image and writing the e-book in EPUB format using HTML, CSS, and images. EPUB generators like Pandoc and Dino simplify the process of generating EPUBs from markdown and running JavaScript on the desktop, respectively.