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"

Change visual state on received event in multiplayer

Hello,

let me describe the context:

I have a multiplayer sports game, with a ball. This ball declares an event, when it's owner changes.

Each character in the game owns a DecalComponent, which renders a colored circle on the floor. I created an instance material, so I can dynamically change the color of this circle, depending if the player has the ball in his hands or not. In the BeginPlay event, I make the character register to the event on the ball, and this is where the modification of the color of the decal happens.

In every case, the owner changed event is always correctly broadcated to every character in the game.

But:

  • in a local game, the colors of the decal under the characters are correctly updated

  • in a multiplayer game when one client hosts the game, only the hosting client has updated colors

  • in a multiplayer game with a dedicated server, none of the clients have updated colors

Again, when I put a breakpoint in visual studio in ABBCharacter::OnBallOwnerChanged, it is triggered for every client, and I can see the code is actually correctly called. It's just that visually, the decals still have the same color.

You can find below all the relevant code used for this case.

Thanks in advance

 UCLASS()
 class ABBBall : public AActor
 {
     DECLARE_EVENT_OneParam( ABBBall, FBallOwnerChangedEvent, const ABBCharacter & )
     FBallOwnerChangedEvent & OnBallOwnerChanged() { return OwnerChangedEvent; }

     FBallOwnerChangedEvent OwnerChangedEvent;

     void SetOwner(
         const class ABBCharacter & owner
         )
     {
         OwnerChangedEvent.Broadcast( owner );
     }
 }



 UCLASS(config=Game)
 class ABBCharacter : public ACharacter
 {
     GENERATED_UCLASS_BODY()

 public:

     UPROPERTY( Category = Default, VisibleAnywhere )
     TSubobjectPtr< class UDecalComponent > DecalComponent;
         
     UPROPERTY()
     class UMaterialInstanceDynamic* DecalMaterial;

 protected:

     virtual void BeginPlay() OVERRIDE
     {
         Ball = GetWorld()->GetGameState< ABBGameState >()->Ball;

         check( Ball.IsValid() );
         
         Ball->OnBallOwnerChanged().AddUObject( this, &ABBCharacter::OnBallOwnerChanged );
     }

     virtual void PostInitializeComponents() OVERRIDE
     {
         Super::PostInitializeComponents();

         if ( GetNetMode() != NM_DedicatedServer )
         {
             UMaterialInterface
                 * material_interface = DecalComponent->DecalMaterial;

             check( material_interface != NULL );

             DecalMaterial = UMaterialInstanceDynamic::Create( material_interface, this );
             DecalMaterial->SetVectorParameterValue( FName( TEXT( "BaseColorParameter" ) ), FLinearColor( 0.0, 1.0, 0.0, 1.0 ) );

             DecalComponent->SetDecalMaterial( DecalMaterial );
         }
     }

 private:

     void OnBallOwnerChanged( const ABBCharacter & ball_owner )
     {
         if ( GetNetMode() != NM_DedicatedServer )
         {
             if ( &ball_owner == this )
             {
                 DecalMaterial->SetVectorParameterValue( FName( TEXT( "BaseColorParameter" ) ), FLinearColor( 1.0, 0.0, 0.0, 1.0 ) );
             }
             else
             {
                 DecalMaterial->SetVectorParameterValue( FName( TEXT( "BaseColorParameter" ) ), FLinearColor( 0.0, 0.0, 1.0, 1.0 ) );
             }
         }
     }

     TWeakObjectPtr< class ABBBall > Ball;
 };




Product Version: Not Selected
Tags:
more ▼

asked Apr 06 '14 at 09:36 AM in C++ Programming

avatar image

Zoubi
167 21 30 35

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

1 answer: sort voted first

I found the error . If I have 2 players connected to a dedicated server, I only got the breakpoint 2 times, on the server.

What I did then was to use a boolean ItHasBall, which is replicated, and which I set in the OnBallOwnerChanged function of ABBCharacter.

And now it works :) (for now)

more ▼

answered Apr 06 '14 at 07:25 PM

avatar image

Zoubi
167 21 30 35

(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