Newb Question about rpg inventory management
Posted: Wed Apr 22, 2015 1:48 am
Hey guys, i got a question about inventory management.
Before that, some setup:
currently there are 2 parts: the backpack, and the item classes. the item classes have inheritance setup, so for example Item is an abstract base class, and armor, weapon, and books are concrete subclasses.
the backpack acts like a container object for items. currently i use a 2d array of item pointers (because subclasses are of varying sizes and im doing this in c++).
the array is organized with each column(or first order of the array) being the item subtype, and each row(second order of array) actually holding the item references where the item type matches the subtype index. for example array[0] is for armor whereas array[1][j] is for books. this works pretty well if all you do is search by item type, since it already eliminates 1/subtype of the work when searching (all bets are off when you search the entire array for more general attributes).
it has basic add, remove, and searching implemented, which is very very reesty (but it works).
the problem is that i set out attempting to make the backpack polymorphic with items, but when i go to add, remove, or search, i end up inspecting the item type (which is made available as a data member from the base class) before i downcast and do the appropriate work to it. everytime i go through the code, my brain is telling me there has to be a better way to do this. not just that, but i dont have any sort of templating, so processing each subclass of item (there are 6 subclasses) requires its own if else branch of code. One bright side I suppose is that the actual searching is a little faster than blind array iteration, but how much faster is arguable.
now the purpose of the backpack is to have an inventory that can be searchable. currently this 'sorta works'. meaning, i have separate search methods for each stat of each item. the methods are organized from most general to specific(where item name is general and attack is specific since attack only applies to weapons). i say sorta works because with this system, i do have a feasible way to let the end user check specific stats one at a time, such as "show me all weapons with attack > 40" or "show me all items with type Consumable". but if i wanted to allow queries like "attack > 40 and item name contains monado", I'd have a tougher time implementing that, as it would quickly lead to a combinatoric explosion of functions i have to write. I will not even attempt to post the code here because my backpack class is over 1600 lines of code (an indicator that i already fucked up).
I should preface by saying I'm not the sole writer of the code, but I am a heavy contributor to the project (it was a college group project, so yea...).
the code is available for viewing on https://github.com/MysticsCode/inventory_system_project
If you guys know of better or more elegant ways of handling a similar rpg inventory system, I'd love to know. Same goes for if there are ways to dynamically handle item types as well as making searching faster and more diverse.
If something isn't clear let me know and I'll elaborate on it.
Before that, some setup:
currently there are 2 parts: the backpack, and the item classes. the item classes have inheritance setup, so for example Item is an abstract base class, and armor, weapon, and books are concrete subclasses.
the backpack acts like a container object for items. currently i use a 2d array of item pointers (because subclasses are of varying sizes and im doing this in c++).
the array is organized with each column(or first order of the array) being the item subtype, and each row(second order of array) actually holding the item references where the item type matches the subtype index. for example array[0] is for armor whereas array[1][j] is for books. this works pretty well if all you do is search by item type, since it already eliminates 1/subtype of the work when searching (all bets are off when you search the entire array for more general attributes).
it has basic add, remove, and searching implemented, which is very very reesty (but it works).
the problem is that i set out attempting to make the backpack polymorphic with items, but when i go to add, remove, or search, i end up inspecting the item type (which is made available as a data member from the base class) before i downcast and do the appropriate work to it. everytime i go through the code, my brain is telling me there has to be a better way to do this. not just that, but i dont have any sort of templating, so processing each subclass of item (there are 6 subclasses) requires its own if else branch of code. One bright side I suppose is that the actual searching is a little faster than blind array iteration, but how much faster is arguable.
now the purpose of the backpack is to have an inventory that can be searchable. currently this 'sorta works'. meaning, i have separate search methods for each stat of each item. the methods are organized from most general to specific(where item name is general and attack is specific since attack only applies to weapons). i say sorta works because with this system, i do have a feasible way to let the end user check specific stats one at a time, such as "show me all weapons with attack > 40" or "show me all items with type Consumable". but if i wanted to allow queries like "attack > 40 and item name contains monado", I'd have a tougher time implementing that, as it would quickly lead to a combinatoric explosion of functions i have to write. I will not even attempt to post the code here because my backpack class is over 1600 lines of code (an indicator that i already fucked up).
I should preface by saying I'm not the sole writer of the code, but I am a heavy contributor to the project (it was a college group project, so yea...).
the code is available for viewing on https://github.com/MysticsCode/inventory_system_project
If you guys know of better or more elegant ways of handling a similar rpg inventory system, I'd love to know. Same goes for if there are ways to dynamically handle item types as well as making searching faster and more diverse.
If something isn't clear let me know and I'll elaborate on it.