Set variables on Child Actor Component

I have a blueprint class WallReplicator that creates a number of child components (class EyeActor) in its Construction Script.

After I create each child eye, I want to set a variable on it. I tried to do this by casing the result of Child Actor Component to the EyeActor class, but Unreal tells me that this will always fail. Evidently I do not understand what a “child actor component” is.

How can I make this work?

3 Likes

Doing additional research, apparently I need to use the Get Child Actor node (which does not include its name once in use) to get the actor out of the component, and then I can cast the actor to the correct class:

2 Likes

It seems that this solution does not fully work.

In above example the Owning Wall is not available in the Construction Script of the new (child) EyeActor.
So if you use Owning Wall in the Construction Script of EyeActor you will get an null warning or error.

For example: You create a new EyeActor (in parents Construct), on EyeActor Construct set default Eye Color to a value based on ‘OwningWall’.

Note: Since the Owning Wall is set in the parents Construction Script it will be available on Begin Play of the EyeActor.

Great to know! If there is a better way to accomplish the same goal without these limitations, please post it as an answer and I’ll gladly accept it instead.

Hey Gavin,

Basically you were on the right track.

I found a solution which goes around the Construction Script errors in the Child.

  1. Create a new Function in the Child (in your case Eye Actor) and name it something like ‘Do Construction’.
  2. Move all functionality from the Construction Script to the new ‘Do Construction’ Function.
  3. (optional) Add a comment (press ‘c’) in construction script ‘DON’T USE! USE Do Construction Function’
  4. Call ‘Do Construction’ at the end of your posted solution.

Additional notes:

  • After casting to the Child, in your case EyeActor, I always store this in a variable of the parent (for easy access). Then I make all following calls to use this variable, which makes the code not have so many wires.

  • Before calling the new function ‘Do Construction’ ensure you first set all required variables. In your case this probably won’t happen, however I sometimes don’t pass the self reference but set other variables.

  • An additional benefit of the new function is that you can use input parameters, however I don’t use this and use Set like you did. This is simply to keep ‘Do Construction’ consistent with Construction Script.

  • The new ‘Do Construction’ function is executed After the original Construction Script, so before Begin Play. I use Add Child Actor Component in my Parent’s Construction Script which makes everything work inside the Unreal Editor :smiley:
    (Begin Play is not executed during normal editing, but Construction Script is)

5 Likes

Great answer!

Thanks a lot for this answer, it’s what I was looking for.

The child actor should be made basically empty and only spawn everyhing when “Do construction” is called. Because otherwise you have your default thing created and on top of it you get your new construction as a result of “Do construction”.