Why I switched from Ruby back to C++

January 2012
UPDATE: This post was pretty popular. I've posted a followup here.

After two months of Sol Trader development in Ruby, I took a difficult decision last Wednesday morning: I’ve decided to rewrite the game code from scratch in C++. Let me explain my reasons.

If you'd like to purchase Sol Trader you can now do so at soltrader.net!

Why I did it

  • Slow frames: When working with Ruby, I use the excellent Gosu library to do all my game specific coding. This initially worked great, but occasionally I’d just get slow frames coming up. My game is timed to run at 60 frames per second, which means that each frame should take no more than 16.67ms to run. Yet every so often my profiling would come up with a frame that would take 25ms or 45ms for no discernible reason. I just couldn’t find the issue here: I turned every sub system in the game off. I disabled garbage collection. I hacked my slow frame detection code into the simplest gosu sample I could find, and they still existed. I didn’t feel like I could quite trust the stack to deliver the framerate I needed, and I hadn’t yet put in half the features I wanted to.

  • Object explosion when bridging to C: A lot of the libraries I was using were written in C, and therefore there was several thousand objects (mostly floats) being created each frame to act as a bridge between Ruby and C code. It feels like that that CPU time should be better spent in the AI improving the quality of the simulation, or on better effects, rather than loading the garbage collection with an unnecessary burden.

  • Ease of packaging and distribution: I feel like packaging is going to be a lot easier. I’m not too bothered about hiding the source code: I may well do that anyway to purchasers of the game. It’s the running on Windows I’m worried about: from my research it feels like it’s going to take some effort to push the game out on a non-Unix platform. And with a video game, releasing on Windows is a must.

  • Manual memory management for performance: The garbage collection is still too stuttery under MRI (even with Ruby 1.9.3, which is a huge improvement on what’s gone before) - it still stops the world each time. I looked at other implementations, and even considered learning all about garbage collection to help improve Ruby myself, but then realised that getting royally distracted wouldn’t help me ship a working game.

What do I miss about Ruby?

  • I miss using RSpec hugely: There are ways of doing testing of C programs using RSpec but it doesn’t feel like I want to wrap each of my C++ classes with a SWIG interface just to check they’re working. I may still do this for some form of Cucumber testing, I’m not sure. I’m using UnitTest++ for my testing at the moment, which is very lightweight and good enough for my purposes.

  • Duck-typing: defining interfaces for everything is a pain in the backside, although it does force you to think more clearly about the roles of your classes.

  • Easy mockist testing: There’s no built in reflection in C++ so it also makes you have to code to interfaces if you want to do any mockist testing. I’m mostly returning to a classist style of testing with small well defined groups of classes being tested at once. It’s not a perfect system and I still have much learning to do here.

  • Terseness of syntax: There’s just a lot more characters to type, and a lot more ceremony for each class. This tempts you to larger classes and methods, which I’m resisting at the moment. I need to take the time to set up c.vim exactly how I want it.

Funnily enough, I don’t miss the automatic memory management: I like having that level of control. Old habits die hard.

So how far have I got?

Thankfully, it’s not a complete rewrite as I’d already done a lot of thinking about the architecture and a lot of the basic classes translate directly over. I worked really hard at the end of last week and got a lot done:

  • I put in SDL to build the basic game framework: hopefully building on Windows will be a snap. I plan to have a working Windows build as soon as I can lay my hands on a cheap Windows 7 PC.
  • Basic testing using UnitTest++, with tests that are compiled and run as part of the build process.
  • Decoupled gameplay/physics updates from the graphical framerate using the techniques here. I have zero temporal aliasing bugs right now, which makes for a super smooth 300+ FPS graphic loop with a fixed 60FPS physics loop.
  • Re-implemented physics using Chipmunk, the same library I used in Ruby, which made it very easy to switch over.
  • Put in a brand new and much improved parallax-scrolled starfield.
  • Added a basic controllable spacecraft, planets and jumpgates back in: the player can fly around as before and collide successfully with other objects.
  • A simple particle system so the spacecraft give off exhaust smoke, and the jumpgates emit spooky purple mist.

Even with using OpenGL immediate mode (this is a bad thing) and rendering 10000 stars each frame (very inefficient), and a throwing bunch of particles onscreen, I’m still getting 300+ FPS on my 2009 MacbookPro and only using 30MB of memory. That’s satisfying.

UPDATE: By (very) popular demand, here’s a screenshot. Be aware this is three days work only, and obviously not final artwork:

Next I plan to add back in jumping between different planetary orbits, and then work on a very basic ‘person-level view’, so that you can get out of your ship and walk around.

Was it the right decision?

I’ll know at the end of the project :) My feeling is though that it was the correct thing to do. Being really close to the metal will make it much easier to implement some of the really complex AI stuff I’d like to do later on. I already know C++ very well, and estimate it’ll only delay me a week or two if I work hard. If I like, I can always bundle my project as a C++ library and control it from Ruby later on, but it’s harder to go in the other direction.

What do you think? Did I make the right call?

If you'd like to purchase Sol Trader you can now do so at soltrader.net!

Share


More articles

Ealdorlight: A Kickstarter retrospective

Ealdorlight Banner

It’s now been over three months since the end of the Ealdorlight Kickstarter campaign. I’ve deliberately been taking some time to think and learn from the fact that it didn’t reach the target, and to work out what to do next. Frankly, I was pretty upset that the campaign didn’t make it, and it’s taken a while to get over it.

It’s also taken a while to think through the campaign properly. Some things are obvious in hindsight, and others less so. A lot of post-Kickstarter analysis feels like a stab in the dark. Nevertheless I’ve given it a lot of thought, and these are my best guesses for why I think Ealdorlight’s Kickstarter failed:

Read more

Ealdorlight's Kickstarter is live at 4pm today

TL;DR: Ealdorlight's Kickstarter campaign is live at 4pm today! Go and back it!

header

The sixth of June is a significant day for me personally. In 2004, I spent the entire of the day in hospital. I remember the 60th anniversary commemorations of D-day on the TV in the background, as I sat beside my wife, in labour with our first child. I became a father an hour after midnight on the 7th June; my son becomes a teenager tomorrow.

Twelve years later, in 2016, I spent the entire of 6th June glued to Steam watching and waiting whilst my first game Sol Trader was released to the world. This was a career dream come true: since I started programming at six years old I’d always wanted to create and ship my own games. Sol Trader’s release was ultimately a painfully formative experience for me, which I wrote about at the time and was interviewed about recently in GamesIndustry.biz.

Over the last year, I’ve been keeping busy doing two things. One is to support Sol Trader as much as I can with countless updates and patches. I’ve also been very busy working on a new game, Ealdorlight, a medieval RPG-style take on Sol Trader’s mechanics, with turn-based combat, realistic damage and great graphics. I announced Ealdorlight in March and demonstrated it at Rezzed, strengthening my hope that the idea was a good one.

I decided fairly early on that I wanted to take Ealdorlight to Kickstarter. Sol Trader’s successful Kickstarter was a brilliant experience. The Kickstarter community is one of the kindest, most positive on the Internet. I also needed funding for this game: Sol Trader was self-funded through many long evenings and contracting work, and for Ealdorlight I need a bigger team to realise the vision. It’s built in Unreal Engine 4, which simultaneously saves me loads of development time and means I need a bigger team to pull off the realistic art style I’ve gone for.

As time came near to launch, the first anniversary of Sol Trader’s released seemed an appropriate day to launch the campaign. So today, 6th June 2017, I will spend the entire day glued to Kickstarter as my campaign goes live at 4pm today.

Visit Ealdorlight’s Kickstarter Campaign

kickstarter launch

There’s plenty more about Ealdorlight on the campaign - head over there and read all about it! A huge amount of work has gone into it, and I’m very grateful for all the support and help I’ve received from the team I’ve put together, and for friends and family who have given me endless encouragements and feedback.

This post is a little earlier than 4pm so that you can watch it go live if you want. Earlier backers get lower edition numberings on some of the rewards, so you might want to be there from the start!

Read more

How Ealdorlight's story stands out

random book

As we head towards the Kickstarter campaign launch on June 6th, I want to talk a little about the story behind Ealdorlight works.

The basic story stays the same for each game: you are discovered wandering through a remote village at a young age, and realise your destiny is to overthrow the King. However, like in Sol Trader, every person you meet is randomly generated. This means that your real identity will be different every time, and you’ll have to discover it all over again every time you generate a new game.

Handcrafted story in a random world

The trick is layering a great story on top of a generated world with random characters. Building empathy with the main character and his family when all characters are generated is hard, and hinges around being able to hook the story in at the right moments.

My plan is to write plenty of tightly connected story arcs that are triggered on events that happen during history generation. These will in turn trigger future quests the player can undertake. Not all story-arcs will appear in every game: it will depend on how the history generation goes. I will constrain things such that there is always a route through the game, and that players always have a way to overthrow the King, even if that might be easier or harder depending on the starting setup. These story-arcs then should interact with each other, hopefully producing a unique path through the game.

Identity

Ealdorlight is set within a low fantasy world, and there’s no traditional magic. The player gets more powerful through discovering key pieces of knowledge about their past. These insights into of your real past feed directly into your character’s stats, skills and abilities.

I’ve long been fascinated with identity: knowledge of who we truly are affects many areas of our lives for the better. In Ealdorlight I wanted to tell a story which takes this to an almost supernatural level. By removing the player from their birth family, they start as an entirely normal person within the world. It’s only after their early game encounter with the Ealdorlight and the discovery of their past that things begin to change.

Much more on this to come, but in the meantime, here’s a glimpse of our story’s beginning.

Ealdorlight: backstory teaser (updated)
Read more

Ealdorlight Kickstarter on 6th June, Sol Trader 1.3 released

I am now back from some extended time away after Rezzed, both on holiday with the family and training some clients away from home. I’ve released Sol Trader 1.3 today, and set the Kickstarter for Ealdorlight to 6th June.

Rezzed was fantastic: it was great to see lots and lots of people wearing our Ealdorlight crowns. We ran out of crowns on both days, with some creative head displays on offer:

View post on imgur.com

Ealdorlight Kickstarter launch date: 6th June

Yes, I know I said May :) I’ve decided to go for a 31-day campaign, starting on the 6th June, for a few reasons:

  • It doesn’t clash with any major US holidays, like Memorial Day. The 48-hour reminder email should go out on the day after 4th July.
  • I want to give myself the best chance of success by polishing the combat demo hard. It was great to get such good feedback at Rezzed and I think it’ll be a great hook. I need more time to do that well.
  • 6th June is the first anniversary of Sol Trader’s launch, so it ties in nicely with the ongoing Revelation Games story.

I’m excited and nervous about this Kickstarter campaign: my third one to date. After succeeding last time I’m really trying to take my time and get it right.

Sol Trader 1.3 released!

Now that I’m back, I’m able to support a new release of Sol Trader: 1.3 is now finally released after a length beta period.

Here are the highlights:

  • You can now chat to characters directly on the right if they’re in the same location as you
  • Pirate Chief and criminals are now more likely to try to destroy you
  • Fix a bug where you’re not paid enough for a mission
  • Dignitaries now fly around a little less than before to make it easier to pin them down
  • Inter-faction missions now pay slightly less
  • Business trips now pay slightly less
  • Taxi missions now pay slightly more
  • Talking to your criminal parents will no longer cause them to forget who you are
  • Fix crash where a character develops an opinion of the player mid-conversation
  • Fix crash when showing GUI for a ship the AI is driving
  • Fix crash where a character would attempt to sell a good on a ship they’ve lent to the player
  • Can now initiate conversations when paused - will restart the game but at realtime speed
  • Fixed the Tiger II achievement

Your steam copy should automatically update. I’ll be releasing an updated downloadable version to SendOwl in the next few days.

Read more

Introducing: Ealdorlight

I’m very excited to be able to announce my new game…

Ealdorlight is a single player RPG set in a low fantasy kingdom where you must influence, cajole and fight your way to the throne.

Players travel the land, meet characters, perform quests and attempt to discover their true identity, different every game. Learning about the pasts of other characters gives the player influence over them, and learning who they truly are allows a player’s character to become more and more powerful. Along the way they will discover ancient weapons with hidden pasts, recruit allies, fight those who get in their way, and finally build a strategy to overthrow the King.

Ealdorlight generates centuries of history and society each game, giving players a whole kingdom of characters and relationships to explore and exploit, with turn based story-focused combat and an open quest structure.

Find out more info on the website!

Development plan

The plan is to put the game back on Kickstarter in May. Kickstarter was awesome last time for Sol Trader’s campaign - I think that the Kickstarter community is one of the best on the Internet for encouragement and positivity. I also want to check in with gamers to make sure the game I’m planning is the one people want me to make.

I’m building this game in Unreal, although parts of Sol Trader’s engine will make it into the game. Using Unreal has been a great experience so far. I’ll post tips and other learnings here as I go, as well as some thoughts on why I chose it.

I’ve already had some very helpful feedback on the idea and am looking for more - what thing about Ealdorlight most draws your attention?

Read more