Best way to manage items?

Started by
6 comments, last by Freeky 11 years, 6 months ago
Hello there.

I'm adding an item system to my game. It's functional, but I'm not sure if it really is the best way to do it.
My game is written in C#, and this is an example of the system I'm currently using:

In my 'main' class (Game.cs) I make an instance 'ItemManager' class. Kinda like this:
[source lang="csharp"]ItemManager items = new ItemManager();[/source]
In the ItemManager class, I define all the items. (Instances of the 'Item' class)
[source lang="csharp"]class ItemManager
{
public List<Item> items = new List<Item>();

public Item potion = new Item(0, "Potion", potionIcon);
public Item bucket = new Item(1, "Empty Bucket", bucketIcon);
public Item rope = new Item(2, "Rope", ropeIcon);

public ItemManager()
{
Items.Add(potion);
Items.Add(bucket);
Items.Add(rope);
}
}[/source]
And finaly, this is kinda what my Item class looks like
[source lang="csharp"]public class Item
{
public int Id;
public string Name;
public Bitmap Icon;

public Item(int id, string name, Bitmap icon)
{
Id = id;
Name = name;
Icon = icon;
}
}[/source]
The system works fine, but there are two main issues:
1. I need to pass the Game.items to every class that has something to do with items, witch are a lot of classes.
2. I need to add the items to the ItemManager.items list in the exact same order as the id's the items have, and I can't skip an item id.

Other than that, I hate to refer to items.items[itemID], but that is mainly just because it looks terrible.

I wonder how you guys would handle this. I have searched around on Google but strangely enough I did not find a good example.
Do you have any suggestions?

Thanks in advance!
Advertisement
Why not try making the class Static?

e.g.

public static class ItemManager {
public static List MyItemsList = new List();
public static void GetitemByID(int ID) { return MyItemsList[Id]; }
}



then you can refer to it anywhere e.g. ItemManager.GetItemByID(3);

or, you could keep what you have and create a static class called Globals, and store it there , e.g.


public static class Globals {
public static ItemManager ItemsManager;
}


and refer to it like Globals.ItemsManager[ID];

edit: personally, I'd go with the 2nd approach.
Thanks!
Making the class static really solves the problem of having to pass an instance of ItemManager everywhere.

But since static classes can't have a constructor: Where should I put the initializing of all the items then?

I can initialize the items everytime I request an item, like this:
[source lang="csharp"]public static class ItemManager
{

public static Item potion = new Item(0, "Potion", potionIcon);
public static Item bucket = new Item(1, "Empty Bucket", bucketIcon);
public static Item rope = new Item(2, "Rope", ropeIcon);

public static Item getItem(int id)
{
List<Item> items = new List<Item>();
Items.Add(potion);
Items.Add(bucket);
Items.Add(rope);
return items[id];
}
}[/source]
But I don't think that's a nice way to solve it. There must be a better way.

Any suggestions?
when you make a game engine, you always have to intiialize stuff, and initialization doesnt' have to be in a constructor
especially for static data
however.. an item list like that needs to be cleaned up when exiting game
so you aren't limited to using constructors and destructors, but it would be "nicer" code if you could do that for that item list
static void Globals.initializeStuff()
static void Global.cleanupStuff()

is what i would do, since it's not something you would forget, or get into trouble for doing
You're better off with a Dictionary<int, Item> (or perhaps <string, Item> rather than a List for your items.

What is the purpose of your IDs? Does an item need to know it's ID? Why can't you just retrive items by name?

A static class in C# cannot have a public constructor, it can however have a private constructor (although you have no guarantee exactly when it will be called, but this most likely doesn't matter for you). Although rather than making the whole class static, why not just have a static GetItemByID/Name() method?
Hello.

I did something similar (in java) using a static arraylist of items in the main class. If your characters will have inventories too, you could build a static arraylist of items into your character class.

On creating items, I created a Utility class which has a static spawnItem method, which sends the variables to the relevant constructor in the relevant item class. Then I populated txt files (one for melee, one for ranged, etc) with the items stats. To create a particular item, you just have to send the item's text file, the row which contains the variables, and the amount of items you want, to spawnItem, which constructs the item, then place it in the relevant item arraylist. This worked, but there are doubtless easier ways to handle this.

Making the class static really solves the problem of having to pass an instance of ItemManager everywhere.

No, it actually does not. It just hides the fundamental problem.

The reason that "having to pass it everywhere" is a problem is not because you're cluttering up function parameter lists with an extra parameter. That's a bit ugly, sure, but it's not the serious issue. The serious issue is that "everywhere" depends on this interface, which is a symptom of a poor or lazy design. Passing interfaces around via function parameters is actually advantageous in that respect because it makes the dependencies on your interface (the item manager in this case) very explicit, so you can see when you have way too many of them and can focus instead on eliminating those dependencies entirely instead of hiding them by making the interface globally-accessible.

To solve this problem in a better fashion, you need to analyze every dependent interface -- everywhere you "need" to pass the ItemManager -- and determine why that interface actually needs it and if/how you can eliminate it. Chances are very good you can -- why don't you try listing some of the places you currently require the ItemManager and what they do with it.


But since static classes can't have a constructor: Where should I put the initializing of all the items then?

Static classes can still have static constructors. For now you can do your item initialization there, although in the longer term you should migrate to a data-driven approach where you item data is stored in a file (perhaps XML or JSON, they are simple places to start) and you read it in to the item manager at runtime. Then you can alter your item data without having to rebuild your game.

Also, thus far this interface does not do anything I'd consider "management" at all. It just holds on to a bunch of data. The term "manager" is a vague/azy and consequently makes for a poor choice in interface names. I'd instead call this interface and ItemCollection or ItemDatabase, depending on the eventual operations you support with it.


Other than that, I hate to refer to items.items[itemID], but that is mainly just because it looks terrible.
[/quote]
This is weird because it implies the item ID corresponds to the position in the items list, which is brittle and non-scalable, because the order in which you add the items must also correspond with the IDs you've assigned items, making for two places where the code must change if you re-order or tweak the IDs of items (especially since you define the ID with the item instead of letting the collection itself define the ID). Better instead to use a Dictionary, probably, as another user has already suggested. The list as a mechanism for storing this things internally doesn't buy you much at all.

You can clean up the interface by either adding an explicit FindItemById() method to the item collection, or using an indexer in the item collection, which would let you access items via itemCollection[itemId]. Then you don't need to expose the item storage directly, which is good because if you do expose the item storage directly, client code can manipulate it (adding and removing) and thus destabilize your interface.

What is the purpose of your IDs? Does an item need to know it's ID? Why can't you just retrive items by name?


It's used for saving and loading: I save the item id's of the items that are in the inventory to a file. This means that I need to be able to find items by their id's, and to get the id of each item.
The name will be shown in-game. I don't want to use it instead of an idea because I would like to leave the possibility for duplicate item names open.


On creating items, I created a Utility class which has a static spawnItem method, which sends the variables to the relevant constructor in the relevant item class. Then I populated txt files (one for melee, one for ranged, etc) with the items stats. To create a particular item, you just have to send the item's text file, the row which contains the variables, and the amount of items you want, to spawnItem, which constructs the item, then place it in the relevant item arraylist. This worked, but there are doubtless easier ways to handle this.


Good idea. I want to implement something like this so I can easily implement item states, Like enchanted or sharpened.
But I don't feel like using text files.


To solve this problem in a better fashion, you need to analyze every dependent interface -- everywhere you "need" to pass the ItemManager -- and determine why that interface actually needs it and if/how you can eliminate it. Chances are very good you can -- why don't you try listing some of the places you currently require the ItemManager and what they do with it.



Also, thus far this interface does not do anything I'd consider "management" at all. It just holds on to a bunch of data. The term "manager" is a vague/azy and consequently makes for a poor choice in interface names. I'd instead call this interface and ItemCollection or ItemDatabase, depending on the eventual operations you support with it.

Thanks for the tip. I renamed it to ItemDatabase.

This topic is closed to new replies.

Advertisement