Best way to house multiple different weapon classes in a singular inventory so that they can be swapped between?

Currently I have melee weapons, ranged weapons, and throwable items and the player can hold multiple of each (but only one equipped in hand at a time). If all of the items were a singular class, I could set each item as an array element determined by a pickup class, and then swap between them would be easy since I could just assign each to an index. The problem is that this would then leave 2/3rds of the variables for any given item unused. If instead I break the items into subclasses, however, then I can no longer access the class defaults for the child classes, only the parent class which allows them to all be stored in the same array. My current solution is to use multiple classes giving each item slot a corresponding inventory index integer. I can then keep track of which item is being holstered, equipped, and or referenced without needing an array but this seems to involve a lot of additional checks. It works fine but I feel like there is a far more efficient way to do this. Is it possible to somehow store all of these items from different classes in a singular array while still allowing me to identify what subclass an item is from and access that child classes variables?

For example, if I wanted to set the default movement speed based on the current item I would select the set item of given class depending on the inventory integer assigned.

Honestly, I didn’t understand what’s your problem with subclasses. I mean if the “Speed while walking” variable is part of the base class, you can totally get it from all of the subclasses objects. It would just be the non-shared variables of subclasses (“fire range”, “current ammo”,…) that you can’t directly access, since you are limited to the common base.

There are (at most) 3 ways to deal with it.

  1. Cast your objects to the class you expect. I wouldn’t recommend.
  2. Store all common data in the base class
  3. Use interfaces to retrieve required data from the object. That way your objects doesn’t even have to share a common base.

Casting can easily lead to a lot of other different issues, but can help to understand what’s going on. OOP wise.

The way I might handle this is parent class “Item”, subclass EquipableItem, subclass WeaponBase, Subclass MeleeWeapon, Subclass of that, BroadswordWeapon. Item has a variable ‘ItemType’ of type GameplayTag. BroadswordWeapon’s ItemType is “Weapon.TwoHanded”. When looking to equip/use, check and react to your Item’s itemtype, a Switch on Gameplaytag can easy to manage for quite a few types. Eventually you will need to Cast into your level of subclass that defines the behavior/variables, but using tags as filters is really fast and easy.

Your inventories can be a basic array of Item. Item has vars for things like Weight, Cost, InventoryImage. WeaponBase might have vars like CurrentDurability, MeleeWeapon might have vars SlashDamage, ImpactDamage, etc.

There are different ways to do this if you’re expecting a lot of different specific behaviors, but I found this kind of approach easy to setup up and maintain and a good starting point.