Best way for saving user info

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 :slight_smile:

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?

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.

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 :stuck_out_tongue: Still thanks for the first tip tho!

I just need like a tutorial or some good way to save it :slight_smile:

You should be able to use WWWForm like this: Unity - Scripting API: WWWForm

Then what you’ll need is an API that handles the server side communication, which is non Unity stuff (but if you want to keep it C# you can use WebApi: Get Started with ASP.NET Web API 2 (C#) - ASP.NET 4.x | Microsoft Learn

If you want to keep it Unity for most stuff, look into UNet: https://docs.unity3d.com/Manual/UNet.html

Keep in mind though that servers cost money :slight_smile:

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…

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.

So, it is possible to save by UNET itself, instead of creating any bypasses as said above (by me)

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

hmm well i never get those things before i havent seen any example code haha :slight_smile:

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

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:

[Serializable]
public class SaveableData
{
    // Saveable fields. You could also use public fields, but that goes against normal C# conventions.
    [SerializeField] private int score;
   
    public SaveableData(int score)
    {
        this.score = score;
    }
   
    public int Score { get { return score; } }
}

public class Level : MonoBehaviour
{
    [SerializeField] private string levelName;
    [SerializeField] private int levelScore;
   
    private void SaveLevel()
    {
        var data = new SaveableData(levelScore);
        DataManager.SaveData<SaveableData>(data, levelName + "_save");
    }
   
    private void LoadLevel()
    {
        var data = DataManager.LoadData<SaveableData>(levelName + "_save");
       
        if(data != null)
        {
            levelScore = data.Score;
        }
    }
}
1 Like