Skip to content

Instantly share code, notes, and snippets.

@SocketWeaver
Last active June 13, 2019 20:10
Show Gist options
  • Save SocketWeaver/640e1b3bbafa459195c89bde5bb10b14 to your computer and use it in GitHub Desktop.
Save SocketWeaver/640e1b3bbafa459195c89bde5bb10b14 to your computer and use it in GitHub Desktop.
Handle PlayersPressedEnter's event
RemotePropertyAgent remotePropertyAgent;
const string PLAYER_PRESSED_ENTER = "PlayersPressedEnter";
private void Start()
{
guiManager.SetLapRecord(lap_, lapsToWin);
// get a reference of the gameDataManager component
remotePropertyAgent = GetComponent<RemotePropertyAgent>();
}
void Update()
{
if (State == GameState.waiting)
{
if (Input.GetKeyUp(KeyCode.Return))
{
// start the countdown
Debug.Log("Starting...");
State = GameState.starting;
// Modify the PlayersPressedEnter sync property.
int playerPressedEnter = remotePropertyAgent.GetPropertyWithName(PLAYER_PRESSED_ENTER).GetIntValue();
remotePropertyAgent.Modify(PLAYER_PRESSED_ENTER, playerPressedEnter + 1);
}
}
}
// GameDataManager events
public void OnPlayersPressedEnterValueChanged()
{
int playerPressedEnter = remotePropertyAgent.GetPropertyWithName(PLAYER_PRESSED_ENTER).GetIntValue();
// check if all players has pressed Enter
if(playerPressedEnter == 2)
{
// start the countdown
InvokeRepeating("Countdown", 0.0f, 1.0f);
countdown_ = countdown;
}
}
public void OnPlayersPressedEnterValueReady()
{
int playerPressedEnter = remotePropertyAgent.GetPropertyWithName(PLAYER_PRESSED_ENTER).GetIntValue();
// check if all players has pressed Enter
if (playerPressedEnter == 2)
{
// the player probably got disconnected from the room
// If all players has pressed the Enter key, the game has started already.
State = GameState.started;
Debug.Log("Started");
}
}
public void OnPlayersPressedEnterValueConflict(SWSyncConflict conflict, SWSyncedProperty property)
{
// If players pressed the Key at the same time, we might get conflict
// The game server will receive two requests to change the PlayersPressEnter value from 0 to 1
// The game server will accept the first request and change PlayersPressEnter value to 1
// The second request will fail and player who sent the second request will get a confict
int remotePlayerPressed = (int)conflict.remoteValue;
// Add 1 to the remote PlayerPressedEnter value to resolve the conflict.
int resolvedPlayerPressed = remotePlayerPressed + 1;
property.Resolve(resolvedPlayerPressed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment