• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Freeky

Best way to manage items?

7 posts in this topic

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! Edited by Freeky
0

Share this post


Link to post
Share on other sites
Why not try making the class Static?

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

[/code]

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.

[code]
public static class Globals {
public static ItemManager ItemsManager;
}
[/code]

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

edit: personally, I'd go with the 2nd approach. Edited by Andy474
1

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='Freeky' timestamp='1349774899' post='4988270']
Making the class static really solves the problem of having to pass an instance of ItemManager everywhere.
[/quote]
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.

[quote name='Freeky' timestamp='1349774899' post='4988270']
But since static classes can't have a constructor: Where should I put the initializing of all the items then?
[/quote]
Static classes can still have [url="http://msdn.microsoft.com/en-us/library/k9x6w0hc(v=VS.80).aspx"]static constructors[/url]. 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.

[quote]
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 [url="http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx"]indexer [/url]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. Edited by Josh Petrie
0

Share this post


Link to post
Share on other sites
[quote name='adt7' timestamp='1349785894' post='4988315']
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?
[/quote]

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.

[quote name='Bartley' timestamp='1349796409' post='4988382']
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.
[/quote]

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.

[quote name='Josh Petrie' timestamp='1349797950' post='4988387']
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.
[/quote]

[quote name='Josh Petrie' timestamp='1349797950' post='4988387']
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.
[/quote]
Thanks for the tip. I renamed it to ItemDatabase.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0