Save variables to database or some other way?

Hello everyone! as the title sais i just want some feedback on a good way to save variables from a webgl game.So i m building a webgl game (simple hyper casual) and i want to save an email and the score at game end, so far i was building it through php and mysql but i was wondering if there is another way? Im new to unity but i do know web dev and sql so it was natural to go that way but it made me think if there was a “unity” way? like save the variables to a text that is saved in the server? just asking!

We do not currently have any APIs specific to saving data on a server database in Unity. There is an API to do HTTP POST requests (Unity - Scripting API: Networking.UnityWebRequest.Post), but that will still leave it up to the developer to actually develop the server side entry point and database backend.

One way to do that would be e.g. to set up a node.js+mongodb server that accepts in JSON-based POST requests, where you’d format an appropriate JSON object to contain the highscore data, and when the Node.js server receives the highscore, it saves it to MongoDB. Unity would then do a UnityWebRequest with the JSON in the POST data when a player finishes a game with a high score.

well i plan on writing a php file used in mysql. i have already written the php script and i dont want to parse any info from the database just to store it there and do queries in the database for viewing the data! Now i dont use wordpress but i think it uses mysql right? so by placing the php in the httpdocs folder should allow it to save that data? By making a canvas with input fields and sending the data there?

That could work. Sorry, I have not used wordpress, so not sure about that. Also I am afraid not much help on PHP either.

If you can develop a “pure JavaScript” way to achieve what you want, then doing the same from within Unity is possible, either via a UnityWebRequest, or by creating a .jslib file to call to the JS code.

1 Like