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;
};

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 :slight_smile: (for now)