Josh-CO Dev

Solving the worlds problems one line of code at a time.

XNA – Basic Inventory System, Part 1.

4 Comments


Here is another one dug up from the archives. Unfortunately, I haven’t been able to dig up part 2 yet.

So, you’ve started working on a game and have decided that you need an inventory system. What I have found is that this task really doesn’t have to be as difficult as it sounds. The first thing we need to do is create a class that will hold our inventory items.

using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace game.Inventory
{
    class InventoryItem
    {

I’ve created an enum to hold what type of item it is. In my case, I’ve got tools, crafting items, furnitue, etc.

       
//what type of item is it
        public enum InventoryType
        {
            craft,
            consumable,
            furniture,
            tool
        };
        public InventoryType thisType { get; set; }

        //the texture for any tool tips
        public Texture2D Texture { get; set; }

        //the number of the item that you have
        public int NumAvailable { get; set; }
        public string ItemName { get; set; }

        private int MaxQuantity = 99;

Everything else is basic properties and variables and should be pretty self explanatory. Texture is the texture that we will use to draw the icon for the onscreen inventory, numavailable is how many you have, itemname is the name of the item, and maxquantity is the maximum number allowed.

We have an empty constructor, and another one that populates the pertinent fields

        public InventoryItem()
        {
           //empty constructor
        }

        public InventoryItem(string name, int numAvailable, Texture2D texture, InventoryType type)
        {
            this.NumAvailable = numAvailable;
            this.Texture = texture;
            this.thisType = type;
            this.ItemName = name;
        }

Two add methods. One of this simply increments the quantity available and the other increments it by a set amount

        public void AddItems()
        {
            this.NumAvailable++;
            CheckMaxQuantity();
        }

        public void AddItems(int numToAdd)
        {
            this.NumAvailable += numToAdd;
            CheckMaxQuantity();
        }

Then we have our basic draw method and a simple method to make sure we never exceed our maximum quantity.

        public void Draw(SpriteBatch sb, Vector2 position)
        {
            sb.Draw(this.Texture, position, Color.White);
        }

        private void CheckMaxQuantity()
        {
            if (this.NumAvailable > MaxQuantity)
                NumAvailable = MaxQuantity;
        }

That’s it. In the next post, I’ll show how I pulled together the actual inventory class and how I am handling the inventory control, drawing of the ui, etc.

Advertisements

4 thoughts on “XNA – Basic Inventory System, Part 1.

  1. Did you get part 2 up? This first part was really helpful, but I’m kind of stuck on what to do next to get everything to work!

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