Adding components/doing anything pre-spawn/before Begin Play

Solved

Hey guys! I'm trying to add components to newly spawned actor. But i'd like to add them on PostSpawn or smth like that, so it happens before Begin Play of that actor. Is there any way to achieve it? I'v found this https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Actors/ActorLifecycle/index.html but can't understand how to do anything on Post Events in SpawnActor line. Thanks in advance!

Posted Nov 15, 2015, 3:22 PM UTC

26
NaojirouDev•

Construction Script does that.

Edit: To expand a bit more: If you pass a parameter to the object being spawned with Expose on Spawn ticked and proceed to spawn this object, you can use this variable on your construction script and as you can check its name, it will be used on the construction of the object, then Begin Play will be called. Construction Script even takes place while you are not playing the game. When you manually place an object on the level and change its parameters that is used on the construction script, you can see that your actor/pawn/whatever gets updated on the fly, even when not you are in the play mode.

2
ExterminatioOP•

Thank you for the answer! :) Was thinking and googling about construction script like 5 mins ago. Kind of silly of me then posting that question (seems easy), but i'v read everywhere before that Construction Script is more like designer solution q_q Also, as I'v just read I need a check in there if object is spawned, since Construction Script runs each time object's Transform is changes, right? Loading UE to check Thanks for help!

1
NaojirouDev•

Actually not, I have some objects that I initialize with Construction Script that takes some parameters, here is a video to show you what I am talking about:

https://www.youtube.com/watch?v=z8yWeinNYU4

The number of slices, their rotations, the texts it has are handled with the construction script and you can see that as I change values, they are being added instantly. The things that I do in the play mode as you can imagine are rotational changes=transform changes and you would expect that IF the construction script was called on every time it is changed, they would have stood in place but they actually move. So with 99% precision I can say that it is a one time call.

2
ExterminatioOP•

Ok...here I hit the wall one more time. What if I spawn actors dynamically...means I can't expose its variables, but only pass it through interface...if so - interfaces events run after Begin Play, right?:( And there's no way to pass it to Construction Script

1
NaojirouDev•

When you spawn actors dynamically, you can still expose variables during spawn. You need to create a variable to store that value, and then use that value on construction script. When the spawn node is called, first thing it will do is to set the variables, then call the construction script, then the event begin play. Instigator as you mentioned on other post is also a way to do it. Just passing this one for your knowledge. The setup you need to do is:

  • Create variable for actor
  • Make the variable editable by clicking the eye icon on its right
  • From the details panel, check the expose on spawn checkbox and your spawn node will have another pin that allows you to set the value of the variable
1
ExterminatioOP•

Soo, I come up with casting to Instigator from Construction Script and getting everything I need. Again, thank you very much for help! :)

1