Author Topic: PlayerData + LoadFile problem  (Read 60 times)

devomage

  • Full Member
  • ***
  • Thank You
  • -Given: 5
  • -Receive: 58
  • Posts: 213
    • View Profile
PlayerData + LoadFile problem
« on: February 17, 2017, 03:56:11 AM »
When a player successfully authenticates I use SetPlayerData to store the info.  The info is not available if you use the following code example.


  1. internal static void SUCCESS(int user_id, string username)
  2. {
  3.         //LoginProgress.ToggleCredentials();
  4.        
  5.         TNManager.SetPlayerData("account/phpbb_id", user_id);
  6.         TNManager.SetPlayerData("account/phpbb_accountname", username);
  7.  
  8.         TNManager.LoadFile("path/" + username + ".txt", LoadFile);
  9. }
  10.  
  11. private static void LoadFile(string filename, byte[] data)
  12. {
  13.         //Debug.Log(TNManager.playerData.ToString());
  14.  
  15.  
  16.         //these 2 vars were set previously, but are not available here
  17.         int phpbb_id = TNManager.GetPlayerData<int>("account/phpbb_id");
  18.         string phpbb_accountname = TNManager.GetPlayerData<string>("account/phpbb_accountname");
  19.        
  20. }
  21.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 254
  • -Receive: 1112
  • Posts: 21,707
  • Toronto, Canada
    • View Profile
Re: PlayerData + LoadFile problem
« Reply #1 on: February 18, 2017, 08:06:58 PM »
I don't see why it wouldn't be available. When you set player data, you fire off a packet to the server, requesting for the data to be changed -- but the change is instantaneous on the client you called it (meaning the set value can be immediately retrieved via get). You could call your LoadFile callback right away and it would have the data available.

The only reason I see this not working is if the player data gets overwritten soon after your LoadFile call and before the callback is executed. This will happen if you've set a save file. TNet automatically loads a previously saved player file if you've called TNManager.SetPlayerSave.

devomage

  • Full Member
  • ***
  • Thank You
  • -Given: 5
  • -Receive: 58
  • Posts: 213
    • View Profile
Re: PlayerData + LoadFile problem
« Reply #2 on: February 19, 2017, 04:21:12 PM »
here is the complete snipet.  the server auth success:

i am not sure what is causing the vars to not be available...

  1. int user_id = reader.ReadInt32();
  2. string username = reader.ReadString();
  3.  
  4. TNManager.playerName = username;
  5.  
  6. TNManager.SetPlayerSave("path/" + username + ".txt", DataNode.SaveType.Text);
  7.  
  8. TNManager.SetAlias(user_id.ToString());
  9. TNManager.SetAlias(username);
  10. TNManager.SetAlias(SystemInfo.deviceUniqueIdentifier);
  11.  
  12. SUCCESS(user_id, username);
  13.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 254
  • -Receive: 1112
  • Posts: 21,707
  • Toronto, Canada
    • View Profile
Re: PlayerData + LoadFile problem
« Reply #3 on: February 21, 2017, 12:55:03 PM »
You need to wait for the player data to arrive before doing any setting.
  1. int user_id = reader.ReadInt32();
  2. string username = reader.ReadString();
  3.  
  4. TNManager.playerName = username;
  5.  
  6. TNManager.onSetPlayerData += delegate (Player p, string path, DataNode node)
  7. {
  8.     TNManager.SetAlias(user_id.ToString());
  9.     TNManager.SetAlias(username);
  10.     TNManager.SetAlias(SystemInfo.deviceUniqueIdentifier);
  11. };
  12.  
  13. TNManager.SetPlayerSave("path/" + username + ".txt", DataNode.SaveType.Text);
Note that using an anonymous delegate here is just an example. You will want to unsubscribe from it after receiving your callback, so ideally you wouldn't use an anonymous delegate here.

devomage

  • Full Member
  • ***
  • Thank You
  • -Given: 5
  • -Receive: 58
  • Posts: 213
    • View Profile
Re: PlayerData + LoadFile problem
« Reply #4 on: February 21, 2017, 05:53:49 PM »
i made a demo script with a fresh install of TNet to show the problem:

i would embrace a better solution, but this seems to be the logical flow for my purposes...

specifically, data is being set before a "LoadFile" and is not available when the LoadFile is called.  either i am overlooking something obvious, or something is wrong.

  1. using TNet;
  2. using UnityEngine;
  3.  
  4. public class demo : MonoBehaviour
  5. {
  6.     private string address = "192.168.1.107";
  7.     private int port = 10420;
  8.     private string playersavepath = "path/";
  9.  
  10.     private void OnEnable()
  11.     {
  12.         TNManager.onConnect += OnConnect;
  13.         TNManager.onSetPlayerData += OnSetPlayerData;
  14.     }
  15.  
  16.     private void OnDisable()
  17.     {
  18.         TNManager.onConnect -= OnConnect;
  19.         TNManager.onSetPlayerData -= OnSetPlayerData;
  20.     }
  21.  
  22.     [ContextMenu("Connect")]
  23.     private void Connect()
  24.     {
  25.         //simulate successful remote server auth via OnConnect
  26.         TNManager.Connect(address, port);
  27.     }
  28.  
  29.     private void OnConnect(bool success, string message)
  30.     {
  31.         Debug.Log(string.Format("success={0} message={1}", success, message));
  32.  
  33.         if (success)
  34.         {
  35.             //return vars from auth success
  36.             int user_id = 1;//reader.ReadInt32();
  37.             string username = "Ziggy";//reader.ReadString();
  38.  
  39.             TNManager.playerName = username;
  40.  
  41.             TNManager.SetPlayerSave(playersavepath + username + ".txt", DataNode.SaveType.Text);
  42.  
  43.             SUCCESS(user_id, username);
  44.         }
  45.     }
  46.  
  47.     private void OnSetPlayerData(Player p, string path, DataNode node)
  48.     {
  49.         Debug.Log(string.Format("p.name={0} path={1}", p.name, path));
  50.     }
  51.  
  52.     private void SUCCESS(int user_id, string username)
  53.     {
  54.         //here is the problem
  55.         //data is set here, but is not available in the "LoadFile" method
  56.         TNManager.SetPlayerData("account/user_id", user_id);
  57.         TNManager.SetPlayerData("account/username", username);
  58.        
  59.         TNManager.LoadFile(playersavepath + username + ".txt", LoadFile);
  60.     }
  61.  
  62.     private void LoadFile(string filename, byte[] data)
  63.     {
  64.         int user_id = TNManager.GetPlayerData<int>("account/user_id");
  65.         string username = TNManager.GetPlayerData<string>("account/username");
  66.  
  67.         Debug.Log(user_id);//returns 0
  68.         Debug.Log(username);//returns null
  69.         Debug.Log(TNManager.playerData.ToString());
  70.        
  71.         //playerData debug.log:
  72.         //doesnt seem to be available on the client
  73.  
  74.         //Version = 20160822
  75.         //Server
  76.         //lastSave = 63623313874361
  77.  
  78.     }
  79. }
  80.