- Home /
Using Mirror Networking, is there a "Run Everywhere" attribute?
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();
}
Answer by Riiich · Sep 13, 2021 at 08:07 AM
You can bypass server authority by using requiresAuthority=false
on the Command
attribute: https://mirror-networking.gitbook.io/docs/guides/communications/remote-actions#bypassing-authority
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: https://mirror-networking.gitbook.io/docs/general/changelog#version-32-1-4-2021-mar-08
[Command(ignoreAuthority = false)]
public void CmdSetDoorState(NetworkConnectionToClient sender = null)
{
// Do command magic
}
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Stop accepting new player connections in Unity Mirror Networking 1 Answer
Reconnecting a player in an online game... 0 Answers
How to make a host-client server? 0 Answers