Hello everyone !
I have problem with one of my replicated variable, which is the owning pawn of an actor.
I have a building class, in which I have this :
Building.h (Derived from APawn)
// Getter/Setter for the health
virtual void SetHealth(int newHealth);
virtual int GetHealth();
// Getter/Setter for the actor owner
virtual void SetActorOwner(APawn* newOwner);
virtual APawn* GetActorOwner();
// Define the commander that own the actor
UPROPERTY(Replicated)
APawn* owningCommander;
// Health of the actor
UPROPERTY(Replicated)
int health;
Building.cpp
// Getter/Setter for the actor owner
void ABuilding::SetActorOwner(APawn* newOwner) {
owningCommander = newOwner;
}
APawn* ABuilding::GetActorOwner() {
return owningCommander;
}
void ABuilding::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
// Replicate to everyone
DOREPLIFETIME(ABuilding, health);
DOREPLIFETIME(ABuilding, owningCommander);
}
Now, I’m trying to spawn a child of this class from the GameMode, one per player, and set the player as his owner in the AActor AWMGamemode::ChoosePlayerStart_Implementation(AController Player)** function.
AFrench_HeadQuarter* headQuarter = GetWorld()->SpawnActor<AFrench_HeadQuarter>(Location, Rotation, SpawnInfo);
headQuarter->SetActorOwner(Player->GetPawn());
headQuarter->SetHealth(50);
The pawn is well replicated over the network, as well as the health. But when I try, in my HUD, to compare the owning actor with the local commander (for box selection), it fails :
if( Cast<APawn>(Cast<ISelectableActorInterface>(*ActorItr)->GetActorOwner()) == Cast<APawn>(this->GetOwningPawn()) ) {
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Success!"));
ISelectableActorInterface is an interface implemented by multiples classes so I can have some common functions. The interface do work, but something seems to be wrong with the references. Even the server cannot select his own unit. I really don’t understand why. Building & Commander are set to bReplicates = true in the constructor. Also, I’m setting the owner in the GameMode which means this is on the server (since GameMode is server only, if I’m not mistaking ?). Why would the health update but not the commander ? I don’t get it.
Note that without this “if” condition, the selection box works perfectly, and you can even damage the selected actor by pressing a key. Damages are replicated correctly over network for/from both clients and server, even on dedicated server.
Maybe the problem is not the actor reference but the “Player->GetPawn()”, but in that case, that would at least work on the server side…
Thanks !