Hello everyone, I want to make a 2 players turn based multiplayer game using google play services but don’t know how to achieve that. I got successful in logging the user in from his/her play account. Now the main problem is matchmaking and sending and receiving the turns made by the user. Following are my code which I’m currently using.
Code for matchmaking:
public static TurnBasedMatch matchedplayer;
public void MatchMakingButtonClick()
{
PlayGamesPlatform.Instance.TurnBased.CreateQuickMatch(1, 1, 0, OnMatchStarted);
}
void OnMatchStarted(bool success, TurnBasedMatch match)
{
if (success)
{
matchedplayer = match;
bool canPlay = (match.Status == TurnBasedMatch.MatchStatus.Active);
if(canPlay)
SceneManager.LoadScene("gameplay");
}
else
{
SceneManager.LoadScene("menu");
}
}
Code for sending data:
public void onSendDataButtonClick()
{
// your representation of the new state of the game after the player
// has taken their turn:
string data = Random.Range(1, 100).ToString();
byte[] myData = Encoding.ASCII.GetBytes(data);
// this indicates whose turn is next (a participant ID)
string whoIsNext = nextParticipantId(matchedplayer);
PlayGamesPlatform.Instance.TurnBased.TakeTurn(matchedplayer, myData, whoIsNext, (bool success) => {
if (success)
{
// turn successfully submitted!
}
else
{
// show error
}
});
}
string nextParticipantId(TurnBasedMatch match)
{
// who is my opponent?
foreach (Participant p in match.Participants)
{
if (!p.ParticipantId.Equals(match.SelfParticipantId))
{
return p.ParticipantId;
}
}
return null;
}
void onTurnBasedMatchedReceived()
{
datatext.GetComponent<Text>().text = Encoding.ASCII.GetString(matchedplayer.Data);
}
Please please please help me…!! I’m getting frustrated now because I cant find a single solution for this problem. Please help. Thank you.!