Best way to delete the correct item from a custom Inventory?

I’m working on an rpg and so having a custom rpg is needed.

In my old rpg’s when the player deletes an item, it would loop through the Inventory table and delete the first value it comes across with the same damage, removes it and then breaks the loop. This isn’t a problem because the items weren’t different ( so 1 “Learners Sword” would be the exact same as another “Learners Sword”) though it could be annoying to some.

With my rpg, weapons are gonna have varying stats (random attack value chosen between two other values, elements, etc) so I can’t do what I do normally.

I was thinking about giving each item a unique ID when an item is added starting at 1 for the player.

Ex. Player begins the game with a Learners Sword (ID 1). He then gets a Health Potion (ID 2).

Then when I loop through the inventory to display the items, I’d set a value or whatever to the ID of the item, then when the player clicks it; it would loop through the inventory table and remove the item with that ID.

The only problem I can imagine if eventually it hit’s the highest number (can’t remember it exactly but I think it’s 2^32 (possibly * 10)) then the Inventory is gonna break.

I know it’s really unlikely for that to happen ( there’s 25 slots for each category: weapons, armour, consumables and materials) but I’d like to know if there’s a better way to do it. Thanks.

Add a new Table for each item Class, [“Swords” ] = {} for Swords only, something like that. Or something to add infront of the ID

And how can you so sure that you will have more than 2^32 * 10 items?

I’m not certain that I’ll have more than 2^32*10 items but I’d rather prepare for the worst tbh.

I don’t think having a table for each class of item would work.

Because that would create a lot of unnecessary work.

I think I should just do it the ID way, I’m doubtful it’ll ever reach the highest number ever,

You can limit the player’s inventory. Memory’s not unlimited, so your inventory shouldn’t be, too.

I plan to limit the Inventory. But say the player uses up all 25 slots in each category, then uses all those items / deletes them. Then fills up all 25 slots in each category again, that’s the ID at 100 now.

Like I said It’s only a small possibility of it reaching the highest number possible, but just curious if there’s a better / different way.

So you’re creating a new entry in a datastore whenever a weapon is generated because its stats are randomized. You then save the ID of this newly generated item. That’s no good. Why not just store the stats of the item in the inventory itself?

For example, assuming your inventory could have gaps in it like a typical RPG, you can serlalize your inventory like so:

inventory = {
  ["1"] = {
    id = "starterSword",
    damage = 1234
  },
  ["2"] = nil, -- blank space in the inventory
  ["3"] = {
    id = "smallHealthPotion",
  }
}

Sorry if I’m not explaining it that well.

I’d store it like this.

Weapons = {{Name = "Learners Sword", Attack = 10, ID = 1, Equipped = true},{Name = "Learners Greatsword", Attack = 15, ID = 2, Equipped = false}},

Then I’d store the highest ID in a seperate variable in a Stats table like

Stats = {highestID = 2}
1 Like

What do you use the ID for? Its position in the inventory?

Yes.

So say the player selects the last item they have in the Weapon part of the Inventory, it would loop through the Weapon Inventory and delete the value with the same ID instead of the first item it comes across with the same name.

Can’t you use the index of the item in the inventory as its ID, and do table.remove(inventory, indexOfItem) to remove it?

Lmao. I never thought about that. :woman_facepalming: Thank you!