# C++ Replicating Animations

**URL:** <https://forums.unrealengine.com/t/c-replicating-animations/353112>\
**Category:** Character & Animation\
**Tags:** Replication, UE4-10, unreal-engine, CPP, question\
**Created:** [April 6, 2016, 5:39am UTC](https://forums.unrealengine.com/t/c-replicating-animations/353112 "2016-04-06T05:39:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vergbergler](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Vergbergler](https://forums.unrealengine.com/u/Vergbergler)\
**Post date:** [April 6, 2016, 5:39am UTC](https://forums.unrealengine.com/t/c-replicating-animations/353112/1 "2016-04-06T05:39:01Z")

</div>

What’s wrong:

- HOST can see the CLIENTS FireAnimation.

- CLIENTS can’t see the HOST’s FireAnimation.

- CLIENTS can’t see other CLIENTS FireAnimation.

What is needed:

What would need to be changed with the following code so that all CLIENTS and HOST can see any connected player’s FireAnimation? Is only a Replicated Multicast Function needed for the solution? What about only using a Replicated Server Function?

PLEASE NOTE:

bReplicates = true; // In player’s the constructor

I’ve already read this:

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

```
// ACharacter.h

public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay, Replicated)
  class UAnimMontage* FireAnimation;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = WeaponUsage, Replicated)
  class USoundBase* FireSound;

UFUNCTION(NetMulticast, Unreliable)
    void MulticastSetFiring();

UFUNCTION(Server, Reliable, WithValidation)
    void ServerSetFiring();

protected:
void OnFire();

// ACharacter.cpp

void ACharacter::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
  Super::GetLifetimeReplicatedProps(OutLifetimeProps);

  DOREPLIFETIME(ACharacter, FireAnimation);
  DOREPLIFETIME(ACharacter, FireSound);
}

void ACharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
  // set up gameplay key bindings
  check(InputComponent);

  InputComponent->BindAction("Fire", IE_Pressed, this, &ACharacter::OnFire);
}

// WIP - Only Server can see animations of clients.
void ACharacter::MulticastSetFiring_Implementation()
{
  // try and play the sound if specified
  if (FireSound != NULL)
  {
    UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
  }

  // try and play a firing animation if specified
  if (FireAnimation != NULL)
  {
    // Get the animation object for the arms mesh
    UAnimInstance* AnimInstance = Mesh1P->GetAnimInstance();
    if (AnimInstance != NULL)
    {
      AnimInstance->Montage_Play(FireAnimation, 1.f);
    }
  }
}

void ACharacter::ServerSetFiring_Implementation()
{
  // try and play the sound if specified
  if (FireSound != NULL)
  {
    UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
  }

  // try and play a firing animation if specified
  if (FireAnimation != NULL)
  {
    // Get the animation object for the arms mesh
    UAnimInstance* AnimInstance = Mesh1P->GetAnimInstance();
    if (AnimInstance != NULL)
    {
      AnimInstance->Montage_Play(FireAnimation, 1.f);
    }
  }
}

bool ACharacter::ServerSetFiring_Validate()
{
  return true;
}

void ACharacter::OnFire()
{
  if (Role < ROLE_Authority)
  {
    ServerSetFiring();
  }
  else
  {
    MulticastSetFiring();
  }
}

```

---

<div class="post-metadata">

**Author:** ![ryanjon2040](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ryanjon2040/32/906587_2.png) [@ryanjon2040](https://forums.unrealengine.com/u/ryanjon2040)\
**Post date:** [April 6, 2016, 5:53am UTC](https://forums.unrealengine.com/t/c-replicating-animations/353112/2 "2016-04-06T05:53:08Z")

</div>

I didnt test this code but can you please try this and report back?

```
// ACharacter.h
 
 public:
 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
 class UAnimMontage* FireAnimation;
 
 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = WeaponUsage)
 class USoundBase* FireSound;
 
 UFUNCTION(NetMulticast, Reliable)
 void MulticastSetFiring();
 
 UFUNCTION(Server, Reliable, WithValidation)
 void ServerSetFiring();
 
 UFUNCTION()
 void SetFiring();
 
 protected:
 void OnFire();
 
 // ACharacter.cpp
 
 void ACharacter::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
 {
   Super::GetLifetimeReplicatedProps(OutLifetimeProps);
 }
 
 void ACharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
 {
   // set up gameplay key bindings
   check(InputComponent);
 
   InputComponent->BindAction("Fire", IE_Pressed, this, &ACharacter::OnFire);
 }
 
 // WIP - Only Server can see animations of clients.
 void ACharacter::MulticastSetFiring_Implementation()
 {
    SetFiring();
 }
 
 void ACharacter::ServerSetFiring_Implementation()
 {
   MulticastSetFiring();
 }
 
 bool ACharacter::ServerSetFiring_Validate()
 {
   return true;
 }
 
 void ACharacter::OnFire()
 {
   if (HasAuthority())
   {
        SetFiring();
   }
   else
   {
        ServerSetFiring();
   }
 }
 
 void ACharacter::SetFiring()
 {
    // try and play the sound if specified
   if (FireSound != NULL)
   {
     UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
   }
 
   // try and play a firing animation if specified
   if (FireAnimation != NULL)
   {
     // Get the animation object for the arms mesh
     UAnimInstance* AnimInstance = Mesh1P->GetAnimInstance();
     if (AnimInstance != NULL)
     {
       AnimInstance->Montage_Play(FireAnimation, 1.f);
     }
   }
 }
```

---

<div class="post-metadata">

**Author:** ![Vergbergler](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Vergbergler](https://forums.unrealengine.com/u/Vergbergler)\
**Post date:** [April 6, 2016, 7:13am UTC](https://forums.unrealengine.com/t/c-replicating-animations/353112/3 "2016-04-06T07:13:27Z")

</div>

Hey ryanjon2040, thanks for the help!  
Here’s the difference after applying your suggestion:

- HOST _can_ see the CLIENTS FireAnimation.

- CLIENTS _can’t_ see the HOST’s FireAnimation.

- CLIENTS _can_ see other CLIENTS FireAnimation.

- Sound has doubled playing on all CLIENTS, but only once on HOST.

Any additional suggestions on how to get all three working?

---

<div class="post-metadata">

**Author:** ![ryanjon2040](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ryanjon2040/32/906587_2.png) [@ryanjon2040](https://forums.unrealengine.com/u/ryanjon2040)\
**Post date:** [April 6, 2016, 7:20am UTC](https://forums.unrealengine.com/t/c-replicating-animations/353112/4 "2016-04-06T07:20:23Z")

</div>

Try changing the SetFiring to Multicast under Authority branch

---

<div class="post-metadata">

**Author:** ![Vergbergler](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Vergbergler](https://forums.unrealengine.com/u/Vergbergler)\
**Post date:** [April 6, 2016, 7:27am UTC](https://forums.unrealengine.com/t/c-replicating-animations/353112/5 "2016-04-06T07:27:15Z")

</div>

Fantastic! The animations work correctly.

However, my CLIENTS are now playing the FireSound twice every shot.

Any thoughts?

---

<div class="post-metadata">

**Author:** ![Vergbergler](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Vergbergler](https://forums.unrealengine.com/u/Vergbergler)\
**Post date:** [April 6, 2016, 7:30am UTC](https://forums.unrealengine.com/t/c-replicating-animations/353112/6 "2016-04-06T07:30:50Z")

</div>

I figured out the double playing sound problem by moving the following into the OnFire function.

```
if (FireSound != NULL)
  {
    UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
  }

```

---

<div class="post-metadata">

**Author:** ![ryanjon2040](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ryanjon2040/32/906587_2.png) [@ryanjon2040](https://forums.unrealengine.com/u/ryanjon2040)\
**Post date:** [April 6, 2016, 7:32am UTC](https://forums.unrealengine.com/t/c-replicating-animations/353112/7 "2016-04-06T07:32:50Z")

</div>

Great to hear that OmicronVega. If the issue is resolved can you please mark my answer? This will help Epic track resolved answers. 🙂
