x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Damage Player Health & Replication C++

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!

Product Version: UE 4.10
Tags:
more ▼

asked Jun 23 '16 at 06:06 AM in C++ Programming

avatar image

Vergbergler
20 6 8 9

avatar image Vergbergler Jun 27 '16 at 03:42 AM

Could I please get an answer to this question?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question