Programming Toxin – Making Levels

October 25, 2012 Toxin 0 Comments

Nearly everyone who asks me about game development will begin with the question, “How do you create levels?”

The main way of going about it is to use a custom editing program. Developers either write their own specifically for each game, or they’ll use one provided by a third-party game engine like Unity, Source or UDK. Sometimes, they might take a popular modding tool like one of the Quake editors and use it for their own ends.

Unity EditorTiled 2d map editor


These editors allow you to create levels visually; you can draw maps, position items and enemies, draw movement paths, edit dialog text and set up events to occur at different points in the level. All this information is saved in a structured file that the game is programmed to understand.

The complexity of these tools and the files they create depends on the game. Some of them are as large and complex as the 3d modelling tools used by animators and architects; others are simple programs coded up in an afternoon that allow a programmer to avoid some laborious data entry.

I’ve always enjoyed writing game development tools. My first map editor was written in 1994 in Blitz Basic on the Amiga for a strategy game I designed that went nowhere. I probably spent more time polishing the editor than working on the game itself. Not long after I wrote an isometric editor that let you sculpt terrain like in Populous, again, for a game that failed to materialise.

My last game, Starblaster didn’t use an editor; the levels were stored as a script, but when I started Toxin, my forthcoming iPhone game, I knew it was time to break out the old skills.

The Toxin Level Editor

Toxin Level Editor main interface
The Toxin Editor uses the same Opengl graphics engine as the iOS game

The Toxin Level Editor is my first ever Mac program, and it’s pretty clunky. It was my first attempt at writing a program in Objective C, using the famous Interface Builder which originally came out of NeXTStep. I always had a thing about NeXTStep, so it was exciting to finally write a program with its direct descendant.

Once I got the basics up and running, I needed to decide how I was going to save the levels. While many developers create their own file formats, I decided to use an existing one. That way I could use an off-the-shelf library to save time and effort.

Having worked in web application development for the past five years or so, I had some experience with XML and JSON. These are text based formats commonly used by web services to exchange information. I chose JSON as my level format because of it’s simplicity and readability. I use the SBJSON library in the editor and in the game for loading and saving.

Levels in detail

Toxin levels contain four main things. A list of cells, a list of linear events, a list of timed events and a set of general preferences that are applied to the whole level.

The cells are toxic blobs which spawn and multiply and gradually fill the playfield. The editor allows me to draw cells into the level and directly set their parameters.

Events in Toxin are instructions to the game to perform an action such as add an enemy to the game, set off an explosion or play a sound effect.

Linear events are fired off one after the other from the start of the level. They are the main way I add mobile enemies to the game. I can split the events into waves using a WAIT event which instructs the game not to add events until the previously created enemies are all dead. Enemies have standard parameters for position and angle, and a set of custom parameters defined as key, value pairs.

Timed events let me perform actions and spawn enemies at a specific time, or at a random time within a range. I did toy with the idea of having rule based events that I could fire off when certain game conditions were met. In my next game I plan to embed a scripting language to handle this kind of thing.

Editing Level Events
Adding an entity to the level. The linear events list is used to construct consecutive waves of enemies. The entities can have many custom parameters

The levels are saved as a bunch of text files which are then zipped and put into the game. I think this was a mistake. While it’s a good technique for larger games, with Toxin I could have got away with storing them in a single file.

If I were working on a desktop game I would try and build the editor into the game itself, as well as give the game a console that lets me manipulate level data and game parameters during play. This isn’t really feasible on a mobile app, though you might be able to do it on a tablet. I do get tired of having to zip all my level files, then replace the zip inside the project and recompile every time I want to try something.

I have a lot more Toxin posts planned, so stay tuned. Let me know if you have any questions or want more details.