Using Strings as IDs
June 26, 2009Sometimes, it's nice to be able to identify things by name, rather than using a number or an enum. The problem is, that if you're not careful, using strings can take a lot of memory, and LOADS of CPU-power, with all the stricmp's. When I was hired for the Tycoon City project, and assigned the task of speeding up the game (which was *really* slow at that point), it turned out they were using several hundred megabytes for storing strings (of which most were used as ID:s, and was mostly duplicates of duplicates), and that string comparisons and string copying was right at the top of the profiling results. I won't go into how we went about solving that particular problem - it was late in the project, and things turned out the way they turned out - but it got significantly faster and smaller, that's for sure.
Ever since then, I'm always a bit wary about using strings - it can easily get out of hand, and it feels wrong to waste both memory and CPU-power on something if you don't have to. So, I thought I'd share my own system for solving this problem, extracted from my (recently cleaned up) Pixie Game Engine. It's a solid, fast solution, ready to just drop into any project, and consists of only two classes. At its core is a class I call StringId, which is easily created by:
StringId myStringIdVariable("myOwnIdNameHere");
Which internally results in the string "myOwnIdNameHere" being located in a global string table (non-case-sensitive), or inserted if it's not already added. The lookup is done through a pretty fast hash-table. In the StringId object is stored a pointer to the shared string, so you can do:
if (myStringIdVariable==anotherStringIdVariable)and know that it will result, internally, in just a comparison of two pointers. Nice and fast. And takes less memory too, as a string is only stored in one place - the global string table. There's also a couple of macros defined (they're nifty at times), strSwitch and strCase, which can be used to compare StringId's in a way similar to switch-case statements:
strSwitch (myFruitTypeStringId)
{
strCase(Apples)
{
// code to do stuff here
}
strCase(Oranges)
{
// code to do other stuff here
}
}
Which is the same as doing this, but looks a bit nicer (though that is, of course, a matter of taste :D )
static StringId applesId("Apples");
if (myFruitTypeStringId==applesId)
{
// code to do stuff here
}
static StringId orangesId("Oranges");
if (myFruitTypeStringId==orangesId)
{
// code to do other stuff here
}
The code is well-written and well-commented, and can be downloaded here. It is C++, and as usual it's public domain - use it anyway you like :D
Don't Look Back
May 18, 2009Terry Cavanagh of Distractionware made this awesome flash game a while back - if you haven't tried it already, you really should - it's that good.
Escape from the Dungeon
May 16, 2009Here's a little game I've been working on for a while, on and off.

It's called "Escape from the Dungeon", and the back story is this: three adventurers (a man, a woman and a dwarf) have been split up while exploring a dungeon, and the player controls all three (can switch at any time he like).

The three characters start at different positions in the dungeon, and you simply need to bring the three together and hit the nearest exit.

Initially, I started this because I just wanted to playaround with generating random dungeons, and after I had done the room generation, I sort of got stuck and didn't know what to do with it, and only recently got moving with it again.

There's going to be monsters too, like this:

There's an early demo of the game here:
Escape from the Dungeon - Demo
The controls are cursor keys to move, TAB to bring up the map. 1,2,3 to change character (or click their portrait)
The characters start some distance away, and sometimes it's a bit tricky to find a way to bring them together. There's no monsters yet, and there's not even an exit for you to find, but it'll all get added in due time.

So, I would very much appreciate any feedback/thoughts on this. Don't hesitate to point out things that don't look right or which you just don't like - anything that can help me improve things :-)
Cat Cat Watermelon
May 14, 2009Came across this fun little free flash game and thought I'd share:
Site Restructure
May 13, 2009Most of the time when I make changes to this site, I don't write a blog entry about it - I just silently make the changes in the background and most of you probably never even notice it.
This time though, I've made quite significant changes - I've moved about 20 blog entries away from this blog to two other sites. Old links will still be redirected properly, but you might not find some entries by browsing the blog - and if that's the case, have a look for it on the new sites instead.
So what are these new sites?
Well, first out is TopHat Arcade. This is a site I have set up to prom ote my favourite indie-games: Simulation, Strategy and RPGs. The idea is to use it to both promote games and their developers, and also to provide useful resources for indie developers and in the long run build a community on the site, if there's an interest. For now, I've moved everything from the blog that was about programming and game development to TopHat Arcade, as well as all the development related material from my Colossus Entertainment site (including the Pixie game engine).
Next up is RetroGameDev. This site has been around for a while, but I haven't really done much with it, until now. The idea is to make it a useful resource and community for everyone who likes retro game development - making games which looks and/or plays like games from the good old days. I've moved everything from this blog which had to do with retro development and the RetroBox to that site, so if you're at all interested in those things, why don't you head over there right away, sign up for the forums and start hanging out? :-)
As you might have guessed, this restructure means that this blog will change a little too. Going forward, I will continue to write about old games, boardgames, the little freeware games I work on and painting miniatures, but there won't be any more stuff about retro game development, 3d engines or general game development - for those things, I will use the other two sites, so make sure to check them out if you're interested in those things.












