Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

"Item" system in a game

$
0
0
Working on a medium-size console game (yup, console as in DOS, as opposed to Xbox, Playstation :D), I'm struggling to think of the best way to make an item system. I made one implementation, which worked, but when I came to doing some more complex stuff with it, the method broke.

What I was using was a class "Item", and then I made static variables for each Item. For example:
// Basic implementation of Item class
public class Item
{
    public string Name { public get; protected set; }

    public Item(string Name)
    {
        this.Name = Name;
    }
}

// In "ItemList.cs"
public static class ItemList
{
    public static Item ScrapMetal
    {
        get
        {
            return new Item("Scrap Metal");
        }
    }
}



You are probably asking why I returned a new Item, instead of just using one. When I came to writing up Weapons, which I treat as Items (inheritance), I found that due to references, everything in the game had exactly the same stats; everyone died when I only hit one guy, for example.

So, my question is: What is a good way to make a weapon/item/defence system? Of course, I'm not asking for the best way, as there will likely be many methods which are all useful in different situations. What I need is a system whereby I can basically say "use weapon X", and a new, non-referenced weapon is used. However, I also need to have, in effect, a list of all possible items; so as well as having the above way of accessing them, I can loop through them all.

I appreciate this post may be a bit confusing; I can't quite phrase this request as I can think it, so please reply with any and all questions that will probably arise after reading.

Hopefully,
Hnefatl

Viewing all articles
Browse latest Browse all 51036

Trending Articles