Author Topic: Relative player number  (Read 2302 times)

rxmarcus

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 62
    • View Profile
Relative player number
« on: August 27, 2017, 03:43:20 PM »
In my game, there are 4 players in a match. While players that join have their PlayerID that TNet assigns them, I also need to keep track of a relative player number in regards to being player 1 - 4. (This number is used by an announcer in the game, and for organizing player UI, etc.)

Currently I've been tracking this myself in my GameManager as players leave and join the server, but it has become a big pain as I have to keep this data synced with players that are already on the server, and then players that later join. Also for example, player 2 could leave, and then I would want the next player to join the server to be assigned player 2 for their relative player number. (so I can't just sort by tnets playerID to determine relative 1 - 4 player numbers)

I'm just now learning more about TNet's SetPlayerData method and others, and am hoping I can get some guidance on what the best approach using these methods might be to easily sync this data across all players at all times? I thought as well about modifying tnet's source code to include a relativePlayerID, but am not sure how i'd do that....

any thoughts are appreciated

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Relative player number
« Reply #1 on: August 27, 2017, 04:05:23 PM »
If all your players are in the same channel (for example global chat channel), you can just do a channel.players.size to get the number of other players there (doesn't include you). The lowest player number in that list connected first. I am not quite sure why you need this, but if you want to assign a specific "slot" to a player, in your Join Channel notification callback simply get the list of players in the channel and check what IDs were already taken (GetPlayerData). Pick one that isn't, then SetPlayerData to claim it.

Personally I'd do it via an RFC though -- to remove any chance of race conditions one player (channel's host) should be responsible for sending players their slot ID. So using the same example above, instead of each player doing it in the join channel notification, have them listen to other players joining, and if TNManager.IsHosting(channelID), tno.Send an RFC to the newly joined player with their slot ID.

rxmarcus

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 62
    • View Profile
Re: Relative player number
« Reply #2 on: August 27, 2017, 05:57:11 PM »
Thanks, that is helpful.

Now that I'm using SetPlayerData...... how am I able to get other players data? TNManager.GetPlayerData only returns OUR player data. How am I able to get other players saved data?

rxmarcus

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 62
    • View Profile
Re: Relative player number
« Reply #3 on: August 27, 2017, 06:06:59 PM »
Oops, Right after I posted I found another forum post explaining that you can use player.Get to get saved data.

Now that I'm saving player data using TNet, do you have any best practices for how I might handle running my game when it's local multiplayer (offline) using the same code?
Previously I was storing my 4 local player's data in normal C# lists, but how can I handle 4 local players when TNet operates on just having 1 player for online?

Hopefully it makes sense what I'm asking....


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Relative player number
« Reply #4 on: August 27, 2017, 08:38:40 PM »
Online or offline, it works exactly the same. When playing "offline" you should still put up an internal game server using TNServerInstance.Start() (no parameters).