x


inventory system

I need help whit create a inventory can anyone help me?

more ▼

asked Oct 05 '11 at 12:23 PM

MajorFacepalm gravatar image

MajorFacepalm
16 1 1 1

if you help me make a gui progress Health for my game i sure do help you

Oct 05 '11 at 12:24 PM Shkarface

What have you done so far? Show us the code you've written thus far (if any) and explain what you're trying to do, and then ask questions about what you don't understand, or what went wrong. Then we can help with that. We can't help with totally general questions, sorry.

Oct 05 '11 at 12:25 PM CHPedersen
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Alright, I've got 20 minutes to kill...

public class Inventory
{
    List<InventoryItem> items = new List<InventoryItem>();
    InventoryItem selectedItem;
    Player owner;
    int currentlySelected = 0;
    public void Init(Player newPlayer)
    {
         newPlayer = owner;
    }
    public bool AddItem(InventoryItem newItem)
    {
         if(owner.strength >= GetTotalMass() + newItem.mass)
         {
              items.Add(newItem);
              return true;
         } else {
             return false;
         }
    }
    public void DropItem(InventoryItem newItem)
    {
       items.Remove(newItem);
    }
    public float GetTotalMass()
    {
         float mass = 0;
         foreach(InventoryItem it in items)
         {
             mass += it.mass;
         }
         return mass;
    }
    public InventoryItem DrawItemsGrid(int columns)
    {
          Texture2D[] textures = GetTextures();
          currentlySelected = GUILayout.SelectionGrid(currentlySelected, textures, columns);
          return items[currentlySelected];
    }
    public Texture2D[] GetTextures()
    {
         List<Texture2D> textures = new List<Texture2D>();
         foreach(InventoryItem it in items)
         {
             textures.Add(it.icon);
         }
         return textures.ToArray();
    }
}

public class Player : MonoBehaviour
{
    public float strength;
    public Inventory invent;
    public Hat myHat;
    public Sword mySword;
    public Sword myOtherSword;
    void Start()
    {
         invent.Init(this);
         invent.Add(myHat);
         invent.Add(mySword);
         invent.Add(myOtherSword);
    }
    public void TipHat()
    {
        //play a hat-tipping animation!
    }
    public void Attack()
    {
        // swing a sword
    }
    void OnGUI()
    {
         InventoryItem currentItem = invent.DrawItemsGrid(2);
         currentItem.DrawGUIInfo();
         if(GUILayout.Button("Activate " + currentItem.itemName))
         {
               currentItem.Activate(this);
         }
         if(GUILayout.Button("Drop " + currentItem.itemName))
         {
               invent.DropItem(currentItem);
         }
    }
}

public abstract class InventoryItem
{
    public string itemName;
    public string dragInfo;
    public float mass;
    public Texture2D icon;

    public abstract void Activate(Player player);
    public void DrawGUIInfo(){ GUILayout.Label(dragInfo); }
}

[System.Serializable]
public class Hat : InventoryItem
{
    void Activate(Player player)
    {
        player.TipHat();
    }
}

[System.Serializable]
public class Sword : InventoryItem
{
    void Activate(Player player)
    {
        player.Attack();
    }
}

That's a start. I'm not offering any guarantees, since I'm pulling this out of thin air here. Give it a shot!

more ▼

answered Oct 05 '11 at 01:04 PM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

I shouldn't have done that. That post was useless to anyone! I need some sleep.

Oct 05 '11 at 01:56 PM syclamoth

It was bothering me, I turned InventoryItem into an abstract class, that way it is faster to inherit.

Mar 27 '12 at 03:17 PM Berenger

What's wrong with using an interface? Although the implementation for those two examples was the same, there's no reason why you wouldn't want to do something different on another class, and abstract inheritance locks you into a certain pattern.

Mar 27 '12 at 09:49 PM syclamoth
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4155
x317

asked: Oct 05 '11 at 12:23 PM

Seen: 3098 times

Last Updated: Apr 08 at 12:48 AM