Unity3D/C#: Custom Lists Part 2

This is to follow on from this tutorial.

What we had last time was a list of predefined list items. And it also came up with a code snippet on how to ‘use’ an item from that list. This part 2 will give your items multiple properties and it’ll stack your item lists.

image

First off, lets create a public struct. I’ve called it ‘List’ because I make mine flexible enough to use for other things (like weapons & abilities). But for this tutorial, I’m only going to be assigning the basic properties.

using UnityEngine;
using System.Collections;

public struct List
{
    public string effect;
    public float modifier;
   public string description;
    public int cost; 
}

A struct is very useful for storing a number of variables into a single type. So this means we can group together all of our items properties into one. I’ve gone for:

Effect – What’s the purpose of this item (you might fill this with “explode” or “heal” or “stun”, basically a verb that you might use in another bit of code to decide the item’s behaviour.

Modifier- A float for how you’re going to modify this effect. If a grenade explodes, it’ll explode for a certain amount of damage. I could say it’ll do 30 damage and I’ll make its Modifier ‘30’. If I had some battle maths, I might say, I want to multiply somebody’s strength stat by 1.5 or use 15% of a person’s Vitality stat.

Description – What description you want to display to the player. “Use to cause explosive damage to a target.” Or anything like that.

Cost – If your game has merchants, it might be the base cost of this item. You might buy a some medicine for “12” units of currency (maybe £ or $ or some fictional currency)

The Name & Item Count are covered by the last tutorial and will remain in the same format.

Where you’ve got your Name & Count arrays, you’ll need to make a new array but for our struct.

public List[] Items;

This will not appear in the Unity3D editor. The only things you need in the editor are the item name and its count, which is covered in the last tutorial.

Next, create a new public static class, don’t worry about monobehaviours. We’ll be having 2 static methods for dealing with our inventory. The first is ‘UpdateInventory(Stats)’

‘Stats’ refers to the ‘Stats’ class, this is the class where you’ve stored your inventory.

What ‘UpdateInventory’ does is check through the names of items and applies attribute to our list struct.

public static void UpdateInventory(PlayerStats Stats)
    {
        for (int i = 0; i < Stats.Item.Length; i++)
        {
            if (Stats.Item[i] == “Medicine”)   
            {
                Stats.Items[i].description = “Use to Restore Some Health”;
                Stats.Items[i].cost = 5;
                Stats.Items[i].effect = “heal”;
                Stats.Items[i].modifier = 30;
            }
           
           if (Stats.Item[i] == “Focus”)
            {
                Stats.Items[i].description = “Use to Boost Concentration”;
                Stats.Items[i].cost = 7;
                Stats.Items[i].effect = “mana”;
                Stats.Items[i].modifier = 15;
            }
        }
    }

Stats.Item and Stats.ItemCount (which isn’t in the above code, but you’ll see it later) are referring to the variables discussed in part 1. In this snippet, the class is ‘PlayerStats’ and is attached to my player object. ‘i’ only refers to the inventory slot. All this is useful for when you want to use an item you’ve got more information or if you want to display your inventory, you’ve got extra pieces of information to display.

The second method is ‘StackInventory(Stats)’. What does this do? Lets say we had our list:

image

And lets say I had used up all of my Focuses. I’d just get ‘Focus x0’ displayed, which is kind of redundant and it wastes space. What ‘StackInventory’ does is move everything below the ‘empty’ slot up to fill in the gap

image

public static void StackInventory(PlayerStats Stats)
    {
        for (int x = 0; x < Stats.Item.Length; x++)
        {
           
           if (Stats.ItemCount[x] == 0)
            {
                if ( x < Stats.Item.Length-1 )
                {
                    Stats.ItemCount[x] = Stats.ItemCount[x+1];
                    Stats.Item[x] = Stats.Item[x+1];
                    Stats.ItemCount[x+1] = 0;
                    Stats.Item[x+1] = “”;
                }
                if ( x == Stats.Item.Length )
                {
                    Stats.ItemCount[x] = 0;
                    Stats.Item[x] = “”;
                   
                }
            }
           
           
           if (Stats.ItemCount[x] > 0)
            {
                Stats.Count = x;   
            }
           
        }
    }

To break it down. It’s checking every item slot. For all slots EXCEPT the last one, it checks if the number of items in that slot has reached ‘0’, so then it obtains the name and number of items from the next slot (the x+1). And for the last slot, it just makes sure it’s empty and the name is cleared. The next if then statement is for how the list is displayed. I will explain this one.

The code I use for displaying my list is this (for BorderTextBox see my code snippets):

 

GUI.skin = Skin;
            Interface.BorderTextBox(“Items”, Screen.width – SideX, SideY, Width, Height, Color.white);
            var h = 1;
            GUI.skin = SmallSkin;
           
               
                for (int i = 0; i < Stats.Count + 1; i++)
                {
                    Interface.BorderTextBox(Stats.Item[i] + ” x” + Stats.ItemCount[i], Screen.width – SideX, SideY + 10 + Gap*h, Width, Height, Color.white);

                    if (selection == i)
                        Interface.BorderTextBox(Stats.Item[i] + ” x” + Stats.ItemCount[i], Screen.width – SideX, SideY + 10 + Gap*h, Width, Height, Color.magenta);
               
                    h++;
                }
            }

 

(Paste into C# for better formatting)

Basically, what it’s doing is using a public int I added to the Stats class (where the inventory is stored) called ‘Count’, which is defaulted as ‘0’. So I show each item one after the other for any slots between slot ‘0’ and the number given for ‘Count’. So in the StackInventory method it will read through every slot and take down the number of each slot with an ItemCount above 0. The last number it will take down will be the number of the last slot. This means, if your inventory limit is 5 and you’ve only got 3 items it will not be displayed as:

“Medicine x6, Focus x4, Focus+ x1, x0, x0”. I imagine what’d happen if you limited yourself to 50 items and only were carrying one. Also, because of the ‘if then’ statement for ‘If selection matches the slot number, then highlight it”, your player would be scrolling through empty slots, which is kind of pointless.

StackInventory() and UpdateInventory() should be called at the top of your OnGUI method (so it’s called before you handle any GUI information)

If you want to use my the same method of ‘item selection’ as me. You will need to add a ‘selection’ as a private int  and the following code into your Update method:

if (Input.GetButtonUp(“Up”))
            selection–;
       
       
        if (Input.GetButtonUp(“Down”))
            selection++;
        selection = Mathf.Clamp(selection,0, Stats.Count);
     

You’ll need to define ‘up’ and ‘down’ in your Input settings in the Unity3D editor, which can be found here:

image

So in the Unity Editor it looks like this (after I removed the Focus):

image

Now lets say I set ‘Stimulant’ to ‘0’

image

Count has gone down from ‘’3’ to ‘2’, scrambled note has moved up a slot and so has its count.

In game, it looks like this:

image

Any that’s all there is to it. For the next tutorial I should add how to ‘add’ and ‘remove’ items from the list via code.

Advertisements

Leave a Reply

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s