Nov 2, 2023

Building a text adventure game using GPT-4, part I

Growing up, I loved interactive fiction games like Zork and Deadline. Can AI spark a rebirth?
DALL-E 3

One of the reasons I founded The Andrew McGill Company was to carve away a bit more time to hack on side projects.

After a busy first few months, I’m excited to drop the first of my experiments — an ongoing experiment in using generative AI to make a video game 🎉

The game

I was a big fan of “interactive fiction” games growing up — Zork, A Mind Forever Voyaging, Deadline.

The opening screen from Infocom's Deadline, outlining the beginning of the adventure.
Deadline's opening screen.

Deadline (👆) came out six years before I was born, but by the late 90s, you could download most Infocom games for free. I loved how detailed they were — and how much freedom they gave you to explore worlds and solve problems creatively.

Of course, you weren’t really free to do whatever you wanted — the entire time, you were on carefully constructed rails, with all the narrative dead ends neatly rounded off. At no point in Deadline, for instance, could I decide I wanted to leave the Robner estate and get a pizza.

Fast-foward to 2023. AI models like GPT-4 have made generating text of astonishing complexity as simple as writing a prompt. As I began experimenting, I began to wonder… could an open-ended text adventure game finally be possible?

I’m on a mission to find out — or, alternatively, build a lot of fun prototypes.

How this series will work

A few rules:

  • Ship ship ship. I’m going to run through a lot of experiments quickly…
  • Write write write. … and blog about them quickly, too.
  • The games ideally should be fun. They don’t have to be, but they should point in the direction of fun.
  • I’m cheap. The games shouldn’t cost more than a couple bucks in OpenAI requests.
  • Lastly, I’m busy. I might have to put the series on pause if work or life heats up.

OK, onto the game:

Game 1: The Mysterious Space Station

Dall-E 3

My first attempt is really simple.

  • What if I gave GPT-4 a prompt setting up the background of a game…
  • … asked it to return a JSON file with a description of what’s happening and a number of actions the player could take…
  • … had the player pick one of the actions …
  • … then fed that back to GPT-4, asking it to spin up another description-action JSON object…
  • … and kept this loop going, feeding GPT-4 an ever-growing transcript of game events?

Here’s the result: https://gpt-game-eta.vercel.app. Give it a shot, I’ll front the OpenAI credits until we hit $10 for the month.

The opening screen from one of my play-throughs.

If you want to dig into the source code, the real magic is in the constants file, where I set up the prompts.

If you poke around in there, you’ll see that I set up the adventure by casting the player as Han Greedo, an intergalactic smuggler. They’ve docked with a mysterious abandoned space station, which harbors a secret…

As the player presses further, they encounter Vase Windex, who sheepishly admits they accidently turned the entire crew into coffee cups.

It’s then up to the player to resolve the situation before they’re transmogrified themselves.

Learnings

I have a few:

  • GPT-4 does a good job of thinking on the fly — presenting the player with good descriptions and actions that make sense.
  • With prompting, it can also follow the outlines of a plot, though its pacing can be a bit weird. As I raced to solve the coffee cup mystery, the game kept warning me that I’d turn into a coffee cup soon… but it never actually happened. That said, I’m impressed it was able to hold the thread even as the accumulated prompt.
  • GPT-4 is unforgivably slow. This program requires a live internet connection and makes a request to OpenAI after every player selection. GPT-4 sometimes took as long as 20 or 30 seconds to respond.
  • GPT-3.5-Turbo writes a worse game. The more basic model is a lot faster, but ultimately writes simpler prose, follows the plot less closely, and is more likely to get stuck in loops (”hey, do you want to look at this control panel AGAIN?”).
  • This game would be really expensive to bake out as static JSON. I’d love to ditch the OpenAI connection altogether and just bake out a bunch of JSON files of various decision trees. But exponential math means this gets expensive fast. If every narrative beat has four possible actions, baking out a game that lasts 10 turns would result in over a million JSON objects (and would cost a couple thousand dollars in OpenAI API fees).

Next experiments

Lots to consider.

  • Can I write the opening prompt better so they are multiple characters and in-game events with whom the player must interact?
  • Can I build a version of this that’s more conducive to baking out as static JSON?
  • Can I have GPT-4 make up events and milestones, and intelligently add those to the prompt as the adventure progresses?
  • Can I add an element of randomness or true risk?

Until next time!

Dall-E 3
Whatever you’re looking to build,
I’d love to chat. Drop me a line.