Best way to handle equipped items?

I’m always trying to learn better practices and get better at programming, but this is something I’m not quite sure on how it should be done

I want to be able to equip/unequip pets efficiently

I know a couple of ways to do it

Method #1
you have loop through this to get equipped pets (which isn’t fun)

data = {["Pet32"] = {equipped = false}, ["Pet74"] = {equipped = true}}

Method #2
this allows you to have a table of all equipped pets, so you don’t have to loop and check if it’s equipped, but I’m not sure how reliable this would be with saving data because they wouldn’t be the same tables after setting/getting data

data = {["Pet2"] = {damage= 12}, ["Pet74"] = {damage= 55}}
equippedPets = {}
table.insert(equippedPets , data["Pet2"])

Method #3
my final idea is similar to the one above, but I don’t use the table value I just use the key which is what I think is the best method out of these 3

data = {["Pet32"] = {damage= 12}, ["Pet74"] = {damage= 55}}
equippedPets = {"Pet32"}

anyone got a better method, or an improvement to one I listed?

Edit

Method #3 would be the choice I would use, it would allow you to manage all of your weapon data in one central location while the others can just pass around a unique identifier. Probably the easiest to configure and manage. Method #1 is slow and it wouldn’t really make sense to keep track of whether a weapon was equipped or not in the same place as you store the data. Method #2 passing around a reference to the data table is a bit unnecessary.

2 Likes

Yeah, I updated my post to be more like my actual script with pets

The equipped pets would need to be saved

but I agree with #3 being the best out of all of them

2 Likes