Download

Actor spawned on server not replicated to clients -why?

I’m sure I’m missing something here, hopefully someone can spot it. I’m trying to spawn a projectile so that it appears on both the server and the clients. At the moment I can see it being spawned on the server (listen server), but it’s not replicated to the clients. Here’s my code:

Code in my pawn to spawn on the server:

.h




        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Custom)
	TSubclassOf<ALinearProjectile> FireOneProjectile;

	UFUNCTION(Reliable, Server, WithValidation)
	void ServerSpawnProjectileOne();
	virtual void ServerSpawnProjectileOne_Implementation();
	virtual bool ServerSpawnProjectileOne_Validate();

.cpp




void APlayerPawnCombat::FireOne()
{
	ServerSpawnProjectileOne();
}



void APlayerPawnCombat::ServerSpawnProjectileOne_Implementation()
{
	
	GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Red, FString("FireONE! ") + GetName() + FString(" : ") + FString::FromInt(Role));

	ALinearProjectile* NewActor = GetWorld()->SpawnActor<ALinearProjectile>(FireOneProjectile, GetActorLocation(), GetActorRotation());

	if (!NewActor)
	{
		GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Red, FString("Spawn FAILED"));
	}
	else
	{
		GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, FString("Spawn OK at ")+NewActor->GetActorLocation().ToString());
	}

}

bool APlayerPawnCombat::ServerSpawnProjectileOne_Validate() { return true; }


And here’s the constructor for my projectile:




ALinearProjectile::ALinearProjectile(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	StaticMeshComponent = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("StaticMesh"));
	//StaticMeshComponent->AttachParent = RootComponent;
	RootComponent = StaticMeshComponent;

	bReplicates = true;
	bAlwaysRelevant = true;
	bReplicateMovement = true;
	bOnlyRelevantToOwner = false;
	//bStaticMeshReplicateMovement = true;

	SetRemoteRoleForBackwardsCompat(ROLE_SimulatedProxy);

}

bool ALinearProjectile::IsNetRelevantFor(const APlayerController* RealViewer, const AActor* Viewer, const FVector& SrcLocation) const
{
	return true; // ALWAYS RELEVANT
}


Can anyone see what I’m doing wrong?

Instead of directly setting bReplicates to true, could you try calling SetReplicates(true) instead? (https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/SetReplicates/index.html)

That setter function does a bit more than toggling the boolean, and that may be one of the things that fixes it.

Turns out the code does work. After spending an hour working on it I just rebooted the computer and it ran fine. I might try that sooner next time.

I had the same issue over and over again. You don’t have to restart your PC. Sometimes the editor seems to be out of sync. You can see this by looking at the compile button, it will disappear sometimes. Just restart the editor and you will be fine.