Basically, I am creating a pseudo-RPG game where a player has an inventory of items and a character doll (to know which items are currently equipped).
What do you recommend is the best way to handle the collection of currently equipped items?
What I currently have is an EquipmentSlot enum that contains all of the possible locations for equipped items on the player, which I can set as a property on each Item that the player has.
public enum EquipmentSlot
{
Head,
Chest,
Arms,
Legs,
Feet,
OffHand,
MainHand
}
Then I have a dictionary that contains each enum as a key, initializing them all to be null in the Player constructor:
PlayerEquipment = new Dictionary<EquipmentSlot, Item>(7);
PlayerEquipment.Add(EquipmentSlot.Head, null);
PlayerEquipment.Add(EquipmentSlot.Chest, null);
PlayerEquipment.Add(EquipmentSlot.Arms, null);
PlayerEquipment.Add(EquipmentSlot.Legs, null);
PlayerEquipment.Add(EquipmentSlot.Feet, null);
PlayerEquipment.Add(EquipmentSlot.OffHand, null);
PlayerEquipment.Add(EquipmentSlot.MainHand, null);
But as I’m coding this I’m starting to realize that it won’t work because I can’t access the keys as enumerations in other methods of my Player, because they are added in the constructor. I’m not sure where else I could add them to have them available to the rest of the class.
Is my dictionary approach the wrong way to go about this?
Provided you define
PlayerEquipmentas a class member, and not a local variable, you can access it anywhere in your class:Note that, if you define the
enuminside of your class, you’ll have to fully qualify it when using it from other classes, ie:Player.EquipmentSlot.Head. If, however, its defined outside of the class, you can just useEquipmentSlot.Head(assuming the same namespace or the appropriate using clause exists).