playMaker

Author Topic: Set PlayerPref and Get PlayerPref not working with store variable~!  (Read 8496 times)

san

  • Junior Playmaker
  • **
  • Posts: 93
Hi, everyone so i have a question with how to save data or store data in one scenes and carry to another scenes….

so i setup the first scene has set int value to 0 - this will do 0 whenever level 1 start ~!
Same level - i set player pref and int variable…
Same level - number of scores has add into same variable by 1 and covert into string and gui-text shows score…so lets says… level 1 has 20 scores in the end of the level 1
Same level - those add int 1 variable add into set player pref int variable ~!
level finish ~! go to menu…. in the menu page i set up Get playerpref int with same string and same int variable…
and covert those int variable into string and set textgui using that string… to see the score…


So the problem is… in the first level i get XXXamount of scores number and put all those add int value number into set playerpref…. variable
but the set playerpref int variable having problem with not carrying store data into menu scenes.

so i am wondering because i set int value to 0 in the begining…? maybe because every time level is finish set int value = 0 … is that why the data is not carrying over to menu page?

so i did another test without using set int value = 0 from level 1.
what happened is… the int add value are adding to score… and i still have my set playerpref and int variable…
let says the game is end with score 15… - menu page pop up…
In the menu page, i just put gut-text with same int value variable number. scores are shows up in the menu page..
but the problem is… i have not use Get playerpref int …..
I only use gut-text with same converted string from level one every time int value add is adding number.

So in menu page.. when i use Get player pref int and those int value convert into string and set gui-text using that string… is showing nothing…
is it because level are loading and playerpref variable data lost by opening another scenes ?

And i am using all global variable…

please help.

Oh and one more thing.... unity ads still not working ~! lol. i want to release this test game but i am having this get and set playerpref issue. if anyone can help please do.  thanks

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Hi,
Playerprefs get/set seems to work fine on my end.

Did you check if the key name on both actions are the same?

Quote
And i am using all global variable…
This is actually a bad thing, try to minimize using globals.
Globals are easy to hack and get messy the more variables you get.

Groo Gadgets

  • Beta Group
  • Full Member
  • *
  • Posts: 175
Heya,

The playerprefs actions work fine, I think the problem is how you are implementing them into your game.

First thing I would suggest is picking up an asset that lets you view all playerprefs in your project. I personally use EasySave since you can extend the data you save into arrays. This asset might be overkill for your project so if you just want to view the playerprefs in your project then Advanced Playerprefs Window might be for you: https://www.assetstore.unity3d.com/en/#!/content/7070

Next, you should experiment in a new scene to get playerprefs working the way you want. If you have only one high score per level this should be easy. If you have more than one score per level then you should consider using EasySave since that asset lets you save arrays. You can, of course, create an array variable and convert it to a string to save into playerprefs.

You need to create an FSM that compares the existing score in your playerprefs with the score that the player just achieved in your level. If the score is lower then do nothing, if the score is higher then set the new score to the playerprefs value.

Does this make sense? I agree with djaydino, you should not use globals when saving and loading scores.

Cheers,

Simon

san

  • Junior Playmaker
  • **
  • Posts: 93
Hi Djaydino, Thanks for relpy ~! i didn’t set up Pref-key before ~! But since you reply i have been testing with Pref-key and still having an issue.

The problem is now Pref-Key-False_event > set playerPref always active whenever the game is restart or play again. and not going into a True_event > get playerPref > convert int to string and set gui text. (is not doing that)
Every time the game restart, the pref has key is going into False event and set a PlayerPref.

I did clearly wrote all global variable clearly so that wasn’t an issue.. Just setting and getting pref is problem.

So this is what i did, on main menu page - just one button to click play the game and text score on top .
- Start state to playpref has key - give two event state (true event and false event) true event goes - Get Pref / false event goes - set Pref and i put 0 on variable.
- In the Get Pref, I gave a int and string variable > finished > i have convert string and set gui text to see the score.

In the game - i have set up a Prefab box that pass by right to left screen so one box pass 1 int add value… with trigger enter.
-Eveytime box enter i send an event to another empty gameManager that has adding int value and converting int to string to see the score in the game.
-So in that game manager i set up like this again > Start State to PlayerPref has key - give two event (true and false event)
- true event goes to Get PlayerPref > finished > convert int to string
- False event goes to Set PlayerPref > finished > convert int to string, this state event finish go to (listener state) that is receiving event from box trigger > go to int add value state and i loop back into convert int to string state again.

So when the player die go back to main menu page and so i keep play for twice and the PlayerPref has key is going False event > Set playPref state… not going into Get playerPref state to see if previous score is higher or lower… not doing int compare.

I had this problem past 4 years ago and i couldn’t pass this get and set pref level even i was able to manage setting all level and Ai expect getting and setting data, i really do need help. Can you do video tutorial for this?

Thanks

san

  • Junior Playmaker
  • **
  • Posts: 93
First image is intro page.
Please take a look~! i think i am doing it wrong, please see which part i need to fix.
so everytime the game is start its going into set_pref. Not going to Get_pref.

Groo Gadgets

  • Beta Group
  • Full Member
  • *
  • Posts: 175
Hey San,

I think the problem you're having is the way you are using playerprefs.

The playerprefs key should be the name of the playerpref you want to use, in your case it should be something like: highScore

The value of the highScore playerpref should be the int of the high score.

I can see you are setting the int of your playerpref to 0 in the start state, if you do this it will overwrite the high score to 0 everytime you play the game. What you need to do is check if the key exists in your start state. If it does then move to the next state, if it doesn't then you need to create the highScore playerpref.

At game over you should get the playerpref value and compare it to the current score value. If the current score is lower than the high score do nothing, if it's higher then set the current score as the new high score.

As I suggested in an earlier post you should get yourself an asset that lets you view all the playerprefs in your project, this is something Unity should let you do without the need of an asset but what can you do  :-)

I've attached a simple scene that shows how this works, let me know if you it helps!

Cheers,

Simon

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Hi,
I agree with Groo Gadgets to get an asset for save/load as playerprefs is more suited for saving/loading prefferences (like key bindings, volume settings and other ingame settings)
And i would suggest 'easy save'

The G_S_Current_Score_Pref, does it has a value inside?

A good tip :
below, on the left side there is a check box called Debug.
Turn that on and you can see whats inside the variables on the used actions.

san

  • Junior Playmaker
  • **
  • Posts: 93
Hi, Groo Gadgets - that is true ~! every time i want to play the game i want to set my int value number to 0.
Now the game score is working fine now. But i still have an issue lol… I don’t exactly know why or how is working fine now LOL
So Groo I take a look at your scenes they are perfectly clear and very good example ~! and i rebuild all state and variable similar as your. This part i understood how int compare go different state and set playerpref stuff..

But the issue is in the game state - I know i want to start out zero score every time i play.

so i put int value to 0 > finish > i use get player pref with previous high score variable > finish > convert int to string to show gui text > finish > listener > finish > add int value 1 and loops back to converting int to string part.

I get confused with how to set a specific variable to tell this score is for this specific variable …

For example : (in your scenes you use playerprefKey - that playerprefsKey is everywhere in get and set in playerpref ) but in the game level scenes how do you set it up to tell this playerprefsKey is only for some specific score?
hmm ok let me show you my game image~!

- so the start state - i put set int value - int variable to CurrentHit - int value to 0. (this is what i want every time i want the game start , score to zero)
- finish and flow next state …  this part which i don’t understand… ( in this state i put playerpref Get int - in here i put same variable key from menu page with HighScore…(not CurrentHit) )
- finish and flow next state, that is converting int to string from CurrentHit to make gui text and doing all loops.

So if i uncheck the PlayerPref Get int in the game state…. when the game is finish the int compare don’t understand - current value is higher or lower and every time showing a currentHit number. This is the part i don’t understand
why is working with PlayerPref Get int with HighScore variable (i try with PlayerPref Set int with CurrentHit or HighScore doesn’t work with int compare in main page. )
 
Anyway thanks for example scenes that is really super helpful for me.
And i also looked at youtube 13- Player Preferences video how he set up two character thag & Gronk by using String variable. I know in my case playerprefsKey is the main, so i want this prefsKey to do know in the game state this playerprefsKey is receiving data … from current hit score.

Hi, Djaydino Thanks for explaining ~! but i don’t want to use asset yet cuz i want to understand how to set a prefskey to do specific thing in the game state. I understand the int-compare part but i don’t know how to Set this prefskey is for this specific score. i don’t know how to set it… for example like how two character thag & gronk set character by using a string variable. i want to understand this part.

Thanks you both~! for explaining and really helpful for me.

Groo Gadgets

  • Beta Group
  • Full Member
  • *
  • Posts: 175
Hey San,

I think you might be confused as to how playerprefs work. They can only be and int, float or string value and each playerpref has a key used to identify it. The key itself is always a string and you can call it whatever you like.

So if your game has 5 levels and you want to save a high score for each level you would need to create a playerpref key for each of the 5 levels like this:
Level 1 = highScoreLevel_1
Level 2 = highScoreLevel_2
etc...

When you create a game and distribute it, say you make a game for iOS and release it on the app store, when someone downloads and plays the games there will be no playerprefs installed with the game. You will need to create each playerpref either when the game is first run or when the player enters the level. That's when you want to use the "PlayerPrefs Set Int" action. The best way to do this is with the "PlayerPrefs Has Key" action.

So the first time your game is run you check to see if there is the highScoreLevel_1 key in your playerprefs. If yes then you want to get the value and load it into an FSM int value called something like "highScore". If no then you want to use the PlayerPrefs Set Int action to create it.

When the player dies you want to compare the players score against the highScoreLevel_1 value. If their score is lower then do nothing, if it's higher you want to overwrite the high score with the score the player just achieved.

So, in short, you only need to get the high score playerprefs value when you first enter the level, then you save the players score ONLY if it is higher than the high score stored in playerprefs.

Does that make sense?

Cheers,

Simon
« Last Edit: May 22, 2018, 02:34:36 AM by Groo Gadgets »

Groo Gadgets

  • Beta Group
  • Full Member
  • *
  • Posts: 175
Re: Set PlayerPref and Get PlayerPref not working with store variable~!
« Reply #9 on: June 03, 2018, 08:17:05 AM »
Hey San,

Just wondering if you managed to get everything working ok?