RPG inventory management question

Started by
4 comments, last by Rubiks14 12 years, 10 months ago
So I just recently (today) got back in to programming. I started making a text rpg and was working on an item management class and got to thinking about what woyuld be thea most efficient way of loading items. Now I know with it being ascii at the moment there won't be a huge hit to memory regardless. Still, I would like an opinion. Is it more efficient to load every item into memory on startup and point references to them throughout the code when needed and delete the listmoment on program shutdown or is it better to load an individual object for every item in use.

I personally would think the former because it would require less time to load the item information. I you had a huge inventory and multiple of each item it could end up being more memory than loading all items once (or at least the stuff that wont take large amounts of memory).

Maybe its a combination of both. Load the small stuff for everything (stats and such) and load big things (models or images (once I get to that point again)) only when needed.

Also, sorry if parts of this are hard to read. Did this on my phone and the gingerbread update for droids has almost made them as bad as iphones when it comes to auto correct.
Advertisement
Well, I really think it depends on a number of factors...

First, the technology - what language would you be using here? Talk of references leads me to think C++, but I suppose you could be using another imperative language. (I'm going to assume an OOP approach) What kind of data structures would you intend on using? Linked lists or something of the sort? Depending on how your inventory system works, a different data structure might be more effective for maintaining the items. How are you loading these items? Using a script or from configuration files, or straight out of the code?

Second, the inventory itself. How similar are different items from each other? Are there similarities between groups? Etc.

Ultimately, you need to look into the finer details of the system to help tailor the system to suit the items. If you're dealing with very few types of dissimilar items, loading them all up at once is good. However, you're likely dealing with multiple instances of many kinds of items, in which case it just seems like a waste of memory to load up everything at once - no need to store content that is not in use. My suggestion would therefore be to maintain the right kind of data structure you need to store item instances. Then, have an item factory of some kind which is able to create items and slot them into your structure as needed - the items themselves could use polymorphism as to interchangeable and be structured in a kind of hierarchy. In all of this, I think it would help to look into design patterns - singleton, factory and maybe also flyweight spring to mind.

Hope that helps somewhat - ultimately, what I'm saying is this: don't load up all the items on start up. But do load up and maintain all the necessary data structures and controllers.
If there are several instances of the same type of object I'd definitely split up the data between what's static and what's dynamic.
Say you have an item type called Iron Sword, and all Iron Swords have a damage stat of 14 and a value of 100 gold. As you use the sword, its condition stat lowers until it breaks.
Then you could have a class called ItemType holding the base information for all items (damage and value) and store one instance of each item type in a list somewhere (an item factory comes to mind, as mentioned in the post above), and a second class called ItemInstance holding the dynamic data (condition), as well as some sort of reference to its ItemType.

But really, remember that premature optimization is more often than not a bad thing. It's good to know of solutions IF your items start to eat memory, but until then, I'd advice against implementing it unless you just want to experiment.
It really depends on the scope of your game. For an MMO you might want to store them on a DB and load the items in use from that. For just a couple people playing at a time, I would store their instances in memory as they are needed. Storing all the instances right away would just make it harder on you imo trying to keep track of all the references you need to keep track of.

Look into the abstract factory pattern. That would probably serve you well here.
This may or may not work for you, but when i last wrote such a game, my invetory stored a description of each item and a set of modifier attributes for the item, rather than a complex class. These attributes were stored as a string, this was because each of the locations, items, enemies etc was written in a homebrew scripting language that allowed me to represent these things.

For example, if someone came across a stamina potion which gave them a ten stamina point boost, it would be represented by the string:

"Stamina Potion [ST+10]"

And if someone found some poison which knocked skill and stamina points off it would be represented as such:

"Poison Bottle [SK-10,ST-10]"

Of course this was done this way because it benefited me to do it this way, so that adding equipment, locations etc did not require changes to the code and so that if i modified the item items within backpacks of existing players was not affected too. This is an important fact to consider. You would not usually want to modify an item that is in circulation and instantly nerf or buff half of your player base, as this makes for angry players!

Please let me know if this helps at all...
Thanks for the fast responses guys. Ill look into the factories and that static/dynamic idea sounds good too.

This topic is closed to new replies.

Advertisement