Hello,
I created two teams in my game. I wanted to assign two colors for each team as they spawn in but I have problems with material replication.
In Character I made this:
Character.h
UFUNCTION(reliable, Server, WithValidation)
void SERVER_SetMaterial(AMyPlayerController* PC);
void SERVER_SetMaterial_Implementation(AMyPlayerController* PC);
bool SERVER_SetMaterial_Validate(AMyPlayerController* PC) {return true;};
UPROPERTY(ReplicatedUsing = OnRep_CheckTeam)
bool CheckTeam;
UPROPERTY(Replicated)
UMaterialInstance * MI;
UFUNCTION()
virtual void OnRep_CheckTeam();
Character.cpp
void AProjekat2Character::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AProjekat2Character, MI);
DOREPLIFETIME(AProjekat2Character, CheckTeam);
}
void AProjekat2Character::SERVER_SetMaterial_Implementation(AMyPlayerController* PC)
{
if (Role== ROLE_Authority)
{
MI = nullptr;
AMyPlayerState * PS = Cast<AMyPlayerState>(PC->PlayerState);
AMyPlayerController * PlayerContr = Cast<AMyPlayerController>(PC);
if (PS->bTeamB == 1)
{
MI = (UMaterialInstance*)PlayerContr->TheMaterialA;
CheckTeam = !CheckTeam;
OnRep_CheckTeam();
}
else if (PS->bTeamB == 2)
{
MI = (UMaterialInstance*)PlayerContr->TheMaterialB;
CheckTeam = !CheckTeam;
OnRep_CheckTeam();
}
}
}
void AProjekat2Character::OnRep_CheckTeam()
{
if (MI != nullptr)
this->GetMesh()->SetMaterial(0, MI);
}
When I call SERVER_SetMaterial(AMyPlayerController* PC) it should be executed on Server, and it should set up replicated property UMaterialInstance * MI; and change the state of bool CheckTeam;. As I read on instructions ReplicateUsing function will execute on clients when the state of bool is changed on the server, but nothing happens in my case. Everything I get is correct colors on the server because I called void OnRep_CheckTeam(); from the server side.
Can some1 give me words of advice, where I made mistake?
Thanks in advance! ![]()