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

devomage

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 62
  • Posts: 232
    • 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: 297
  • -Receive: 1136
  • Posts: 21,937
  • 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: 7
  • -Receive: 62
  • Posts: 232
    • 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: 297
  • -Receive: 1136
  • Posts: 21,937
  • 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: 7
  • -Receive: 62
  • Posts: 232
    • 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.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 297
  • -Receive: 1136
  • Posts: 21,937
  • Toronto, Canada
    • View Profile
Re: PlayerData + LoadFile problem
« Reply #5 on: February 24, 2017, 08:36:08 AM »
Why are you doing a LoadFile call? That's for manually loading a file from the server. Setting a player save will already load the player data for you -- and as I mentioned in the previous post after you make that call you should effectively exit the function and wait for a response to come back before doing anything else.

Inside your OnSetPlayerData callback is where you should be checking -- has the data been set already? If not, then set it (set your account ID etc).

devomage

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 62
  • Posts: 232
    • View Profile
Re: PlayerData + LoadFile problem
« Reply #6 on: February 24, 2017, 04:38:48 PM »
you contradicted yourself saying the data is available locally and then said i should wait for the data via the event.  yes, confusing.

i am not familiar with how LoadFile and SetPlayerSave works and is why i am posting this question.  an attempt on my part to fully embrace all of TNet's functionality...

i am now clear that i should set the data and wait for the event.

appreciate the clarity, thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 297
  • -Receive: 1136
  • Posts: 21,937
  • Toronto, Canada
    • View Profile
Re: PlayerData + LoadFile problem
« Reply #7 on: February 26, 2017, 11:51:45 AM »
LoadFile just requests the server to load the specified file and return its contents. This file is assumed to have been saved previously using SaveFile, although not necessarily so. I've used this in Windward in order to save custom player symbol textures for sails, server-side mods, audio clips, and more. Basically things that need to be accessed by all players when needed, rather than all the time as is the case with player profile data.