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 SaintTippu · Jan 29, 2018 at 05:47 PM · photonmultiplayer-networkingturn-based

Photon Unity Turn based Multiplayer game

I am trying to create a simple 2D Turn based multiplayer game using Photon Unity Networking. It is just a simple turn based game where a player 1(host) presses his button and it adds his score and changes its turn to player 2(client) who presses his button to add score and change turn to player 1. It continues to infinite. I was able to connect two players in the game using the basic Photon documentation. Now I need to add the networking logic of taking turns and changing them. I searched the internet but I can't understand the RPC and SerializeView of Photon. I am really confused with that. Please Help me. Thank you in future. Here is my GameManager Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 public class GameManager : Photon.PunBehaviour
 {    
     public Text roomName;
     public Text player1Name,player2Name;
     public List<string> playersConnected = new List<string>();
     int scoreP1 = 0, scoreP2 = 0;
     public Text scoreTextP1, scoreTextP2;
     public Button p1Btn, p2Btn;
     int playerTurn = 1;
 
     void Start()
     {
         roomName.text = SceneManager.GetActiveScene().name;
         p2Btn.gameObject.SetActive(false);
         p1Btn.gameObject.SetActive(true);
 
     }    
 
     public void AddScoreP1()
     {
         scoreP1++;
         scoreTextP1.text = scoreP1.ToString();
         ChangePlayerTurn();
     }
 
 
     public void AddScoreP2()
     {
         scoreP2++;
         scoreTextP2.text = scoreP2.ToString();
         ChangePlayerTurn();
     }
 
     void ChangePlayerTurn()
     {
         if (playerTurn == 1)
         {
             playerTurn = 2;
             p2Btn.gameObject.SetActive(true);
             p1Btn.gameObject.SetActive(false);
         }
         else
         {
             playerTurn = 1;
             p1Btn.gameObject.SetActive(true);
             p2Btn.gameObject.SetActive(false);
         }
         print("Player Turn: P" + playerTurn);
     }
 
     void LoadArena()
     {
         if (!PhotonNetwork.isMasterClient)
         {
             Debug.LogError("PhotonNetwork : Trying to Load a level but we are not the master Client");
         }
         Debug.Log("PhotonNetwork : Loading Level : " + PhotonNetwork.room.PlayerCount);
         PhotonNetwork.LoadLevel("Room for " + PhotonNetwork.room.PlayerCount);
     }
 
     public override void OnLeftRoom()
     {
         SceneManager.LoadScene(0);
     }      
 
     public void LeaveRoom()
     {
         PhotonNetwork.LeaveRoom();
     }
 
     public override void OnPhotonPlayerConnected(PhotonPlayer other)
     {
         Debug.Log("OnPhotonPlayerConnected() " + other.NickName); // not seen if you're the player connecting
         foreach (PhotonPlayer _player in PhotonNetwork.playerList)
         {
             playersConnected.Add(other.NickName);
         }
 
         if (PhotonNetwork.isMasterClient)
         {
             Debug.Log("OnPhotonPlayerConnected isMasterClient " + PhotonNetwork.isMasterClient); // called before OnPhotonPlayerDisconnected
 
 
             LoadArena();
         }
     }
 
     public override void OnPhotonPlayerDisconnected(PhotonPlayer other)
     {
         Debug.Log("OnPhotonPlayerDisconnected() " + other.NickName); // seen when other disconnects
         foreach (PhotonPlayer _player in PhotonNetwork.playerList)
         {
             playersConnected.Remove(other.NickName);
         }
 
         if (PhotonNetwork.isMasterClient)
         {
             Debug.Log("OnPhotonPlayerDisonnected isMasterClient " + PhotonNetwork.isMasterClient); // called before OnPhotonPlayerDisconnected
             LoadArena();
         }
     }
 
 
 }
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 ChristianSimon · Feb 01, 2018 at 10:33 AM

Hi @SaintTippu,

there is a PunTurnManager in package you have downloaded from the Asset Store. I guess using this or taking at least a look at this will help you for the turn based behaviour.

RPCs are remote procedure calls which are invoked on one client to process a function on the same object on all remote clients. Therefore you have to use the RPC function on an attached PhotonView component and also have to register a function which is called. This function requires the [PunRPC] attribute. To see how RPCs are working I recommend you taking a look at the RPCs and RaiseEvent documentation page.

OnPhotonSerializeView only works if there are two or more players in the room. OnPhotonSerializeView provides a continuing data exchange between the clients and their objects and is called 20 times per second by default. To use this, you have to implement the function yourself and add the script as an observed component to the also attached PhotonView component. Since your game is turn based and not realtime, I guess you don't have to use OnPhotonSerializeView for now.

Comment
Add comment · 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

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

81 People are following this question.

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

Related Questions

What is needed for a mobile multiplayer game? Unity+ vs Photon+ 0 Answers

Photon OnPhotonPlayerDisconnected Works For Only Master Client 0 Answers

Android mobile local multiplayer game via Wifi 4 Answers

How to connect to our own dedicated server using photon networking in unity?(Self-hosted) 0 Answers

Photon Multiplayer not Syncing 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