Journal

Unity Game Project: Yo Dawg Arrays


I’ve begun work on a game project that I hope to complete this month. It’s actually very far along after only a couple of hours so I’m hopeful. I’ve also found an excellent open source hexagon art collection for my other game project (which I stopped because of NaNoWriMo, but I haven’t forgotten it). I wanted to discuss today something I did that I’m fairly pleased with. Basically I created a hash table of references to individual lists.

Basically the game I’m building requires knowledge of how many of an individual critter there is. I didn’t want to have a billion different tags, and equally I don’t want to be searching my scene for enums or something else equally messy. So instead I’ve created reference “pens” that will hold all the members of their particular size. There are currently 14 unique sizes in the game, this is relatively arbitrary and doesn’t add any complexity on my end of things (thankfully).

So what I did was create a unique list for each of the sizes and then I created a dictionary (hash table) of each list, with the key being the enum for the critters size, and the value being the reference to the relevant list. Then, whenever the game tells my critters to grow it fires off two extremely simple requests. The first is “find the key that matches my old size, remove me from the list that is that key’s value.” The second is “find the key that matches my new size, add me to the list that contains that key’s value.”

Amusingly I was doing greater than, instead of greater than or equal to for a bit. So my first list never cleared anyone. Liz pointed it out and I fixed it. Always helpful to have a second pair of eyes. This is create because I love one liners, or in this case I suppose two liners. It’s fast, and as far as I can tell, 100% accurate. I’m really pleased with how it turned out. In fact now that I mention this I just realized I don’t even need the greater than or equal than because of how I changed it. Now I can just run with greater than. The psuedo code is as follows:

//Here we convert the int into the critters size.
size = newSize;

var tempDict = DictReference;

//This stops us from calling for an array that doesn’t exist.
if (tempDict.ContainsKey(size – 1))
{
List<CritterController> oldPen = tempDict[size – 1];
oldPen.Remove(this);
}

//Now that we’ve been removed from our old pen, add to the new one!
List<CritterController> newPen = tempDict[size];
newPen.Add(this);

I don’t think I screwed anything up here by psuedo coding it. However, this works great! For some reason I totally forgot about “ContainsKey”. Now that I’ve got that in there the whole thing reads nice and smooth. If you are at least “rank 2” you’ll remove yourself from your previous rank (there is no rank 0). The next bit of code I’ll be writing is the “breeding” code where my critters will look in their pen to see if anyone is up for some shagging. Each pair will join up and create a new larger critter (so it’s a bit like Anglerfish Sex in that respect). Doing it this way I can just check each array in turn, rather than the entire field each time. It should provide me with enough granular control that I have no race conditions.

And as always, if you are removing things from lists or arrays, do so from the bottom up. This way your items don’t move their index numbers (removing entry 12 in a 12 entry long array won’t change anyone else’s position, removing the first entry will shift all 11 remaining down 1). The reason for turning the two lists into references “oldPen” and “newPen” is that in the future I suspect I’ll be doing special things depending on their size. For instance, I could flag the pens as skippable if their count is 0. Stuff like that.

So that’s my little rant about my “YoDawg” array. I really need to get an addon for WordPress that doesn’t format code snippets. I’ll look for one soon, I don’t recall what happened to my old one.