How to recognize, is current client the owner of this actor into a OnRep_- method?

ActorGun is spawned dynamicly.
By SetOwningPawn(..) the MyPawn is set as owner.
On every shot I call method (on Server), which changes property of ActorGun, named LastShotInfo.
This prop. is declared such way:

UPROPERTY(BlueprintReadOnly, ReplicatedUsing = OnRep_LastShotInfo, Category = "Shot")
FLastShotInfo LastShotInfo;

FLastShotInfo here is the USTRUCT type name.

LastShotInfo inside AActorGun::GetLifetimeReplicatedProps(..) is added without any conditions:

DOREPLIFETIME(AActorGun, LastShotInfo);

So, I expect, method OnRep_LastShotInfo(..) will be called on every relevant (e.g. located <= some distance) client every time the LastShotInfo property is changed.

This method I’ve defined such way:

void AActorGun::OnRep_LastShotInfo()
{
        PlayShot(); //! plays cosmetic

        if (Role == ROLE_AutonomousProxy) 
        {
            OnShot.Broadcast(); //! signal to current client UI to redraw some effects.
        }
}

I want the OnShot.Broadcast() to be called only on owning client to redraw UI only for player, who have did this shot

BUT! this broadcast is never called =(

Every time, I fall inside OnRep_LastShotInfo(..), the Role is == ROLE_SimulatedProxy.

Why is it so?
How can I separate unique logic for owner client only inside the OnRep_LastShotInfo() method?

Try creating a method something like this,

bool AActorGun::IsLocallyControlled() const
{
	auto* Owner = Cast<APawn>(GetOwner());
	if (!Owner) return false;
	return Owner->IsLocallyControlled();
}

and then using it like this,

void AActorGun::OnRep_LastShotInfo()
{
     PlayShot(); //! plays cosmetic

     if (IsLocallyControlled()) 
     {
         OnShot.Broadcast(); //! signal to current client UI to redraw some effects.
     }
}

Thank you, cancel! It’s exactly, what I was looking for.

But I still wonder, why if (Role == ROLE_AutonomousProxy) doesn’t work in expected by me way (doesn’t do the same)?

No problem! Please mark the answer as accepted so that other people can find it if they search for a similar question.

An instance of the running game (a game client) will only have Role == ROLE_Authority (which is equivalent to HasAuthority()) on an Actor when it was locally spawned. A remote client possessing a replicated Actor that was spawned by the server does not give the remote client authority. It merely indicates that it is “controlled”, hence the IsLocallyControlled() call, which eventually reaches calls this code on the player controller: Controller->IsLocalController();

Oh, thanks, for reminding.

Hmm, it seems there is some weird flaw in the UE4 answerhub where questions go back to being unanswered if the original author or person who responds adds a comment again. I think you have to mark it as answered again after commenting. That seems like a serious flaw to me.

Ok, thanks!