Riiich
September 12, 2021, 1:22pm
1
I understand that Mirror uses the authoritative server approach but I was wondering if there was any override for just simply calling a method on both the server and client, without caring who sent the command?
At the moment I use this kind of programming while running commands
[Command]
public void CmdOpenDoor()
{
RpcOpenDoor();
OpenDoor();
}
[ClientRpc]
private void RpcOpenDoor()
{
OpenDoor();
}
private void OpenDoor()
{
DoorScript.Open();
}
Is there anything like this so you can shorten code?
[GlobalRpc]
public void GlobalOpenDoor()
{
DoorScript.Open();
}
Riiich
September 13, 2021, 8:07am
2
You can bypass server authority by using requiresAuthority=false on the Command attribute: Remote Actions - Mirror
Example usage:
public enum DoorState : byte
{
Open, Closed, Locked
}
public class Door : NetworkBehaviour
{
[SyncVar]
public DoorState doorState;
[Client]
void OnMouseUpAsButton()
{
CmdSetDoorState();
}
[Command(requiresAuthority = false)]
public void CmdSetDoorState(NetworkConnectionToClient sender = null)
{
bool hasDoorKey = sender.identity.GetComponent<PlayerState>().hasDoorKey;
if (doorState == DoorState.Open)
{
doorState = hasDoorKey ? DoorState.Locked : DoorState.Closed;
return;
}
if (doorState == DoorState.Locked && hasDoorKey)
{
doorState = DoorState.Open;
return;
}
if (doorState == DoorState.Closed)
doorState = DoorState.Open;
}
}
Edit:
If you’re using a version of Mirror before 8th March 2021 the attribute is called ignoreAuthority
source: Change Log - Mirror
[Command(ignoreAuthority = false)]
public void CmdSetDoorState(NetworkConnectionToClient sender = null)
{
// Do command magic
}