- Home /
Turn Based Multiplayer using Google Play Services
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.!
Answer by nadzrinp · Jun 24, 2018 at 05:09 PM
If you look at your logical. Turn Based does not invoke from Listener as with RealTimeMultiplayer. You need to invoke OnSendingData to the server, or just take turn after you create quick match, then you can see there are participants were moving if he/she enter the game.
Take a look at the code here: Since there is no Listener from the TurnBased.Initialize, You need to manually retrieve everything from server and send back to server:
Create dummy move after $$anonymous$$atch Created:
public void $$anonymous$$atch$$anonymous$$akingButtonClick()
{
PlayGamesPlatform.Instance.TurnBased.CreateQuick$$anonymous$$atch(1, 1, 0, On$$anonymous$$atchStarted);
}
void On$$anonymous$$atchStarted(bool success, TurnBased$$anonymous$$atch match)
{
if (success)
{
matchedplayer = match;
bool canPlay = (match.Status == TurnBased$$anonymous$$atch.$$anonymous$$atchStatus.Active);
if(canPlay)
Scene$$anonymous$$anager.LoadScene("gameplay");
PlayGamesPlatform.Instance.TurnBased.TakeTurn(matchedplayer,
match.data,null, (bool success) => { });
}
else
{
Scene$$anonymous$$anager.LoadScene("menu");
}
}
Receiving data:
void onTurnBased$$anonymous$$atchedReceived()
{
//Your match in the code is obsolete. Update your math from server
PlayGamesPlatform.Instance.TurnBased.GetAll$$anonymous$$atches(ip => {
for (int i = 0; i <= ip.Length-1; i++)
{
if (ip[i].$$anonymous$$atchId == matchedplayer.$$anonymous$$atchId)
{
matchedplayer = ip[i];
}
}
});
datatext.GetComponent<Text>().text = Encoding.ASCII.GetString(matchedplayer.Data);
}
Your answer
Follow this Question
Related Questions
Is this possible to merge Unet with Google Play Games multiplayer? 0 Answers
Issue with GPGS TurnBased.CreateQuickMatch() in unity 0 Answers
What to do when user clicks back on google play games realtime multiplayer auto-match ui ? 0 Answers
Setting up invisibility for a turn-based JRPG style multiplayer game 0 Answers
Multiplayer Respawning Problems 0 Answers