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"

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?

Product Version: UE 4.8
Tags:
more ▼

asked Aug 11 '15 at 02:52 PM in C++ Programming

avatar image

Sashka
93 15 23 23

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

1 answer: sort voted first

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.
      }
 }
more ▼

answered Aug 12 '15 at 10:22 AM

avatar image

cancel
2.4k 101 72 123

avatar image Sashka Aug 12 '15 at 02:38 PM

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

avatar image cancel Aug 12 '15 at 02:46 PM

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

avatar image Sashka Aug 12 '15 at 03:22 PM

Oh, thanks, for reminding.

avatar image cancel Aug 12 '15 at 03:25 PM

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.

avatar image Sashka Aug 13 '15 at 09:44 AM

Ok, thanks!

avatar image Sashka Aug 12 '15 at 02:45 PM

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

avatar image cancel Aug 12 '15 at 03:11 PM

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();

(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