Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by madhusudan1997-sharma · Feb 13, 2017 at 12:11 PM · unity 5multiplayergoogle play gamesturn-based

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.!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nadzrinp · Jun 25, 2018 at 01:06 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges