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).

How is that different from what I did? Do I need to not just have BP Base Weapon component as the target, but the actual getChildActor as the target?

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.

What you did is indeed the correct way to change the child actor at runtime.

I just wanted to clarify a bit what’s going on behind the scenes here: You won’t need a separate actor instance (as you have in the bottom left “equippedWeapon,” - you can just use the child actor component’s “Child Actor” instance.

You probably don’t want to attach a child actor to another actor, as the child actor is still “owned” by the original child actor component; it can get your code messy real quick. (You cannot detach a component from an actor, just an actor from another actor.)

You should probably use an entirely separate actor (perhaps via SpawnActor) that you attach via AttactToActor (ie abandoning the child actor component method), or fake it by killing/spawning an separate instance of the child actor in the other blueprint on pickup/drop.

So spawn actor from class of the referenced weapon in the pickup? If I instantiate a new object though, would there be any way to keep the variables associated with the weapons at a point in time? Like if I drop my weapon with 20 rounds left, pickup the other one, then switch back to the original weapon, I’d be forced to have a whole new instance right?

My apologizes for so many questions, but I thank you greatly for your help!

Using an attached actor instance instead of the child actor component would solve this, it would have its own variables.

While a bit less elegant/efficient, you can still keep track of these variables with the child actor component method, just get & set them when picking up or dropping the weapon.
(ex: PICKUP: get actor variables → delete actor → set child actor class → set child actor variables;
DROP: get child actor variables → change child actor class → spawn actor → set actor variables)

Within a Blueprint Function you could track these as local variables to keep your main BP graph tidy, or just route the data directly via the BP nodes to avoid variables altogether.

Okay, that makes sense so far. I can see that in the following image, the commented code is doing the same thing as the code outside of the comment below it. equippedWeapon is just another reference in memory to the same actor essentially, correct?

I’ll mark your answer as accepted but I do have another question if you’re willing to help. If I want the current weapon and pickup weapon basically to trade places between the player and the ground, would the bottom approach allow me to detach and enable physics, causing the Garand to fall? Proceeded by changing my child actor class that’s above to set the MP40 as equipped?

Watch this