Download

Network replication

Hi. I experience some problems understanding how replication works.
What i got is basic character code and projectile that i took from Shooter Game sample.

First of all, i had problems with projectile replication: if i spawn it on server - it gets replicated to clients, if i spawn it on client - it does not get replicated at all. Fixed that by changing function that spawns projectile to replicated(Reliable, Server, WithValidation); i don’t remember exactly, but it didn’t work if it was Client function(somewhat confuses me, as client specifier says it should be replicated to the clients).

Now i can’t properly set orientation of the projectile. Orientation for spawn is set in Tick by calling FindOrientation() function which simple traces under mouse position and stores orientation to FRotator FacingOrientation property. I’ve marked property as Replicated and added it in GetLifetimeReplicatedProps(). However, it does not seem to be replicated properly, as projectile spawned in totally wrong direction and the property actually is different on server and client, but it also gets updated the weird way - it gets updated if i move around, and it always points to one specified object on my level, it’s like circling around it. Tried making additional function especially for setting FacingOrientation, it changes nothing as well; it also had some strange bug, if i mark the function as Client, then client would not connect to server at all.

There are some code snapshots. Thank you in advance.


UFUNCTION(Reliable, Server, WithValidation)
void SpawnProjectile();

void AMyCharacter::SpawnProjectile_Implementation()
{
	if(ProjectileClass != NULL)
	{
		const FRotator SpawnRotation = FacingDirection;
		const FVector SpawnLocation = GetActorLocation() + GetActorForwardVector() * 20.f;

		UWorld* const World = GetWorld();

		if(World != NULL)
		{
			FActorSpawnParameters Params;
			Params.Instigator = this;

			World->SpawnActor<AProjectile>(ProjectileClass, SpawnLocation, SpawnRotation, Params);
		}
	}
}


UPROPERTY(Replicated)
FRotator FacingDirection;

..

void AMyCharacter::FindFacingDirection()
{
	APlayerController* PlayerController = Cast<APlayerController>(GetController());

	if(PlayerController != NULL)
	{
		FHitResult Hit;
		PlayerController->GetHitResultUnderCursorByChannel(TraceTypeQuery1, true, Hit);

		FRotator OldRotation = GetActorRotation();
		FRotator NewRotation = FRotationMatrix::MakeFromX(Hit.ImpactPoint - GetActorLocation()).Rotator();
		NewRotation.Roll = OldRotation.Roll;
		NewRotation.Pitch = OldRotation.Pitch;

		FacingDirection = NewRotation;
	}
}

void AMyCharacter::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);

	FindFacingDirection();
}

I think you need to use the sufixes for the methods.
Check out my thread here the code works so maybe it give you an idea of what you need to do.

Oh i do actually, just “lost” the correct code after pasting here.

Edit: after netprofiling found out that FacingDirection property is being replicated only when pawn is moving, replication frequency is quite low as well…

Edit #2: so, if to rephrase, the question would be: “How to replicate player character orientation that is driven by mouse cursor in top-down game?”
I’ve tried calling FindFacingDirection on server, but FacingDirection was not updating even on the client in this case.

Fixed. The problem was that i didn’t check bHit after tracing, and apparently it was tracing twice in a row for some reason, first trace was hitting objects, second one was not. So FacingDirection was always pointing to 0,0,0 location.