So, Im trying to create a survival game. In this game I want the player to be able to equip different pieces of equipment. Weapons, Clothing, Vests, Backpacks etc. I current have it setup where each equipment piece has a variable containing a reference to the currently equipped object. Each variable is a reference to the base class of that object type (backpack_base, vest_base etc). The issue I am having is when creating the object to be equipped the return value type for SpawnActor is a static mesh. I cannot connect the static mesh when assigning the variable because it does not match the class of variable type. Is there any way to convert the static mesh reference to the class that the variable is referencing. I need to preserve the class of the variable for performance to prevent unnecessary casting and it would result in having to change the code everywhere that variable is used. See the referenced pictures. Any help or advice would be greatly appreciated as I could not find any clear answers.
So, I would need to make all my equipment base classes inherit from a single master class that has all the variables that the classes share? And then use that class master class as the class for my variables?
hello. I don’t know how to pronounce your name BTW
- you can’t convert or assign variables of different classes to each other. in your case one is ‘static mesh’ and the other is ‘backpack base’. unless they have child-parent relation.
so, here is a suggestion
- make your all pickable items inherit(parent) from the same class.
no, that is not how it works. in a master class, you only need to create the variable and functions that are common across all pickable in a master class. And then define them in child class. then other variables defined in their respective child classes.
Okay, this helps. I do however have another issue. I can now assign the objects to my variables which are now of type MasterBase and all my equipment inherit from that base; However, now when I am referencing them in my blueprints I only have access to the base variables defined on MasterBase and am unable to access the specific variables on the different classes such as my backpacks capacity. The only way to fix this that I know of is to cast to that object class. Is there anyway to get the variables from the children without casting?
yes, there is a solution without casting
- first, make a function in the parent class and now override it in the child class.
like make a “GetValue” function in the parent class that returns an integer, now override it in child classes to make it return different variables. for example, backpack class returns its capacity, pistol class returns its ammo, vest returns its health, etc.
On second thought, casting is an excellent method for communication between blueprints until they don’t have hardcore references of assets. and you can avoid hardcore referencing by making multilevel inheritance(multilevel parent-child blueprints).
- like in parent class make all variables and functionalities and only child class have all hardcore refrences