Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Best way for saving user info

Discussion in 'Scripting' started by luckie12, Nov 7, 2016.

  1. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Hi, im working on a small game that has to be multiplayer. The thing i want to know is, what and how is the best way to save data on the server?

    What it should do is like: Every player that logs in should get his referencing money, guns etc inventory items. But i dont know the way to do it...

    I tried WWWForm to load stuff, but i think thats not the safest way and i didnt get it to work. Using Data prefs is easy to change by the player itself.

    How can i save the playerdata to the server i am using and load it by user login?

    Thanks for any help :)
     
  2. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    If the game has to be multiplayer saving some user information should not be that hard. It always comes with stuff for that.
    What are you using for multiplayer/matchmaking or logging in?
     
  3. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    289
    The best (albeit not easiest) way would be a server running a database. Unity can communicate with this database through http requests (GET, POST, PUT mainly). At my last workplace we used the BestHTTP plugin for one project (Unity frontend), can't give you any details on the server backend, though, since that was done by a colleague of mine.
     
  4. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    I am using WWW and WWWForm for login and registering. I couldnt find any good resource about WWWForm and item/stats saving of an player.

    As a starter, its expensive :p Still thanks for the first tip tho!

    I just need like a tutorial or some good way to save it :)
     
  5. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
  6. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    In what case? We have around 50 servers available to host it on haha

    But i thought about saving the XML to the user file and then let it upload and delete again, but i think thats not the best way as people gonna abuse the way of saving the XML file and/or editing it...
     
  7. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    If you want to save it local I wouldn't save it as XML no, rather save it as binary data as most users won't be able or bother to manipulate that. Then you can just use load that data in memory and persist it to the server using WWWForm.
     
  8. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    So, it is possible to save by UNET itself, instead of creating any bypasses as said above (by me)
     
  9. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Well, UNET uses UnityWebRequest which in turn uses WWWForm if I look at it's scripting reference, so no need to create a basic handler youself as far as that is concerned: https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.html
     
  10. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    hmm well i never get those things before i havent seen any example code haha :)

    I do try to understand it but almost never will understand it haha
    and all my google searches didnt resolve anything on it haha
     
  11. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Well, the link I posted above shows how to use WWWFrom and UnityWebRequest so you should be able to figure that out.

    As for saving and loading Binary data you could use the Generic Data manager from my signature: https://github.com/deMD/Unity-Data-Manager/blob/master/UnityDataManager/DataManager.cs

    Sample usage:
    Code (CSharp):
    1. [Serializable]
    2. public class SaveableData
    3. {
    4.     // Saveable fields. You could also use public fields, but that goes against normal C# conventions.
    5.     [SerializeField] private int score;
    6.    
    7.     public SaveableData(int score)
    8.     {
    9.         this.score = score;
    10.     }
    11.    
    12.     public int Score { get { return score; } }
    13. }
    14.  
    15. public class Level : MonoBehaviour
    16. {
    17.     [SerializeField] private string levelName;
    18.     [SerializeField] private int levelScore;
    19.    
    20.     private void SaveLevel()
    21.     {
    22.         var data = new SaveableData(levelScore);
    23.         DataManager.SaveData<SaveableData>(data, levelName + "_save");
    24.     }
    25.    
    26.     private void LoadLevel()
    27.     {
    28.         var data = DataManager.LoadData<SaveableData>(levelName + "_save");
    29.        
    30.         if(data != null)
    31.         {
    32.             levelScore = data.Score;
    33.         }
    34.     }
    35. }
     
    Defero likes this.