Hi everyone,
I’m trying to set a character position using SetActorLocation
but randomly, my character goes to the wrong spot.
Here my full character creation process:
- I load my level using
GetWorld()->ServerTravel(TEXT("/MAPNAME?listen?SpectatorOnly=1"));
- In
MyGameMode::PostLogin
function, I callClient_PostLogin
(Client RPC) to check weither or not I need to spawn a MyCharacter. - If yes, in the same previous
Client_PostLogin
, I callServer_SpawnAndPossessVIVECharacter
(Server RPC) in which I spawn an possess a MyCharacter :
Here is my spawn code
// Spawn the default pawn.
// For now I just don't care about the spawn position.
MyCharacter* NewPawn =
Cast<MyCharacter>(GetWorld()->SpawnActor(
GameMode->DefaultPawnClass));
// Possess the new pawn
Possess(NewPawn);
-
Obviously, my
MyCharacter::BeginPlay
will automatically be called. There, I set manually the position using the following code:if (GameInstance->IsServer())
{
// Function bellow
FVector StartLocation(Server_GetVIVEStartLocation());
SetActorLocation(StartLocation, false, NULL,
ETeleportType::TeleportPhysics);
}///////////////////////////////////////////////////////////////////////////////
FVector MyCharacter::Server_GetVIVEStartLocation()
{
for (TObjectIterator Itr; Itr; ++Itr)
{
APlayerStart* PlayerStart = *Itr;
if (PlayerStart->GetName().Contains(“ViveStart”))
{
return PlayerStart->GetActorLocation();
}
}
return FVector(0, 0, 0);
}
The actual issue
I have two APlayerStart
in my level, but of course only one is called “ViveStart”'. Randomly, my newly spawn MyCharacter pick of the two. No matter which location I tell him to be. I’ve also noticed that when I manually disconnect and reconnect the client, my character can also be at (0, 0, 0).
What am I missing there ? Possible issues could be:
-
SetActorLocation
is not working as I expect - All
APlayerStart
are maybe not created yet when I’m using them. But since I use them in aBeginPlay
function they should already be in the world. - I’m an idiot
Thanks for reading. Hope you will help me figure this out. I’m just starting to understand properly Unreal Engine network “magic”.
Best regards.