Efficient way to design an inventory system?
#10

Quote:
Originally Posted by Mado
Посмотреть сообщение
Got it!

Still got more questions now; is there a limit to the amount of items used if I use that method? For example, if I use these stats;

- 60 playes playing at the same time, they can carry a maximum of 30 items each
- 20 vehicles in-game, with an average loading capacity of 20 items each
- Roughly 800 dropped items in-game

Would be a total of 3000 items and thus enums. Would this be doable or would it be a big stress on the server?

And how should I link the item to the player?
Theres no real point in using an array that stores all items. Rather use it for items that are on the ground and not in any inventory.

e.g. if youre using pickups for those dropped items
Код:
enum ENUM_ITEM
{
e_item_id,
e_item_condition,
e_item_pickup_id
}
For the inventory items its much easier to have them in inventories only, in the way i described above.
Using this plugin handling a players inventory would come down to just this

Код:
// On server start for each player slot, vehicle, etc
gPlayer[playerid][item_id_vector] = cvector();
gPlayer[playerid][item_condition_vector] = cvector();

// Getting an item
itemid = cvector_get(gPlayer[playerid][item_id_vector], item_slot);
itemCondition = cvector_get(gPlayer[playerid][item_condition_vector], item_slot);

// Adding an item
cvector_push_back(gPlayer[playerid][item_id_vector], item_id);
cvector_push_back(gPlayer[playerid][item_condition_vector], item_condition);

// Removing an item
cvector_remove(gPlayer[playerid][item_id_vector], item_slot);
cvector_remove(gPlayer[playerid][item_condition_vector], item_slot);

// On player leave
// Save items to database, then clear the vector so the next player wont "inherit" the items in that player slot
cvector_clear(gPlayer[playerid][item_id_vector]);
cvector_clear(gPlayer[playerid][item_condition_vector]);
No unused arrays wasting memory, good performance. You might also reduce it to a single vector by putting itemid and condition in one cell (e.g. first 10 bit for item id, last 22 bit for condition). Its even possible to create an iterator for vectors, so you can loop through inventories with foreach.
Fixed-size arrays just are a bad choice when the range of inventory sizes is rather big.

My complete multilayer inventory engine, including save/load, isnt even 400 lines.
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)