Systematic Gaming

August 29, 2008

Functional Gaming: Writing a game in OCaml

Filed under: game programming, ocaml — Tags: , , — systematicgaming @ 2:25 am

If you’re following the latest trends in multi-threading you may have seen some renewed interest in functional programming.

When you bring up functional programming you’ll often get one of two opinions:

  • It’s the silver bullet of parallelism, and will allow automatic parallelizing of code, and also is easier to read, write and creates less bugs.
  • I remember those from university, they made my head hurt and ears bleed. Nobody uses those type of languages in the real world.

I find you’ll get more of the second responses, because really, who codes in functional languages outside of university?

Of course I’m biased since I generally hang out with other game programmers. If you program for consoles you do it in C or C++, since there aren’t any other compliers provided. So nobody I know uses functional programming on a daily basis.

That said, I’m intrigued by the possibilities offered by functional programming. I can see how they can parallelize well, and they allow you to write some clean mathematical code. To form a more informed opinion I decided to start exploring the world of functional programming.

I first started with Haskell. Coming from a C++ background, it was a bit of a challenge to get into the right frame of mind, but I was able to persevere through the recursive functions, strange type system and lack of modifiable variables to create this masterpiece: Tic-Tac-Toe. I won’t say how long it took me, it’s embarrassing, but it does work, and plays the perfect game. I did learn a lot about the language even if my code doesn’t use many advanced features of the language.

So feeling a bit more confident with functional programming I’ve decided by next step is to make a more complex game, but this time in OCaml. Why Objective Caml? No real reason, I’ve heard it one of the faster functional programming languages, rivaling C/C++ for speed. Also it looks like OCaml has some straightforward support for calling C libraries, which could be useful.

This time however I’m going to go for a more complicated game. Since my goal is to learn OCaml, not create an innovate game design masterpiece, I’ll just make a clone one of those tower defense games. Except with shittier graphics.

I’m using GLCaml as a base (which is a bit of a pain to setup, but thankfully someone explained the steps). Here’s the simple framework program I’ll start from:

It doesn’t do much, left click to place a “tower”, right click to delete one. The game currently is just a test that I’ve got my system all setup and ready and have a main event loop in place.

So far my OCaml thoughts are

  • It’s similar to Haskell in syntax, but feels messy. Hard to describe, but OCaml’s base syntax just doesn’t seem as clean. Maybe too much syntactic sugar.
  • Some of the syntax just looks ugly, like the double semicolons ;; and the specialized float operators +., *., etc. just make math look ugly.
  • The compiler errors are pretty useless most of the time, with the generic “Syntax Error” being not very helpful. I’d thought Haskell’s cryptic errors were bad, but OCaml are useless other than the line number.
  • What’s up with “let rec” to define recursive functions? Recursive functions are the bread and butter of functional programming, it’s a pointless keyword as far as I can tell.
  • What’s up with enforcing dumb formatting rules? Like how functions must start with lowercase letters. It wouldn’t bother me so much if the compiler actually gave useful error messages, but it look me quite a while searching for “syntax errors” before I realized what I had done wrong.
  • I found the Haskell tutorials, especially Haskell for C Programmers, provided a better introduction than the ones I found for OCaml.

That’s the bad points, but I do like a few things

  • Relaxed about side effects. This was a pain in the ass about Haskell. Especially when you’re not familiar with the language or development environment being allowed to drop printf’s throughout your code really helps.
  • There is enough of a community that stuff like SDL wrappers are available with some simple instructions to install and get it all working.

I’ll be posting occasional updates showing progress of the game as well my thoughts on OCaml and writing games in a functional language.



  1. Interesting… I hope you will update us how it will go. I was just lately thinking about starting doing the game in ocaml again.

    Comment by jankoM — August 30, 2008 @ 9:59 pm

  2. > What’s up with “let rec” to define recursive functions? Recursive functions are the bread and butter of functional programming, it’s a pointless keyword as far as I can tell.

    The reason for rec is that by default, you can redefine things in terms of previous things. So for example, say you defined foo, and now want to redefine foo in terms of the previous foo. Using rec tells the compiler to bind to _this_ version of foo, not the previous version.

    Comment by wtetzner — August 27, 2011 @ 1:54 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: