Game Development

Blog 803: Getting Stacked

I’ve had the first pass at Exon‘s inventory screen up and running for a while now, so it’s high time I refined it further. I was using a Slug Rifle the other day and realised that while I put this gun on sale, I didn’t actually put any ammunition for it on sale, and that led me to questions of how ammunition should be handled at all. Which in turn led me to item stacking.

Item stacking is a fun one because, on the face of it, it’s very easy: you just assign a number of “charges” to a particular item, count them up as you find copies of the same item, and count them down with each use.

Oh, my sweet summer child.

Getting Stacked

Let’s rewind a bit.

The main inventory works as I want it to. You have equipment slots and backpack slots and items can be swapped between them by clicking and moving and clicking again. It is, I am not ashamed to admit, an almost 1:1 recreation of the Baldur’s Gate/Infinity Engine inventory screen. I am not ashamed because I find this style of inventory very satisfying to use — although it’s not the full Deus Ex-style Inventory Tetris I originally envisaged, there’s still the ability to organise (e.g. put all your consumables at one side and junk at the other) and limited space to ensure you have to make choices about what to carry along and what to abandon.

(Maybe the need to right-click on an item to see its details is a little bit obtuse and unintuitive because there’s absolutely no indication you can right click on any interface anywhere else in the game, but we can sort that out later.)

Gold bars for illustrative purposes only.

Ammunition is currently handled by a totally separate, invisible store. If you pick up some ammo, it instantly disappears and is translated into pure numbers. Those numbers are capped depending on the ammo type but, crucially, you can’t sell ammo you don’t care about anymore because it’s all hidden away.

Ammo handling is further complicated by the ammunition types you will be able to find. In particular, the artillery cannon will be able to fire different types of shells (supplanting the “Shell Mod” system of Project Y4) and with this system there’s simply no way to select which type you’d want to use. From here it feels like ammunition needs to be brought in line with the rest of the inventory; after all, in Baldur’s Gate you made this kind of selection by dropping blocks of ammo into a character’s ammo slots. (My original angle was to do something like Deus Ex where a key toggles the chosen ammunition type, but I dismantled the whole reloading mechanism a long time ago because it simply doesn’t fit with anything else I’m trying to do — guns are not the primary focus of the game and will rarely (if ever) be your primary weapon.)

Okay, fine, so you make ammo stay as items, but if you take a single shot with your Shard Rifle I need a sensible and visible way of tracking that — nobody wants to end up with six ammo blocks that only have an invisible three shots left in each. Likewise, I don’t want to punish a prudent player for trying to carry more than one health pack by having them sacrifice half their backpack for the pleasure. The two needs thus converge in the form of item stacking — compressing more than one instance of an item into a single stack that has multiple charges.

Consumable items can now be used from their Details screens. I am, however, starting to lean towards some kind of quick slot(s) so you can use them during combat — Nox had this specifically for Health, Mana and Antidote potions, but obviously I’d have it for anything consumable because… why not?

This actually dovetails quite nicely with my save/load system. Each item type has a static Prefab ID which can be trivially compared on picking something up: if your backpack already contains another item with the same Prefab ID, then it’s ripe for stacking. Stacks still need to be limited, of course, so it’s not quite as easy as that.

Health packs, for example, I am limiting to a maximum of five. This actually fits quite intuitively with the slot-based inventory — it’s simply saying that five health packs can fit into the same physical space as one sword or one shield; it’s adding (by implication only) a slightly more granular grid layout. So if you have four health packs in a single stack, and pick up another four, then you end up with the first stack filled up to five and a second stack of three. It’s actually totally iterative, so if you have two stacks of four then you’d end up with two of five and a final one of two — and if your inventory is otherwise full, you’ll leave those final two on the ground.

Yes, it is of critical importance that somebody with a full inventory but some stack space can pick up those items. (This is the problem I attempted to side-step inin When the Freedom Slips Away and This Wreckage by using a combination of auto-use runes and normal items, but that only worked for picking up a stack for the first time — once the disappearing rune’s charge had been transferred to the hard item in your inventory, it was stuck that way.)

I’ve replaced being able to drop items via the central window onto the real world with a proper ground slot area, because clicking the “paper doll” more intuitively suggests “equip this” than “drop it”. The side effect is that you can give your mech silly shield hats.

Naturally, if you can make stacks, you have to be able to split them. Maybe you need some cash and think you can go without most of those health packs, you will want to split a few off for selling. Maybe you’re confident you’ll find more artillery shells on your next mission, but want to keep a couple just in case.

This is a fun one because when an item is consumed into a stack, it is destroyed — it ceases to be a physical entity in the game world and becomes just another notch on the stack’s bedpost. So when a stack is split, a new stack needs to be brought (back) into existence.

Unity actually makes this fairly easy. Although one traditionally calls Instantiate on a prefab to create a version of it in the live game world, you can do it on live game objects too to clone them. So bang, clone the item with a portion of the stack, set its stack size to the split portion, deduct that same portion from the original, change its save/load object ID (can’t have the same ID twice, whoops) and away we go.

I haven’t actually got on to ammunition handling yet, but stacking at least is now working nicely. That’ll be next week’s project!

When the odds are stacked against you, it’s time to split.

And you tell me...

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.