Ready Check and Lobby Ready
See the API reference for more details ReadCheck LobbyReady
Setup
- Attach the
LobbyReady
component to your network manager or other non-networked object. - Attach the
ReadyCheck
component to your player's lobby object. - Ensure the
ReadyCheck
component is configured to synchronize itsIsReady
state from the owner client to the server, and from the server to all observers. This is typically handled by theSyncVar
attributes on theIsReady
field within theReadyCheck
script, which ensures the value is set on the owner client and reset on the server whenLobbyReady.SetAllClientsNotReady()
is called.
Usage
Setting Player Ready
To set a player as ready, you can simply update the IsReady
field of their ReadyCheck
component to true. This can be done either manually through code, or through user input such as a "Ready" button. Mirage will then sync this change to server and other clients. For example:
readyCheck.SetReady(true);
Reacting to Ready changes
When the IsReady
field of a player's ReadyCheck
component is changed, the OnReadyChanged
event is invoked on all clients to reflect the new value. You can subscribe to this event and perform actions based on the player's ready state. For example, you can update UI elements to show the player's current ready status:
public class ReadyUI : MonoBehaviour
{
public ReadyCheck ReadyCheck;
public Image Image;
public void Start()
{
ReadyCheck.OnReadyChanged += OnReadyChanged;
// invoke right away to set the current value
OnReadyChanged(ReadyCheck.IsReady);
}
private void OnReadyChanged(bool ready)
{
Image.color = ready ? Color.green : Color.red;
}
}
Sending Messages to Ready Players
To send a message to all players that are ready, you can use the LobbyReady.SendToReady
function. Here's an example:
[NetworkMessage]
// Make sure to regieter message on client
public struct MyMessage
{
public string message;
}
public class LobbyController : MonoBehaviour
{
public LobbyReady LobbyReady;
public void SendToReady()
{
var myMessage = new MyMessage { message = "Hello, world!" };
// Send message to ready players
LobbyReady.SendToReady(myMessage);
}
}
You can also send messages to not ready players by setting the sendToReady
parameter to false. Note that this function only sends messages to players that have ReadyCheck
attached to their character and are synced with the server.
public void SendToNotReady()
{
var myMessage = new MyMessage { message = "Hello, world!" };
// Send message to ready players
LobbyReady.SendToReady(myMessage, sendToReady: false);
}
Resetting Ready
Resetting Ready State for All Players
You can reset the IsReady
field for all players by calling LobbyReady.SetAllClientsNotReady()
. Here's an example:
public void ClearReady()
{
LobbyReady.SetAllClientsNotReady();
}
This will set the IsReady
field to false
for all ReadyCheck
on the server, the values will then be synced to client.