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"

HitTesting on server followed by SpawnEmitter on each client

I just started using Networking in Unreal for the first time and have a hard time figuring out how to Spawn my beam Effect so every client can see it.

 void Fire();

 UFUNCTION(Server, Reliable, WithValidation)
     void ServerFire();
     void ServerFire_Implementation();
     bool ServerFire_Validate();
 
 UFUNCTION()
     void OnRep_PlayBeamChanged();
 
 UPROPERTY(ReplicatedUsing = OnRep_PlayBeamChanged)
     bool bPlayBeamEffect;

my cpp code looks like this

 void AInstantHitWeapon::Fire(){
     //HitTestCode
         bPlayBeamEffect = true;
         OnRep_PlayBeamChanged(); //I saw a post saying that in c++ you need to call it, as it only calls automatically in Blueprint.
         if (Role < ROLE_Authority){
           ServerFire();
     }
 }
 void AInstantHitWeapon::ServerFire_Implementation(){
     FireWeapon();
 }
 bool AInstantHitWeapon::ServerFire_Validate(){
     return true;
 }
 void AInstantHitWeapon::OnRep_PlayBeamChanged() {
     //Emit beam effect. Only show on the client calling Fire();
 }

Can someone explain me what i am doing wrong ? Thanks!

Product Version: UE 4.8
Tags:
more ▼

asked Aug 10 '15 at 08:01 AM in C++ Programming

avatar image

platernity
0 1 2

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

2 answers: sort voted first

I found the solution. The Weapon Actor was fine all along. The problem is spawning this Actor and setting its owner for replication as its spawned by an other replicated actor.

Here is how to Spawn an Actor inside an other one:

     UPROPERTY(Replicated)
     AActor* weapon;

     void EquipWeapon();
     UFUNCTION(Server, Reliable, WithValidation)
     void ServerEquipWeapon();
     void ServerEquipWeapon_Implementation();
     bool ServerEquipWeapon_Validate();

cpp code:

 void APlayerCharacter::EquipWeapon() {
     if (Role < ROLE_Authority){
         ServerEquipWeapon();
     }
     else{
         weapon = GetWorld()->SpawnActor<AInstantHitWeapon>(GetActorLocation(), FRotator::ZeroRotator);
         Cast<AInstantHitWeapon>(weapon)->OwningCharacter = this;
         weapon->SetOwner(this);
         weapon ->bReplicates = true;
     }
 }
 void APlayerCharacter::ServerEquipWeapon_Implementation() {
     EquipWeapon();
 }
 bool APlayerCharacter::ServerEquipWeapon_Validate() {
     return true;
 }
 
 void APlayerCharacter::BeginPlay()
 {
     Super::BeginPlay();
     
     EquipWeapon();
 }


more ▼

answered Aug 12 '15 at 09:46 AM

avatar image

platernity
0 1 2

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

That is only calling the function on the local client that fired the weapon. You need another function to do it. Add another UFunction set to be called on the client (ClientPlayBeam() for instance). Then in the ServerFire() call that function for each client you want it replicated to (non-owning clients).

more ▼

answered Aug 10 '15 at 08:56 PM

avatar image

joeGraf STAFF
1.5k 35 12 69

avatar image platernity Aug 10 '15 at 10:00 PM

call that function for each client you want it replicated to (non-owning clients)

How do I achieve that? Is there a way to get all the clients and iterate through? Or is it something else? Just making a client function and calling it from server didn't help. NetMulticast didn't work either

avatar image joeGraf STAFF Aug 11 '15 at 05:17 PM

I wanted to pull an example up but I don't have the ShooterGame sample on my machine. Download that and take a look at how the replicate fire effects. Typically, that's not done on the weapon, but the controller. Anyway there should be a good example in ShooterGame

avatar image platernity Aug 12 '15 at 08:17 AM

I copied the whole code of the ShooterGame AShooterWeapon_Instant class, and i have the same result as with my code. It only shows the trail on the client that fired it. Could it be my Weapon Spawning code? Or Project settings ?

Edit: Actually when i deactivate dedicated server, the player who host the game can fire and it shows on other clients. Other clients don't.

Edit: Debugging shows that ServerNotifyHit(..) is called when i fire, but ServerNotifyHit_Implementation(..) is not being run through.

avatar image Fluppi393 Aug 12 '15 at 09:05 AM

Have you defined the GetLifetimeReplicatedProps function? For your code to work it should look like this (don't forget to include UnrealNetwork.h):

 void AClass::GetLifetimeReplicatedProps(TArray<FLifetimeProperty> & OutLifetimeProps) const
 {
     Super::GetLifetimeReplicatedProps(OutLifetimeProps);
 
     DOREPLIFETIME_CONDITION(AClass, bPlayBeamEffect, COND_SkipOwner);
 }
(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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