Hello, I’m trying to find a simple way to replicate player heath damage. I want each player to lose 10 points of their own health when they press the “TakeDamage” button.
Here’s what happens when a certain number of players are currently in game. These tests are all done locally in Unreal Editor 4.10.4 by editing the “Multiplayer Options->Number of Players…” (Please note that the game is ran as a dedicated server and none of the players are considered hosts)
Cases:
Only one player in game: Client #1 loses health. (Correct)
Two players in game: Both players cause the other to lose health. (Incorrect)
Three players in game: Client #1 damages Client #2. Client #2 damages Client #1. Client #3 damages either none, all other players, Client #2, or Client #1; of which is (for some reason) randomly changed every time the game launches. (Incorrect)
Here’s my code so far:
PlayerCharater.h
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay, Replicated)
float m_Health;
UFUNCTION(NetMulticast, Reliable)
void MulticastTakeDamage();
UFUNCTION(Server, Reliable, WithValidation)
void ServerTakeDamage();
void OnTakeDamage();
virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override;
PlayerCharacter.cpp
APlayerCharacter::APlayerCharacter()
{
m_Health = 100.0f;
}
void APlayerCharacter::BeginPlay()
{
Super::BeginPlay();
bReplicates = true;
}
void APlayerCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
// set up gameplay key bindings
check(InputComponent);
InputComponent->BindAction("TakeDamage", IE_Pressed, this, &APlayerCharacter::OnTakeDamage);
}
void APlayerCharacter::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(APlayerCharacter, m_Health);
}
void APlayerCharacter::MulticastTakeDamage_Implementation()
{
if (m_Health - 10.0f > 0.0f)
{
m_Health -= 10.0f;
}
else
{
m_Health = 0.0f;
}
}
void APlayerCharacter::ServerTakeDamage_Implementation()
{
MulticastTakeDamage();
}
bool APlayerCharacter::ServerTakeDamage_Validate()
{
return true;
}
void APlayerCharacter::OnTakeDamage()
{
if (HasAuthority())
{
MulticastTakeDamage();
}
else
{
ServerTakeDamage();
}
}
Please let me know what I’m doing wrong.
Thank you!