Download

Provide Actor with data before it's spawned?

Hello Forum, this is my first post here as I reached the end of my abilities.
I’m usually a Java, C# and Python kinda guy but for Uni I also need to use Unreal Engine. I worked with this engine for about one and a half years, but I never got around this particularly annoying issue: In blueprints you can’t execute Constructor-Like operations on an Actor before it’s spawned and fires all kinds of events.
Now, I am familiar with most workarounds, the most cited one being “Give the Actor a flag and only allow it to execute behavior in events when the flag is true. The flag then is set to true after calling the Data-Supply-Function.”

Obviously this works in some ways, but what if I immiedately need the data and waiting one tick or two can already be game breaking? Is there any way I can supply an Actor with needed data before it’s spawned?

1 Like

You can unless I misunderstood what’s needed. Expose variables (flag them as Instance Editable and Exposed on Spawn)

Execute what’s needed in the Construction Script. Is this not what you’re after?

1 Like

This works when the Actor itself just initialises some variables, what I’m looking for is the equivalent of a Java/C#/C++ Constructor (obj myObj = new obj(value, value, value, value))
You could imagine it like this: The best thing would be, if in the “Spawn Actor from Class” node you could put custom input keys that supply the actor with data before it’s placed into the world.
Does this explain the problem sufficiently?

Edit: Sorry, I misunderstood what you tried to say. I tried doing exactly what you said and now the Variables appear in the SpawnActor Node. Thank you so much!

2 Likes

Only to add some further information.
If you want to use C++ to spawn actors and do something before the actual spawning occurs, you can use the following code.

const FTransform SpawnTransform();
AActor* MySpawningActor = Cast<AActor>(UGameplayStatics::BeginDeferredActorSpawnFromClass(this, TheClassToSpawn, SpawnTransform));
if (MySpawningActor)
{
    // DO WHAT YOU WANT TO DO BEFORE ACTUAL SPAWNING OCCURS HERE!!!!
    UGameplayStatics::FinishSpawningActor(MySpawningActor, SpawnTransform);
}
1 Like