Hi guys,
I am trying to spawn an actor with some dynamic data which is obtained from a database. These are things like health, weapon classes and the data of those weapons etc. Currently i am struggling to get the data correct on both the server and client. Which is the correct way to initialize an actor/character? I would prefer a way without making all my variables replicated. I know about the option to only replicate initially, but i would think it should be possible to first completely construct an actor on the server before sending any data to the client about it? Or am i missing something here about the unreal architecture?
Thanks for any advise on this part!
1 Like
Spawning an actor deferred may be what you are looking for. The normal method of spawning an actor, via UWorld::SpawnActor creates the actor and immediately initializes it, for example it calls BeginPlay() automatically. Spawning an actor deferred allows you to do dynamic stuff before the actor’s initialization is finalized. The way to do this:
- Spawn the actor with UWorld::SpawnActorDeferred()
- Do your dynamic initialization stuff here
- Call the spawn actor’s FinishSpawning function
Example code:
UWorld* World = GetWorld();
const FTransform SpawnLocAndRotation;
AMyActor* MyActor = World->SpawnActorDeferred<AMyActor>(AMyActor::StaticClass(), SpawnLocAndRotation);
MyActor->MySetupFunction( ... );
MyActor->FinishSpawning(SpawnLocAndRotation);
Now you’re certain your dynamic values are set in time before any replication.
12 Likes
For some reason I had to change the UWorld* to not be a const, otherwise it wouldn’t compile.
UWorld* World = GetWorld();
‘T *UWorld::SpawnActorDeferred(UClass *,const FTransform &,AActor *,APawn ,ESpawnActorCollisionHandlingMethod)’:
cannot convert ‘this’ pointer from ‘const UWorld’ to ‘UWorld &’
Seems fairly obvious why, really. const
means it can’t be modified, but to add an actor to the world, you have to be modifying it.
1 Like
You meant UWorld* const
, not const UWorld*
. The two are very different.
I updated the example code.
1 Like