Ok so i want to spawn an actor on the client-only but it crashes when i spawn the object, I narrowed it
down to the fact that there are multiple worlds on a Server-Client game. So my question is, how do i
get the world that my client is in so i can spawn an actor there?
If you only spawn it on the client, who will control it? Who will handle its behavior? You should spawn it on the server - always - and make it visible only to the needed clients.
No i know, I need to spawn it on the client only so i can access variables from it on the client and then destroy it.
To be honest that doesn’t really make sense to me, but if its what you need: spawn it on the server, replicate it, the client will have access as well.
Yeah i wanted to avoid that route but i’m spawning it on the server now, Thanks anyway.
You could get the controller for the player and check if it is the is the local controller.
And if so spawn what you need, this whould in effect spawn it in the clients world only.
Here you go:
//this code can be executed on server or client, and it will spawn a LOCAL object (not replicated)
FActorSpawnParameters SpawnInfo;
SpawnInfo.bNoCollisionFail = false;
SpawnInfo.bRemoteOwned = false; //we are responsible for this Actor
ACharacter* const Unit = MyWorld->SpawnActor<ACharacter>(*UnitClass, SpawnLocation, FaceDirection, SpawnInfo);
if (Unit)
{
Unit->SetReplicates(false); //don't replicate it.
return Unit;
}