Stick to UTF-8 and these three character sets

Handling text is an important part of every programmers life, and there are many subtleties to it. Lately I have started to realize that all text I handle in my everyday work falls into three categories:

  • Free text: this includes names of people and places, contents of chat messages, books, etc.
  • Code: this includes C++, JSON, HTML, etc – anything that is both human and machine readable and writable.
  • Identifiers: these are unique names, e.g. instance keys in JSON, file names, database keys and resource names.

I think we as programmers can make our lives much simpler if we decide to stick to these three categories, and to all agree on their character sets and encodings.

Read More

Simple coroutines for games in C++

In games it is very common to have scripted sequences - for instance, a piece of dialogue or a cutscene. I don’t mean these sequences are written in a scripting language (though they might), but that they follow a script like a play or a movie does. Of course, contrary to a movie script in a game the script may have some conditional branches (an NPC only saying something if the player choses to ask a certain question, for instance).

Expressing such a sequence in code should be straight-forward  – after all, computer code is very much like a script. However, these scripts are often span minutes and happen while a bunch of other things are also happening. The now obvious solution to this is to put these in a separate thread - but now you open yourself up to race conditions and other nasty thread-related bugs. So what to do?

Read More

Error Context - a stack trace for data

Often a crash is the result of unexpected data. For instance, a crash could be caused by an extra space at the end of a string, or an integer being negative when your code didn't expect it to be. Or maybe the crash happened during the processing of a bunch of files and you want to know the name of the file that caused the crash. What you really want is the ability to inspect the values of arguments and locals at different parts of the stack. But how can we write that data to a log file on a crash without writing it when there is no crash?

Read More

The fastest code is the code that never runs

It was early 2014, and I was working at Arrowhead Game Studios on our reboot of the classic arcade game Gauntlet. It was the week before we where going to present our game to the world for the very first time. We where going to let the public play a select level from the game, and over the last weeks the team had been busy fixing bugs, refining gameplay and tuning the visuals. It was looking good, but during the last week something happened: the frame rate dropped from the desired 60 FPS to a crawling 20-25 FPS. Panic! What had gone wrong? And did we have time to fix it?

Read More

Lessons after a year with Lua

When I started working at Arrowhead Game Studios a little over a year ago, I came from over a decade of using statically typed languages, the main one being C++. At Arrowhead we wrote most of our game code in Lua, which is a small but sweet scripting language. In this article I will summarize the main lessons I've learned, and how scripting languages in general, and Lua in particular, related to statically typed, compiled languages such as C++.

Read More

The Myth of RAM, part I

If you have studied computing science, then you know how to do complexity analysis. You’ll know that iterating through a linked list is O(N), binary search is O(log(N)) and a hash table lookup is O(1). What if I told you that all of the above is wrong? What if I told you that iterating through a linked list is actually O(N√N) and binary searches as well as hash lookups both are O(√N)?

Read More