Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(3 edits) (+1)

Okay, so that was the responsible thing to say...now, let me teach you about a hack you can employ to solve your data saving problems.

So, Unity allows you to create a public field in your script, then lets you drag a prefab onto that field in the inspector and during runtime you instantiate that prefab and use it however you see fit... This is Unity 101 and everybody knows this.

What you might NOT know, though, is that you don't HAVE to instantiate that object before you use it. As long as you have a reference to it then you can use it directly.... this includes changing values in the prefab. the thing is, though, that if you didn't instantiate the prefab first then it means you are actually modifying the prefab that WILL be instantiated later but hose changes are forever. You cannot undo whatever changes you made...

...and here comes the interesting part...

...even between play sessions. Yup. Hit play, change any values you want, remove your hard drive and plug it into a separate machine, boot that up and start the game and still the data will be the values you changed them to. Copy the game to a flash drive and install it on your mate's PC and the first time he plays the game he will start with your new values. It would basically be as if you originally set those values when you were busy making the game in the first place.

Unfortunately, this is not the most helpful bit of advice because if you save player's settings this way then every single player will start the game with the settings of the last person who played the game. See? Useless... But since it sounds like this issue is related ONLY to people playing the itch.io hosted WebGL version and you are planning on moving the game to other platforms, well, maybe you might be prepared to take this drastic measure...

What about you create a class (THAT CAN NEVER CHANGE AFTER THE FIRST TIME YOU DO THIS!!!) that contains all the data you want to save for a player. I.e. score, the upgrade level of each weapon, the current ammo, world position, rotation, kill count, etc. All of it. Let's say you call that class PlayerData. Now simply create this script, create a prefab from it and make sure something points to it so you can access the prefab:

public class AllSavedData : MonoBehaviour {
    public Dictionary<string, PlayerData> data;
    public void SavePlayerData(string me, PlayerData mydata) { data[me] = mydata; }
    public PlayerData GetPlayerData(string me) { get { return data.ContainsKey(me) ? data[me] : null; } }
} 

Please note that I have not tested this but in theory this should work. This is incredibly bad coding and should only be used out of sheer desperation but, if I am correct with this then all you would need to do is find SOME way of uniquely identifying a user (PC GUID maybe?) and then you can store all their data as part of the game POST release. The biggest problem with this is that all player's data will load for every player and then you have to extract just the entry you want. This is why I say this is very bad code.

Edit: Come to think of it, I think this means that each time you update the game you will overwrite this prefab anyway so unless using Butler to only update changes somehow allows this to still work then good, I guess... but yeah, just know about this little bug / feature / hack and see if this can be of use to you. I highly advise against it but if you can't come up with a solution that works then what have you got to loose by trying, right?

The other, most safest way, is to save the data to your own server. A very basic and simple solution would be to write a super simple php script that does nothing but create a new file and save anything you sent it to that file. This way you can fetch back that file using the super simple:

string my_settings;
IEnumerator GetMySettings(string me){
WWW w = new WWW("http://mysite.com" + me + ".txt");
yield return w;
my_settings = string.IsNullOrEmpty(w.error) ? w.text : string.Empty;
}

So now you just have to worry about how to serialise PlayerData into a string and back again and hope and pray that someone doesn't use your script to upload code to your site (there are workarounds to prevent that so not that big of a deal) and there you have it. Save the data on your website as a small text file per user and you never have to worry about loosing any settings ever again.

Again, also a drastic move but just throwing some ideas out there.