Author Topic: player.data - what, where, and when?  (Read 6699 times)

Lumos

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 4
  • Posts: 21
    • View Profile
player.data - what, where, and when?
« on: December 14, 2014, 01:26:02 PM »
I'm not entirely certain what the player.data is best used for. The file it's defined in mentions that it gets synchronised on the server, but in one thread on here, Aren mentions that it's entirely local and does not get synced. Which one of them is it? (I did check all references to it briefly, and I saw it being written and parsed in a couple of places. Heh.)

Also, if we assume that player.data doesn't get synced, what's the best way for me to keep track of stuff like players' teams and score and whatnot?

creativitysquare

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 41
    • View Profile
Re: player.data - what, where, and when?
« Reply #1 on: December 14, 2014, 05:44:25 PM »
Hi Lumos

While you wait for Aren to answer I tell you what I do. This may help you with keeping track of scores by storing the score of every player in the player class and broadcasting it to all players.

In the game i have i need to get two players to compete against each other and i therefore need to:
1) Know the score of the other player
2) Know the nickname set by the other player
3) Know the position, the name and other parameters of the other player

I solve 1 and 2 by adding the values into the class responsible for the player which I can NDPlayer.
I find my object by calling NDPlayer.instance and I set properties that I can synch over the network (set;get) with and RFC
Example of score:
  1.         private string NeonDanceScoreBackProp;
  2.  
  3.         public string NeonDanceScore{
  4.                
  5.                 set{
  6.                        
  7.                         tno.Send("OnNeonDanceScore", TNet.Target.OthersSaved, value);
  8.                 }
  9.                
  10.                 get{
  11.                         return this.NeonDanceScoreBackProp;
  12.                 }
  13.         }
  14.  
  15.         [RFC]
  16.         void OnNeonDanceScore(string score){
  17.                 NeonDanceScoreBackProp = score;
  18.         }
  19.  
You can do the same for his nickname and anything else. You can store there his TNet ID if you want.
So by calling "NDPlayer.instance.NeonDanceScore" I set the score of my player (copy the score into this class value when you call to update the score) and deliver it to all other players.

Now. The problem is to find the other player so that I can read the values that he is setting into his NDPlayer.

I do this. I set a gameObject that I call Player2 as global var - this will be the opponent. And i load Player2 like this:

  1.                 NDPlayer[] ndPs = FindObjectsOfType(typeof(NDPlayer)) as NDPlayer[];
  2.                 Debug.Log ("Total Players NDPlayers: "+ndPs.Length);
  3.                 foreach (NDPlayer pl in ndPs) {
  4.  
  5.                         Debug.Log ("P2 is "+pl.gameObject.name);
  6.                         Debug.Log ("Is this mine? "+pl.IsMine());
  7.                         // assign P2 as the one that is not mine - created by me
  8.                         if(!pl.IsMine())
  9.                                 Player2 = pl.gameObject;
  10.                 }
  11.  

I look for all objects of type NDPlayer and I load with Player2 the one that is not mine. Clearly.

To determine if is mine, in the NDPlayer class in Awake() I load the property IsMine like this

  1. void Awake () {
  2.  
  3.                 isMine = TNManager.isThisMyObject;
  4. }
  5.  

Then i have a method that i can call on the instance to know if that gameObject was created by me:

  1. /// <summary>
  2.  
  3.         /// Determines whether this instance is this mine.
  4.         /// </summary>
  5.         /// <returns><c>true</c> if this instance is this mine; otherwise, <c>false</c>.</returns>
  6.         public bool IsMine(){
  7.  
  8.                 return isMine;
  9.         }
  10.  

So once you find the Player2 you have all the other information about the player (my point 3 above) like rotation, position etc.  and you can show the correct information to both players.

As long as you have two players only in the contest you can make all the decision by using the TNManager.isHosting. if you have more players you have to make a different design.

Hope this helps a little.

Cheers
Egi

Lumos

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 4
  • Posts: 21
    • View Profile
Re: player.data - what, where, and when?
« Reply #2 on: December 14, 2014, 10:47:28 PM »
Thanks for your input, but I'm familiar with the RFCs (as the whole networking, including guns and players and whatnot, is working, and I'm now moving onto the "other stuff"), but was kinda hoping to avoid using RFCs and to piggyback on top of the already built-in data thing. But we'll see; hopefully Aren, or someone who's experimented with this, will shed some light on what the data's actually meant to be used for.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: player.data - what, where, and when?
« Reply #3 on: December 15, 2014, 01:12:48 PM »
Player data used to be local, but I made it auto-sync'd a while back.

Best way to use it is to use TNManager.playerDataNode instead -- any changes you make to that DataNode structure can be sent to everyone else via TNManager.SyncPlayerData().

In Windward I use it to store the player itself -- equipment, inventory, talents, etc. I simply do stuff like
  1. TNManager.playerDataNode.GetChild("Inventory", true).SetChild("Crew", crewItemObject);
  2. TNManager.SyncPlayerData();
This way every player has access to each other player's data at all times. I also save this data locally separately by simply saving the TNManager.playerDataNode.Write("My Player"); from time to time.

Lumos

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 4
  • Posts: 21
    • View Profile
Re: player.data - what, where, and when?
« Reply #4 on: December 15, 2014, 06:24:16 PM »
Thanks, Aren, this is a good idea. It'll also save me the implementation of some sort of a tree later on. I think I'm going to go with this... By the way, does player data get redistributed to newly-joined players automatically, or should I handle this myself?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: player.data - what, where, and when?
« Reply #5 on: December 17, 2014, 03:38:13 PM »
Fully automatic.