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"

Accessing owner of PlayerState on multiplayer client

I have a custom property on a PlayerState, "craftColor", that determines the color of the possessed pawn's static mesh. I have this property set to replicate with a ReplicatedUsing callback.

The problem is that when I receive the replicated callback on a client, I don't have an obvious way to tie the current PlayerState to any specific pawn. GetOwner() returns null. I understand that PlayerControllers don't exist on all clients, but I was hoping there would still be a way to go from PlayerState -> Possessed Pawn.

I may be able to use PlayerState->PlayerId to find the pawn, but at that point it feels like I'm fighting the system.

Am I using PlayerState incorrectly? It really seems like the correct place to put something like a player selected color. Should I move the "craftColor" variable to the Pawn, or is there some access method I'm failing to see?

Product Version: Not Selected
Tags:
more ▼

asked Jan 27 '15 at 06:10 PM in C++ Programming

avatar image

sinoth
77 13 13 12

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

2 answers: sort voted first

No, but what you could do is something like this, declared in your custom PlayerState header.

 HEADER
 -------------
 DECLARE_DELEGATE(FOnPlayerColorChange)
 
 class AMyPlayerState : APlayerState
 {
     GENERATED_BODY()
 public:
     UPROPERTY(ReplicatedUsing=OnRep_Color)
     FLinearColor PlayerColor;
     FOnPlayerColorChange OnColorChangeDelegate;
 
     UFUNCTION()
     void OnRep_Color();
 };
 
 
 SOURCE
 -------------
 void AMyPlayerState::OnRep_Color()
 {
     OnColorChangeDelegate.ExecuteIfBound();
 }

And then in your custom Pawn header.

 HEADER
 -------------
 class AMyPawn : APawn
 {
     GENERATED_BODY()
 public:
     virtual void PostInitializeComponents() override;
     void OnPlayerColorChange();    
 };
 
 
 SOURCE
 -------------
 void AMyPawn::PostInitializeComponents()
 {
     Super::PostInitializeComponents();
 
     AMyPlayerState* MyPlayerState = Cast<AMyPlayerState>(PlayerState);
     MyPlayerState->OnColorChangeDelegate.BindUObject(this, &AMyPawn::OnPlayerColorChange);
 }
 
 void AMyPawn::OnPlayerColorChange()
 {
     AMyPlayerState* MyPlayerState = Cast<AMyPlayerState>(PlayerState);
     FLinearColor NewColor = MyPlayerState->PlayerColor;
     //DO COLOR RELATED STUFF
 }


more ▼

answered Jan 28 '15 at 06:45 AM

avatar image

HailstoneRyan
694 38 23 53

avatar image sinoth Jan 28 '15 at 02:42 PM

Thanks for that example! It is my first time to see the DECLARE_DELEGATE syntax... seems handy!

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

Could you not also do this through the Pawn itself? The Pawn has a reference to the PlayerState so you could just watch for changes in the Pawn's tick.

If you wanted to do something Async you could setup a delegate that gets fired whenever you replicate the color variable.

more ▼

answered Jan 27 '15 at 08:50 PM

avatar image

HailstoneRyan
694 38 23 53

avatar image sinoth Jan 27 '15 at 09:18 PM

Yeah, color could live on the Pawn but it does make me wonder exactly what PlayerState is for. It seems awkward that the intent is to store per-player information, but there isn't a clear way for it to access the possessed pawn to apply any of this information.

avatar image HailstoneRyan Jan 28 '15 at 01:33 AM

Sorry, wasn't clear. I mean you have access to the player state inside the pawn, you don't need access to the pawn in the player state. Either setup a delegate inside the player state that triggers a function on the pawn when the color changes, or constantly check inside the pawns tick.

avatar image sinoth Jan 28 '15 at 06:33 AM

I'd rather not poll in tick if possible.

What you said about a delegate inside PlayerState that triggers a function on Pawn is what I'm requesting, if I'm understanding you correctly. The problem is you can't trigger anything on a Pawn that you know nothing about. I suppose I could do something like add a variable to PlayerState that points to the Pawn, and initialize it on initial replication... but that is starting to get convoluted.

After pondering it some more, I think the solution in this particular instance is to simply move the variable to Pawn and replicate it there. PlayerState seems better suited for global information you'd display on a scoreboard -- not information specific to the pawn.

(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