Creating an inventory system in Unity, using C#

From the ImpactJS game, X-Type

From the ImpactJS game, X-Type

I’ve been working on a shmup for some time now, and just started blogging about it. One issue I came across was writing an inventory system. There are many out there, but they just didn’t work for me. I wanted something simple, and only needed it to work with my weapons and powerups.

Since my last post, I added a 2D Shooter Bullet and Weapon System from Sean Duffy at HobbyistCoder.com. I wrote my own a while ago, but his was FAR, far better than what I had. In seconds, I had a weapon system for a side scrolling, top down, and supported multiple control schemes. Here’s the description from the site:

This 2D Shooter Bullet and Weapon System asset for Unity3D provides you with a customisable bullet and weapon system, along with a bullet pooling manager which is simple to drop into your Unity game or project and start using right away. In addition, you also get a customisable player movement script which allows for two modes of player movement controls.

The system allows you to easily create preset bullet patterns or weapon types for your 2D games, whether they be bullet hell shmups, or top-down zombie defense games. The bullet and weapon system is fully customisable, and includes ricochet (bounce) and bullet impact effects (version 1.2 and above). The WeaponSystem script contains sliders for every property on the weapon allowing you to easily control properties like bullet count, fire rate, bullet randomness, spread, offsets, and even alternate properties like spread size on an alternating timer system. This amounts to a huge number of possible bullet patterns and weapon types for your games and projects!

The included demo scene gives you 13 preset weapon types to try, 2 different bullet types, 4 different bullet colour options as well as the sliders needed to create your own customised bullet/weapon types.

Try the Web demo

I don’t really like to use many plugins, becuase they are often more work to integrate than it would be to write your own, but with this, I was going instantly.

While it does have a built in inventory already, I still needed a few more things for my own project. More specifically, when I  use this weapon system, I’m given all of the weapons at once. I wanted to have an empty inventory, and have my character pick them up as it went along, either though the user of power-ups or leveling up.

Getting started

To get around this, I needed to create an empty inventory array, and use an index variable to keep track of which weapon is currently equipped:

 

 // Holds the weapons in our inventory
 private Boolean [] weaponInventory;
// Which weapon is the player currently using?
 private int currentWeaponIndex = 0;

You also need to understand how the weapon system works, to comprehend the inventory.

 public enum BulletPresetType
 {
     Simple,
     Shotgun,
     CrazySpreadPingPong,
     GatlingGun,
 }

The weapons are stored as an enumeration, which under the covers is an efficient way to define a set of named integers (numbers) that may be assigned to a variable (weapon name).

I also have an event handler to check if I’m switching the currentWeaponIndex. Each time the index is changed, the BulletPresetType is assigned. Additionally, I’m setting the length of my inventory to the total number of BulletPresetTypes that I have.

Because I’m using a boolean array to keep track of which weapons are in my inventory, I can set any number in the array to “true” and that means it is in my inventory, and I can use it.

 

Start()
{
     // Subscribe to the BulletPresetChanged Event.
     BulletPresetChanged += BulletPresetChangedHandler;

      // Cap the inventory size to the total number of weapon types, then add Simple to inventory right away
      weaponInventory = new bool[Enum.GetValues(typeof(BulletPresetType)).Length];
      weaponInventory[(int)BulletPresetType.Simple] = true; // Only set first weapon to true (put it in the inventory)
}

I’m doing a bit of typecasting above (converting an enum to an integer) to state that the Simple is in my inventory. Looking at our BulletPresetType enum again, as an integer, it would look like this:

 

 public enum BulletPresetType
 {
     Simple = 0,
     Shotgun = 1,
     CrazySpreadPingPong = 2,
     GatlingGun =3
 }

So when I write

 weaponInventory[(int)BulletPresetType.Simple] = true;

what I really mean is

 weaponInventory[0] = true; // The first item in the BulletPresetType, is currently in my inventory

The event handler

We need a way to define and store the properties for each weapon, and we use a delegate and private / public property to cycle between them, like so:

We still need an event handler. I’m not going to write one here, because that would give away all of Sean’ Duffy’s secret sauce, so buy the plugin, and you’ll see what I’m talking about, or, you can write your own.

 Now I can just write BulletPreset = (BulletPresetType)0; and it will switch to BulletPresetType.Simple. 

Adding more functionality

So we’re able to start an inventory now, but we need to actually make use of it. What happens when we pick up a wepaon? How do we switch to the next weapon? What if we want to change to a specific weapon in our inventory? (Say, map each weapon to a key). The funcitons below do exactly that, and make use of the code I’ve illustrated above. I’ve commented all of it pretty well, so I won’t get into much detail.

I can now call this from my Powerup.cs class, and my player will receive a specific weapon when it collides with a power up. I do that like so:

But

Switching to the next weapon

The hardest part though, was switching to the NEXT weapon in my inventory. It’s easy to define which weapon I want, as I can just pass in an integer, but now I needed to check if any weapon is even in my inventory first, and decide what to do if it is or is not found. This had my stuck for weeks. I’m sure that there are cleaner ways of writing this, but here it is:

Can it work in other languages?

There’s no reason this couldn’t easily work in any C#, JS, or C++ framework. I’m not using any kind of Unity specific code here.

Enumrations don’t exist in JavaScript, so you’d need to just create an object, which looks identical to an enumeration. This site explains it well.

-----------------------


subscribe-to-youtube

Leave a Reply

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