Updating (child) actor components at runtime, or alternatives?
I'm trying to wrap my head around UE4 components, child actor components specifically. Here's the setup:
BaseWeapon - parent for all weapons, has skeletal mesh component, particle system component etc FirstPersonCharacter - has a skeletal mesh component for the arms and acts as the parent for the BaseWeapon component
The player needs BaseWeapon as a component, not just a skeletal/weapon mesh, or else the character would have to hold onto the particle system component and all the other components that are currently on the weapon. (Player class would be bloated with components).
Now, in trying to create a weapon pickup, I've got a WeaponPickup class that has BoxCollsion and BaseWeapon as components. This means I can make a pickup of any weapon since they're children of BaseWeapon.
So the question is, is there a way I can change my child actor class of the BaseWeapon component at runtime (change from a Garand class to a MP40 for instance, both extend from BaseWeapon).
If not, do I need to use specific instances for the component? Such that rather than using BaseWeapon as a component I would use Garand, and upon picking up a new weapon detach that component and re-attach the MP40 component?
What I tried is pictured below. Pickup Weapon is a reference to the BaseWeapon of the WeaponPickup. The BP Base Weapon node above is a refernce to the highlighted component (yes I know its not pictured wired up).
asked Aug 18 '17 at 10:56 PM in Blueprint Scripting
IIRC Since an actor can't be put inside of another actor, the "child actor component" is really more of a reference than an actual component. Luckily this makes it quite easy to change at runtime, albeit a bit more confusing than a regular variable.
You can change one actor to another by referencing the child actor in BP (just drag it onto the graph) and use the "Set child actor" node to select the new actor's class.
If you want to access the actor itself, get the "Child Actor" node from the "Child Actor Component" and then cast it to the class of the Actor that it is.
answered Aug 19 '17 at 12:14 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here