Need help with Events. Please help me...

Options
I am new to PUN. I have spent lots of time on different demo, reading PDF, and online post. I still can't figure out how to use Events.

I am making a turn-based game, which after both players submit the action, the game will use the data to run the result. After finished, start next turn.

I think I don't need to use PhotonView here. However, is there any demo shows how to use RaiseEvents and OnEvent? Or is there any better way to send message in my type of game?

I really need a help or some guides here. Thank you very much!!!!!

Comments

  • dreamt1204
    Options
    After reading more post on this forum. I found out I could use PhotonView + RPC to achieve my goal, is it right?

    Give both Players PhotonView script then while they submit the action, use RPC to send out all the data he has to another player. However, is there any restriction using RPC, such as can only send serialized type as parameter? Can I use my own Type of variables to be parameter and send out by RPC?

    Sorry... being really confused here. I really like PUN and want to use it to finish my game. Thanks!!!

    Best Regard.
  • vadim
    Options
    Hi,

    You need at least one PhotonView to be able communicate between clients. To send your types in RPC calls, implement serialization for this type. See doc on custom type serialization http://doc.exitgames.com/en/realtime/cu ... -in-photon
  • dreamt1204
    Options
    Thank you so much, vadim!

    I have played around with photonview 2 days. I am still really confused about its concept.

    My game is a turn-based game, which doesn't require real time Object movement update. Thus, I create a "SyncManager" script, that manager all the PRC for the game, attach to a Game Object "Sync" in the scene. I literally place "Sync" in my scene. Now here comes the problem. I only have to put "Photon View " script to Sync, right?

    I tried this. However, it seems only my first connect client (Master Client?) have everything synced. What's going wrong here? Do I attach Photon View to the wrong object? Should every client initiate they own "Sync" object in order to have different view ID?

    Thank you!!
  • dreamt1204
    Options
    So I have tried to Instantiate a gameobject attached with "Photo View " and "SyncManager" script. However, only the Master Client get updated and received the data. Any idea why this happen? Thanks a lot!!!!!

    I have this script on a object in the scene:
    public void OnJoinedRoom()
    	{
    		GameObject SyncManagerObject = PhotonNetwork.Instantiate(syncManagerPrefab.name, transform.position, transform.rotation, 0);
    		GameControl.SyncManager = SyncManagerObject.GetComponent<SyncManager> ();
    
    		if (PhotonNetwork.player.ID == 1) 
    		{
    			GameControl.SyncManager.SetPlayerConnectionStatus(1,true);
    		}
    			
    		else if (PhotonNetwork.player.ID == 2)
    		{
    			GameControl.SyncManager.SetPlayerConnectionStatus(2,true);
    		}
    	}
    

    In SyncManager Class, I have this:
    	public void SetPlayerConnectionStatus(int playerIndex, bool connected)
    	{
    		Debug.Log (photonView.viewID);
    		photonView.RPC("RPCSetPlayerConnectionStatus", PhotonTargets.Others, playerIndex, connected);
    	}
    
    	[RPC]
    	void RPCSetPlayerConnectionStatus(int playerIndex, bool connected)
    	{
    		if (playerIndex == 1)
    		{
    			GameControl.isPlayer1Connected = connected;
    		}
    		else if (playerIndex == 2)
    		{
    			GameControl.isPlayer2Connected = connected;
    		}
    	}
    

    GameControl is the class that control the main logic of my game. Basically, I want to keep updating SyncManager to send PRC to update both clients' GameControl.
  • dreamt1204
    Options
    After more investigation, I found out that if I put the following RPC line in void Start() in SyncManager script, it works. When SyncManager is Instantiated, it start() event calls this and it is working.
    photonView.RPC("RPCSetPlayerConnectionStatus", PhotonTargets.Others, playerIndex, connected);
    

    However, if I put that RPC line into some public function in SyncManager script and called the function from other class, it is not working anymore (Only Master Client recieved data.)
    public void SetPlayerConnectionStatus(int playerIndex, bool connected)
    {
    	photonView.RPC("RPCSetPlayerConnectionStatus", PhotonTargets.Others, playerIndex, connected);
    }
    

    This is the way I call this function from another class already in the scene:
    public void OnJoinedRoom()
    {
    	// Instantiate syncManager
    	GameObject SyncManagerObject = PhotonNetwork.Instantiate(syncManagerPrefab.name, transform.position, transform.rotation, 0);
    
    	SyncManagerObject.GetComponent<SyncManager> ().SetPlayerConnectionStatus(1,true);
    }
    

    How can I call this function? Something I am missing here? Or is there a better way to solve this? I am still really confused about the concept of PhotonView and RPC. Which object or class should have them? Thanks for helping me!!!!
  • vadim
    Options
    1. RPC fired by 1st client does not work since 2nd client is not online yet.
    2. It's not clear what are you trying to do. But for sure comparing player.ID with concrete value is not good practice since you can't expect always 1 and 2 id's in the room - they assigned automatically. Handle OnPhotonPlayerConnected or check PhotonNetwork.playerList to trace connected players statuses.
    3. If for some reason you still want call RPCs while others not connected then use PhotonTargets.OthersBuffered in call - clients connected after call will get it from cache.
  • dreamt1204
    Options
    Omg.... Thank you so much, vadim!! I almost forgot the connection from player 2. Use PhotonTargets.OthersBuffered does solve my problem here. A lot thanks!!!

    Still have few questions here, if you can help me, I will appreciate a lot :)
    1) I just want the first joined player be player 1 and second joined as player 2. You said it's not good to use player.ID. If I use PhotonNetwork.playerList[0], will it always guarantee he is always the first connected in this game?

    2) Like I described above. Attach Photon View and SyncManager scripts to a gameobject. Then Instantiated two of these objects for both players. Then just use this SyncManager script to send RPC to both client to update other classes should work fine for my turn-based game, right? Do I still have to attach Photon View to any other object?

    Lots of Lots of thanks here!!
  • dreamt1204
    Options
    Oh, just another quick question I forgot to mention above:

    3) If I defined my own enum in my own player class like the following script:
    public enum _TurnAction{
    	Idle,
    	Skip,
    	Move,
    }
    
    Then, in the same player class, I have a type like this:
    	[HideInInspector] public _TurnAction TurnAction;
    

    While use RPC and tried to Serialize this type, I got error with the following codes:
    private static byte[] SerializePlayerTurnAction(object customobject)
    	{
    		_TurnAction ta = (_TurnAction)customobject;
    
    		byte[] bytes = new byte[4];
    		int index = 0;
    		Protocol.Serialize(ta, bytes, ref index);                     <--------- This failed. Because ta is not a short type
    		
    		return bytes;
    	}
    	
    	private static object DeserializePlayerTurnAction(byte[] bytes)
    	{
    		_TurnAction ta = new _TurnAction();
    		int index = 0;
    
    		Protocol.Deserialize(out ta, bytes, ref index);            <--------- This failed. Because ta is not a short type
    		return ta;
    	}
    

    Is there any easy way to send my enum or serialize it? I will appreciate a lot if you can show me a quick example based on my enum here. Thank you soooo much!! :)
  • vadim
    Options
    1. PhotonNetwork.playerList order is not guaranteed. Handle both OnJoinedRoom and OnPhotonPlayerConnected. 1st and 2nd players triggered one of those events are what are you looking for.
    Probably you can also rely on players id order. Greater one is for more recently joined player at least for current server realization.
    PhotonNetwork.masterClient property returns player with lowest id. This is one more option to get oldest player in the room (concerning disconnected players).
    2. Single PhotonView with RPCs is enough for game logic sync between clients. You do not need 2 objects per client. Instead of instantiating in realtime, create empty 'Scripts' object with PhotonView in editor and place it in scene (check PUN demos for 'Scripts' objects).
    3. Try cast enum to int on serialize and back on deserialize.
  • dreamt1204
    Options
    Thank you so much, vadim!! These solved all my problems. Thank you so much! :D