Download

Checking IslocallyControlled in a multicast not always working

This question is also asked here Checking IslocallyControlled in a multicast not always working - UE4 AnswerHub

In order to check to make sure certain sounds are not being played for the client whos calling them i usually do a multicast rpc which gets called from a server rpc. in the multicast i usually do the following.



if(IsLocallyControlled())
{
return;
}
else 
{

// code here for all but client who called the server rpc

}


for some reason this does not always work even if the rpcs are set up the same way is there another way of doing this so that things will get done for all but the client who called the rpc to start with?

my ufunctions tags look like this



UFUNCTION(Server, Unreliable, WithValidation, BlueprintCallable)

UFUNCTION(NetMulticast, Unreliable)


if anyone has any way of doing this that works all the time that would be great thanks.

I usually do this when trying to avoid Multicast playing twice locally:

bool CheckIfCanPlayMulticast()
{
!(UKismetSystemLibrary::IsServer(this) && !CharacterRef->IsLocallyControlled();
}

For instant feedback, I play the sound locally (even inside the Server call) AND call the multicast, then use this check to avoid playing again.

My code is basically this:


void PlaySound(FName RowName)
{
    if (GetOwnerRole() < ROLE_Authority)
    {
        Server_PlaySound(RowName);
    }
    else
    {
        Multicast_PlaySound(RowName);
    }

    Internal_PlaySound(RowName);
}



void Server_PlaySound_Implementation(FName RowName)
{
    Multicast_PlaySound(RowName);
    Internal_PlaySound(RowName);
}


void Multicast_PlaySound_Implementation(FName RowName)
{
   if (CheckIfCanPlayMulticast())
   {
        Internal_PlaySound(RowName);
   }
}

In my case, I use a component that gets data from a DataTable, thus I use RowName…

if you want anyone to play but the owner, skip the Internal_PlaySound inside the PlaySound and Server_PlaySound