Author Topic: Assigning GUI avatars to players in a channel  (Read 2795 times)

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 46
    • View Profile
Assigning GUI avatars to players in a channel
« on: November 23, 2016, 09:26:01 PM »
I have a scene where there is a channel limit of six players. The attached GUI shows 6 'slots' where I want to implement content based on each of the six players in the channel....

So...
Player1 ---> Slot1
Player2 ---> Slot2
Player1 ---> Slot3
and so on....

How would one go about assigning these players (lets say...each playerID) their respective GUI objects.

Is there an example here in the forums that can guide me in the right direction?

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Assigning GUI avatars to players in a channel
« Reply #1 on: November 24, 2016, 06:15:00 AM »
- player1 is the client/host
- code logic in onPlayerJoin/onPlayerLeave to deal with players as they come and go

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: Assigning GUI avatars to players in a channel
« Reply #2 on: November 24, 2016, 08:57:07 AM »
What I'm asking is how would I identify the order in which players enter the channel so I can then assign them to the slots. Im confused as to how to get each playerId by the order in which they joined.

eg.
player1 (host) is first to join so -> slot1
player2 joined is second to join so-> slot2
player3 joined is third to join so-> slot3
and so on.....

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Assigning GUI avatars to players in a channel
« Reply #3 on: November 24, 2016, 05:37:42 PM »
TNManager.playerID is incremented internally when a new player *connects* to the server.

The order players join depends on when each client hits TNManager.JoinChannel and in turn fires the onPlayerJoin event.  Keep in mind that onPlayerJoin does not fire for the client/host, only other players.  Use onJoinChannel to detect when the client/host joins the channel.

How you assign each player depends on your game logic.  For example, if all 6 slots fill up and then slot 4 disconnects - either shift players around so the next join is player 6 or simply replace slot 4.

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: Assigning GUI avatars to players in a channel
« Reply #4 on: November 24, 2016, 07:30:10 PM »
TNManager.playerID is incremented internally when a new player *connects* to the server.

The order players join depends on when each client hits TNManager.JoinChannel and in turn fires the onPlayerJoin event.  Keep in mind that onPlayerJoin does not fire for the client/host, only other players.  Use onJoinChannel to detect when the client/host joins the channel.

How you assign each player depends on your game logic.  For example, if all 6 slots fill up and then slot 4 disconnects - either shift players around so the next join is player 6 or simply replace slot 4.

Ah... TNManager.JoinChannel may be what I have to use to get the order in which the players join...Ill try it out ...

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Assigning GUI avatars to players in a channel
« Reply #5 on: November 24, 2016, 10:08:19 PM »
this template should get your started in the right direction.

you should go through NeilM's tutorials...  as well as the examples that come with TNet.

http://www.tasharen.com/forum/index.php?topic=13953.0


  1. using TNet;
  2. using UnityEngine;
  3.  
  4. public class TNetDemo : MonoBehaviour
  5. {
  6.     public string address = "192.168.1.106";
  7.     public int port = 5127;
  8.  
  9.     private void OnEnable()
  10.     {
  11.         TNManager.onConnect += OnConnect;
  12.         TNManager.onDisconnect += OnDisconnect;
  13.         TNManager.onJoinChannel += OnJoinChannel;
  14.         TNManager.onPlayerJoin += OnPlayerJoin;
  15.         TNManager.onPlayerLeave += OnPlayerLeave;
  16.     }
  17.  
  18.     private void OnDisable()
  19.     {
  20.         TNManager.onConnect -= OnConnect;
  21.         TNManager.onDisconnect -= OnDisconnect;
  22.         TNManager.onJoinChannel -= OnJoinChannel;
  23.         TNManager.onPlayerJoin -= OnPlayerJoin;
  24.         TNManager.onPlayerLeave -= OnPlayerLeave;
  25.     }
  26.    
  27.     [ContextMenu("Connect")]
  28.     public void OnClick_Connect()
  29.     {
  30.         //onConnect event
  31.         TNManager.Connect(address, port);
  32.     }
  33.  
  34.     [ContextMenu("Join Channel")]
  35.     public void JoinChannel()
  36.     {
  37.         //onJoinChannel event for host
  38.         //onPlayerJoin event for others
  39.         TNManager.JoinChannel(1000, null, false, 6, null, false);
  40.     }
  41.  
  42.     #region TNet Events
  43.  
  44.     private void OnConnect(bool success, string message)
  45.     {
  46.         //TNManager.playerID is now valid
  47.         if (success) JoinChannel();
  48.     }
  49.  
  50.     private void OnDisconnect()
  51.     {
  52.         //
  53.     }
  54.  
  55.     private void OnJoinChannel(int channelID, bool success, string message)
  56.     {
  57.         if (success)
  58.         {
  59.             //deal with player 1
  60.         }
  61.     }
  62.    
  63.     private void OnPlayerJoin(int channelID, Player p)
  64.     {
  65.         //logic for players 2 - 6
  66.     }
  67.  
  68.     private void OnPlayerLeave(int channelID, Player p)
  69.     {
  70.         //logic for players 2 - 6
  71.     }
  72.    
  73.     #endregion
  74. }
  75.  

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Assigning GUI avatars to players in a channel
« Reply #6 on: November 24, 2016, 10:27:20 PM »
seems i cant edit my post...

edit: 

there is only one host and the host should be doing most or all of the logic...  in both OnJoinChannel and OnPlayerJoin