- Home /
Accomplishments in each Level not Saving
Say you make a game where in each level there are 3 things to do, aside from finishing the level. for example: One is to finish the level without being hit, the other to collect an item, and the last to finish the level in a certain amount of time.
You get badges for these accomplishments, and see them displayed at the end of that particular level. The next level offers the same tasks (minor tweaks in the editor and inspector. That part is already set up).
How does one complete the 3 tasks on level 1, and only see what was completed on that level, and then progress to the second level where he or she only sees what was accomplished in the second level? It is essential that player be able to leave level one, and return to see what has and hasn't been accomplished. Currently, they are not saved.
My researched has pointed fingers at XML, arrayPrefs2, GetInstanceID, DontDestroyOnLoad, and many other things. All of which I'm not particularly sure of how to tackle for my intentions, but could very well be the key to getting it done. How would you?
Answer by KittyKatKat · Apr 12, 2014 at 06:35 PM
I'd probably save it in a specific PlayerPref file by doing something like (this is just an example to give you an idea):
PlayerPrefs.SetFloat("Level " + Application.loadedLevel.ToString(), pointsAcquired);
Then, when you want to load those points, just do:
PlayerPrefs.GetFloat("Level //That level number//");
You might do some extra research about player prefs on the wiki. (I'm really no expert my self!)
On the other hand, one thing worth noting is that PlayerPrefs is not really a 'secure' code. It's easily editable by the player if he knows what he's doing. But if that's just a single player game with no online-score-sharing thing, players are only going to ruin their own fun by doing so.
I hope I at least somewhat helped :)
Thanks for the response! Couple questions:
I have a variable set up to tell the script which level I am on (cleverly titled: "thisLevel"). When you write '"Level " + ', are you referring to the level the player is on? And can I subsequently replace that with my "thisLevel" variable?
(or is "application.loadedlevel" essentially my "thisLevel" variable?)
does anything go in the parenthesis of "loadedlevel.ToString()"?
and pointsAcquired is I know just your example of what to have the script save in, but my $$anonymous$$i tasks are set up with booleans. Either they have them or they don't. How would you go about setting that up? $$anonymous$$eep in $$anonymous$$d, if the player gets 2 accomplishments, it could be the first and second, the second and third, or the first and third. So a numerical set up wouldn't work.
thanks again!
It takes out the index of the level when you preform the command Application.loadedLevel (to find out what the indexes are, go to File -> Build Settings... And on the right side below 'Scenes in build'. ToString simply puts that number as an ASCII/string. You can make a variable which could contain the level you're playing simply by doing (I'm using C#, I don't know how much that suits you, but I think JS is similar)
private int thisLevel = Application.loadedLevel;
pointsAcquired is just some variable I made up that contains a float (I'm assu$$anonymous$$g you're points are a variable of the 'float' type. You can store integers as well with SetInt and so on. Also you don't have to put anything as a parameter of the ToString() command, it converts the number 'behind' it automatically.
I just tried setting a player pref for a boolean to test it out, but couldn't find it. You could however convert a boolean into an intiger, like:
if (someBool)
PlayerPrefs.SetInt("someInt", 1);
else
PlayerPrefs.SetInt("someInt", 0);
And take that integer later and perhaps turn it back into a bool. Like (this all just comes out of my head):
if(PlayerPrefs.GetInt("someInt"))
someBool = true;
else
someBool = false;
(the reason why I didn't have to do PlayerPrefs.GetInt("someInt") == 1 is simply because, C (I'm not sure about C#, you might have to test that out, but I'm very sure it's the same) language takes every value other than 0 as true. so it will automatically take 1 as true and 0 as false.)
As for multiple booleans, you could just raize the potential value of the integer (someInt could be any whole number - 0, 1, 100, 3141 etc.)
I hope it at least somewhat makes it more clear.
it worked!! You are amazing!! THAN$$anonymous$$ YOU SO $$anonymous$$UCH!!! It took some finagling, but the basis of what you shared here saved me thousands of lines of code!
Answer by nicolasjr · Apr 12, 2014 at 06:37 PM
Well, I think that you should take a look on the arrayPrefs2. That's, in my opinion, the best way to handle your demand. You have no alternative, it seems, if not keep the badges accomplishments recorded, so, I'd definitively go with that approach.
is there any info you can give me to nudge me in the right direction? I looked at the wiki but it's filled with such broad info, I find it hard to know what to try.
Your answer
Follow this Question
Related Questions
Set int to object from list? 0 Answers
Saving character selection 1 Answer
PlayerPrefs for FPS? 1 Answer
Enemy saving multiple defeats 1 Answer
Level exporting (with video example) 1 Answer