Designing an API for a Video Game
Tags: gamedev, devlog
Published: 2022-03-14 17:24:02

---
title: Designing an API for a Video Game
published: true
date: 2022-03-13 17:00:00 UTC
tags: gamedev,devlog
canonical_url: https://www.jdno.dev/designing-an-api-for-a-video-game/
---

![Designing an API for a Video Game](https://images.unsplash.com/photo-1599110906632-28d25094470f?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDEwfHxibHVlcHJpbnR8ZW58MHx8fHwxNjQ3MTYwOTIy&ixlib=rb-1.2.1&q=80&w=2000)

I am working on a video game for programmers. The game is played by writing code that interacts with the game through an API. This post explores the design of the API that connects the player's code, the API layer, and the game simulation.

## A Video Game for Programmers

I have been thinking about a [video game for programmers](https://www.jdno.dev/why-i-want-to-build-a-video-game-for-programmers/) for a while now. What differentiates such a game from others is that it is played by writing code. The player cannot interact with the game world themselves but only through an API. Essentially, they create a program that plays the game for them.

The time has come to make this dream a reality and build a working game. I am working on [Auto Traffic Control (ATC)](https://dev.to/jdno/auto-traffic-control-a-video-game-for-programmers-f9k), a game in which players have to safely route airplanes to an airport.

## ATC as a Distributed System

Over the past two years, I experimented with different ideas for the API and the architecture of the game. One prototype had a [REST](https://en.wikipedia.org/wiki/Representational_state_transfer) interface, another tried to wire RPC calls into the core loop inside the game engine, and there were a few that explored various asynchronous interfaces such as message queues. But all of them eventually ran into problems, with a root cause that in hindsight is totally obvious:

At its core, the game that I want to build is a [distributed system](https://en.wikipedia.org/wiki/Distributed_computing).

There are three components that are independent of each other: the player's code, the API layer, and the game simulation. The player's code runs in its own process and is totally decoupled from the game. But even the API layer and the game simulation are only loosely coupled, and run in parallel in different threads. The result is a system in which all components run concurrently, and any attempt to introduce tight coupling between them is bound to fail.

## Event Sourcing and CQRS

The architecture for the game takes a lot of inspiration from [Event Sourcing and CQRS](https://danielwhittaker.me/2020/02/20/cqrs-step-step-guide-flow-typical-application/). The player sends a _command_ to the API, which validates it and then puts it into a queue. The different systems that power the simulation inside the game take commands from the queue, and make the corresponding change to the simulation. The change in turn triggers an _event_, which is send back to the API via another queue. The API receives the event, and sends it over a stream to the player.

![Designing an API for a Video Game](https://www.jdno.dev/content/images/2022/03/2022-03-13-ATC-Architecture.png)

The diagram above visualizes this architecture inside the game. The API on the left has different services that each manage a single resource. The event source streams events to the player, while the airplane service allows players to interact with the airplanes in the game. The API is coupled with the simulation on the right through two queues: the _event bus_ and the _command bus_. The game is built with the game engine [Bevy](https://bevyengine.org/),  which runs many _systems_ in parallel to simulate the game. All systems are connected to the event bus so that they can publish any change they make to the game world. And systems that can be influenced by the player are connected to the command bus as well.

This architecture has worked really well in experiments, and I can't wait to see how well it'll work for a full game. Stay tuned to learn more about that in the future!

## Follow the Project

If you're interesting in this game or programming games in general, make sure to follow along. I am not sure where the road will take us, but I am very excited for the journey!

Subscribe to my blog to receive weekly updates about the progress on the project.

[jdno.dev](https://jdno.dev)

I also stream some if not all of the development of this game on Twitch, so follow me there as well.

[jdno_dev - Twitch](https://www.twitch.tv/jdno_dev)