Client variable update for all clients (dedicated server)

I did this part , but with your example , I want change the variable on a client , then the variable be update in all clients how can I do ?

void AMyClass::SetExampleBoolean(bool newBoolean)
{
bExampleBoolean = newBoolean;
if ( Role < ROLE_Authority)
{
ServerSetBoolean(bExampleBoolean);
}

}

void AMyClass::ServerSetBoolean_Implementation(bool newBoolean)
{
SetExampleBoolean(bool newBoolean);
}

Thats what I try with UFUNCTION(Server, WithValidation), but dont work for the rest of the clients.

I’ll preface this by saying doing it this way, depending on the context, can be very dangerous. The server shouldn’t trust information coming from the client ever.

A function like you have above will work in conjunction with replicating the variable. The client can update it on their side and then send the RPC to the server which will update the variable on the server and trigger replication of the new value.

Thanks for you help.
So it’s not really possible to pass de replicate variable like :

client → to server then after update the variable , server → to clients ?

If its not possible I have to get all clients and update the variable with the server ?

The way you have it set up will work just fine mixed with replication.

Client updates their copy of the variable and notifies the server via RPC. The server then notifies all clients of the change using replication.

If possible can you explain me with an example how i pass that step with the server notifies all clients ? Thats the part where Im blocked.

Thanks for you patience.

By replicating the variable that you’ve declared in that class.

HEADER

UPROPERTY(Replicated)
 bool bExampleBoolean;

UFUNCTION(Server, WithValidation)
void ServerSetExampleBoolean(bool bInExampleBoolean);

CPP

 void AMyClass::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
 {
     Super::GetLifetimeReplicatedProps(OutLifetimeProps);
 
     DOREPLIFETIME(AMyClass, bExampleBoolean);
 }


void AMyClass::SetExampleBoolean(bool bInExampleBoolean) {
   bExampleBoolean = newBoolean;
   if ( Role < ROLE_Authority)
   {
      ServerSetBoolean(bExampleBoolean);
   }
}

void AMyClass::ServerSetBoolean_Implementation(bool newBoolean)
{
   SetExampleBoolean(bool newBoolean);
}

HEADER

UPROPERTY(Replicated)
 bool bExampleBoolean;

void SetExampleBoolean(bInExampleBoolean);

UFUNCTION(Server, WithValidation)
void ServerSetExampleBoolean(bInExampleBoolean);

CPP

void AMyClass::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const
 {
     Super::GetLifetimeReplicatedProps(OutLifetimeProps);
 
     DOREPLIFETIME(AMyClass, bExampleBoolean);
 }

void AMyClass::SetExampleBoolean(bool InbExampleBoolean)
{
    bExampleBoolean = InbExampleBoolean;
    if ( Role < ROLE_Authority)
    {
        ServerSetBoolean(bExampleBoolean);
    }
}

void AMyClass::ServerSetBoolean_Implementation(bool InbExampleBoolean)
{
    SetExampleBoolean(InbExampleBoolean);
}

Ok thanks , but I dont understand how you said in code the notif about the variable updates for all clients.

Said me if Im wrong , but I understand in this code , the client change her replicate variable , and send to the server with the Role_Autority.

You dont need a while or a function for said all clients have to update her variable ?

hello mates ,

Just for know is it possible in a dedicated server to have a client with a replicate variable be update by the server and get update for all clients ?

I try to used UFUNCTION(Server // NetMulticast)

but its not really working.

I will appreciate some helps if possible , thank you.

Variables marked as Replicated function this way.

UPROPERTY(Replicated)
bool bExampleBoolean;

You will also need to make sure you include UnrealNetwork.h in your CPP file and define this function in your CPP file.

void AMyClass::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);

	DOREPLIFETIME(AMyClass, bExampleBoolean);
}

No, because the variable is set as replicated the engine handles notifying the clients.