Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Managing Items and Inventory  (Read 2138 times)

0 Members and 1 Guest are viewing this topic.

grim

  • Guest
Managing Items and Inventory
« on: May 02, 2014, 04:25:01 am »
First some background info: the game I'm working on is really just for my portfolio, but I am treating it as if I actually planned on releasing it for commercial. So even though I may not actually have a lot of item types or a lot of items for each type, I would still like to implement code that is efficient in handling such a scenario.  Though I do plan to have at least 5 item types, with somewhere between 10-20 items per type. The amount of items that can be stored in an inventory is based on weight. Also, I plan on implementing some form of multiplayer with a small server size of say...4 people.

I have an Item class that has several sub classes (although technically just one at the moment). Information on all items will be aquired from lua scripts.
My first question is how to handle items and their data in general? Specifically, how should they be initially loaded into the game? Should I just create an instance of the item object, load its information in straight from the lua script, and then place the object in the inventory of the player that obtained it? OR, for each npc/player that has an inventory system, they will have their own list of all the items in the game? Or some other third way.

I've actually given the 2nd one some more thought but I don't know if it would be very efficient in terms of memory and data allocation. Basically the idea is that each inventory object (1 for each player and possibly for npcs) will have its own list of all the items in the game.  It will also contain a data structure (assume empty in this case) that will store the items that the person has. So when the person acquires a new item, it will be located in the master list, the item count will be updated, and then a copy of that object will be put into the personal list. (Yeah, it doesn't sound pretty, but I've been awake for 36 hours straight now because of finals).

My second question is, regardless of how the master item list is handled, what would be the best data structure to use to store the items in a person's inventory, given that items will be removed, added and can be sorted. I've looked at vectors, linked lists, and maps but I'm having a hard time deciding which would be better.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Managing Items and Inventory
« Reply #1 on: May 02, 2014, 08:02:41 am »
For the container, probably a vector.  You aren't going to be inserting or removing items from it dozens of times a second (...right?), so performance is a non-issue, so you might as well use a vector so you have random access and full control over ordering.

For the item loading thing, I'm really not sure (this sounds far more complicated than any item system I ever tried to write), but my gut feeling is it would make far more sense to have a single ItemProperties in main() that accesses the lua scripts in its constructor, and pass a const reference to it to all of your inventory objects.  Though I'm not even sure why you'd use lua scripts for this (why use a scripting language to define *data* rather than scripted events?), so I may be missing something.

grim

  • Guest
Re: Managing Items and Inventory
« Reply #2 on: May 02, 2014, 10:08:55 am »
I'm using lua for both data and events...at the moment.  I really like Lua's single data structure approach and it just seemed like a good way to store the data so that when the game starts the information can be pulled from the script once and put into a master list.  I mean if there's another way to handle the master item list that's simple and fast, then I would gladly do it.

My first question may seem complicated because of all the detail I put into it.  Basically I want to store all items and their data in a place that is easily accessible by all objects in the program that need access to it, WITHOUT actually having to hardcode a c++ data structure with each item object stored in it (realistically it'd be kind of a pain in the ass to have to recompile my program everytime I wanted to change something about an item object).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Managing Items and Inventory
« Reply #3 on: May 02, 2014, 10:14:33 am »
You can't change the C++ code/class structure at runtime, thus you'll have to go with a generic approach. Either make sure all your items have the same properties or make it possible to have any kind of property added to the inventory item.
For the first approach you can also use some derived classes to specialize for a few item types, but of course, you'd need to rebuild for each new type.

Storing is easy, as you simple store a std::unique_ptr of you item base class in a vector or maybe a map.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Managing Items and Inventory
« Reply #4 on: May 02, 2014, 07:12:53 pm »
Of course, recompiling the code whenever the script changes defeats the purpose of scripting in the first place.

As eXpl0it3r says, you should have a data structure that stores items appropriately. I would not adopt the Lua tables 1:1 and try to store everything in a global table, but rather directly insert the data in containers that are meaningful for the game logic. Can you be more concrete about how such data/events would look? Ideally simplified, we don't need your whole script ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: