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"

Apply Damage From Client to Server in C++

So I have been searching for hours now but could not find a solution to this and replication still confuses me a bit, especially in C++. I want my Characters to be able to apply damage to eachother but this only works from server to client so how can I get this to work in the opposite direction?

My code right now (a bit condensed):

ADevCharacter.h:

 public:
 UPROPERTY(Replicated)
         float Health;
 

ADevCharacter.cpp:

 void ADevCharacter::Fire() {
     FCollisionQueryParams RV_TraceParams = FCollisionQueryParams(FName(TEXT("RV_Trace")), true, this);
     RV_TraceParams.bTraceComplex = true;
     RV_TraceParams.bTraceAsyncScene = true;
     RV_TraceParams.bReturnPhysicalMaterial = false;
 
     FHitResult RV_Hit(ForceInit);
 
     FVector Start = this->GetActorLocation();
     FVector End = (this->GetActorForwardVector() * 300.f) + this->GetActorLocation();
 
     bool isHit = GetWorld()->LineTraceSingleByObjectType(RV_Hit, Start, End, ECC_Pawn, RV_TraceParams);
 
     DrawDebugLine(GetWorld(), Start, End, FColor::Green, false, 1, 0, 5);
 
     TSubclassOf<UDamageType> P;
 
     ADevCharacter* HitChar = Cast<ADevCharacter>(RV_Hit.GetActor());
     if (isHit) {
 
         FPointDamageEvent* DamageEvent = new FPointDamageEvent();
         GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("HIT"));
 
         UGameplayStatics::ApplyPointDamage(HitChar,10.f, this->GetActorForwardVector(),RV_Hit, this->GetController(), this, P);
     }
 }
 
 float ADevCharacter::TakeDamage(float DamageAmount, FDamageEvent const &DamageEvent, AController *EventInstigatior, AActor *DamageCauser) {
     Health -=  DamageAmount;
     if (Health < 0) {
         AWarPlayerController* PlayerController = Cast<AWarPlayerController>(Controller);
         if (PlayerController) {
             PlayerController->OnKilled();
             Destroy();
         }    
     }
 
     return DamageAmount;
 }


Product Version: UE 4.20
Tags:
more ▼

asked Aug 21 '18 at 01:33 AM in C++ Programming

avatar image

Rokolell
3 5

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

You definitely wouldn't want it to work in the opposite direction. Unreal Engine uses an Authoritative Server model, meaning the server has control over replicated values. This is to prevent hacking of important variables such as health on the client side. You should do all of your hit detection on the server.

more ▼

answered Aug 21 '18 at 03:02 PM

avatar image

HailstoneRyan
679 37 23 53

(comments are locked)
10|2000 characters needed characters left

In blueprints there is a server function ApplyDamage. It is probably there in C++ too

more ▼

answered Dec 07 '18 at 06:14 PM

avatar image

mightyenigma
3.7k 17 18 14

(comments are locked)
10|2000 characters needed characters left
Your answer
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