"Unfortunately, by that stage the game was running so slowly that the framerate was probably less than 1..."

Major, major milestone this week!  I realise that I haven't posted for a while, partly because real life keeps getting in the way and partly because I've been bum down working on adding a first attempt at AI to Pax et Belli.

The milestone?  I played against and defeated an AI civilisation.  It's the first time I've actually managed to play for long enough, with enough systems in place, to reach the point of defeating a civilisation.  It was a really great feeling!  Unfortunately, by that stage the game was running so slowly that the framerate was probably less than 1, so before I release my evil AI minions upon the world I'm doing some pretty deep optimisation work.

Turns out that Unity (the game engine I'm using) and C# (the programming language) take all sorts of shortcuts in order to make things easier.  Of course, this means that it's way easier to dive in and build games which is a Very Good Thing.

A wee digression here before technical meanderings commence.  I love games - not just computer games - and I totally love the recent explosion in Indie games.  All the different voices and viewpoints making amazing things that communicate so well through gaming is just amazing!  So I fully support Unity (and GameMaker and all the others) doing such awesome work to enable game creators of all abilities and colours and flavours, cause it's just awesome!

Anyhoo, optimisation.  TL;DR: Blah blah memory blah leaky abstractions blah blah, non-technical bit at the bottom :)

The problem with making things easier is, loosely, the Leaky Abstraction problem (named, as far as I know, by the excellent Joel Spolsky.  The general idea is that hiding away the hard parts is great right up until you realise you really need to know about the hard parts because they're busy biting your knees off.

In my case, I use lots of lists of things (lists of cities, lists of resource transports, lists of AI decisions, lists, lists, lists) and each time you iterate through one of those lists Unity was grabbing a bit of memory to allow it to be nice and helpful.  But when you're iterating through lists inside lists inside lists, those little bits of memory add up real fast, and then C# in it's wisdom comes along and goes "Everyone stop what you're doing while I sweep up this mess" and your game looks like a bad timelapse and you get hatemail.  I'd quite like to avoid that, so I've been ripping out all sorts of stuff and replacing it with the slightly fiddlier versions.  For the coders, it's mostly been replacing foreach loops with for loops, and finding a bunch of places where I was creating new List<> as return objects and instead passing in reused static List<> instead (which will be horrible if this ever gets multithreading but Unity is mostly resolutely single threaded so hopefully I'll dodge that bullet)

Well that's been rather longer than usual.  If you've got this far, I just want to say that I REALLY appreciate you all coming on this journey with me.  Every time I get a like or follow or comment or share it makes my day and makes this adventure a little more worth it :)