I currently have an array called player equipables inventory which is the parent class for several subclasses (guns, melee items, equipment, etc). I would like to store items from each of these classes in a single array. Is there a way I can identify which subclass (guns vs melee vs equipment) so that I can reference the specific variables associated with each class while still storing these different items together in the same array?
You’re storing the class objects instead of instances. If you store the instances, you can do GetClass on it and then check the class with a cast. What I tend to do is create an enum and add it as a member that is set by each class. Then you can use a switch node and then you can cast directly from each pin.
This seems to be what I am looking for. I always understood casting as a way to get data directly from another blueprint (basically just having a blueprint reference without storing the variable) in a way often better done with interfaces. Is casting more commonly/better used to test the identify of a variable the intended application of the node?
In my example with the enum, you don’t cast to find out if it’s the right class. The enum does that. So you now know what type it is and you can safely cast to it to access its variables directly. Using casting to test if it’s a specific class is ok if done in isolation, but to test a bunch of classes, best to come up with an alternative method. Either have virtual methods, an interface or as in my example, an enum.
Also, casting isn’t about having a reference. It’s about what type you’re accessing. If class B derives from class A, you can access it as type A, but you don’t have access to functions in class B. So you can cast it to type B to have access to its functions and properties. How you make the determination if it’s of class B, that’s up to you and how you have things set up.
Casting is effectively the same as checking for an interface. Interfaces are a pattern that allow you to decouple implementation from a definition, but aren’t ‘better’.
Casting is essentially saying ‘Treat this Object as this type if you can’ and is a pretty fast operation.
