x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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

Product Version: UE 4.17
Tags:
capture.png (273.8 kB)
more ▼

asked Aug 18 '17 at 10:56 PM in Blueprint Scripting

avatar image

Dieinthedark93
27 3 7 11

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

alt text

capture.png (72.6 kB)
more ▼

answered Aug 19 '17 at 12:14 AM

avatar image

DMershawn
46 5 3

avatar image Dieinthedark93 Aug 19 '17 at 12:19 AM

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?

avatar image DMershawn Aug 19 '17 at 12:27 AM

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.

avatar image Dieinthedark93 Aug 19 '17 at 12:53 AM

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? alt text

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?

capture.png (203.0 kB)
avatar image DMershawn Aug 19 '17 at 01:08 AM

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.

avatar image Dieinthedark93 Aug 19 '17 at 01:40 AM

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!

avatar image DMershawn Aug 19 '17 at 01:47 AM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question