playMaker

Author Topic: [Solved] PlayerPrefs overwrite/replace values  (Read 4850 times)

Thiaz

  • Playmaker Newbie
  • *
  • Posts: 11
[Solved] PlayerPrefs overwrite/replace values
« on: January 25, 2019, 07:47:11 PM »
So in one of my games i tried using Playerprefs to make a highscore system. And in this i need to store in playerprefs 8 int and 8 float.

So everything was working smoothly with 1 playerpref but then i got to 2 or more and i had this problem.

If i set more than 1 playerprefs, they are all equal to the same value. They don't have the same key, nor the same value. But the last playerpref that is saved replace all the others. And if it's a different type like a float, it reset to 0 my ints.

So i made another project to see what was wrong and i still encounter the same problem.

Simple project, 3 buttons :
- 1 : add 1 to an integer (ScoreB1) https://puu.sh/CCnEp/cb8b637c85.png
- 2 : add 1 to an integer (ScoreB2) https://puu.sh/CCngb/1a5b1dedbb.png
- 3 : a save button that set the playerprefs (save the score B1 and B2 in 2 differents keys) https://puu.sh/CCnFT/e95c27549c.png

4 ui text :

1 - Show the value of ScoreB1
2 - Show the value of ScoreB2
3 - Show the value of the storedKey equal to B1 (the highscore, stored when you exit the game)
4 - Same as 3 but for B2

https://puu.sh/CCniw/84694e4c23.png

But if i set the values to ScoreB1 = 15 and ScoreB2 = 7

https://puu.sh/CCnjv/597ed15e4b.png

then save it : https://puu.sh/CCnkI/bb99beb08c.png we can see the 2 differents keys and the 2 differents values in the set int.

But when i restart the game : https://puu.sh/CCnPB/80e4699b44.png both of my keys have a same value in the Get. (and on the ui text https://puu.sh/CCnlX/25368e5125.png)


Only thing i found is a guy from reddit (4 YEARS AGO) that had the same problem.

https://www.reddit.com/r/Unity3D/comments/27h5ea/need_a_little_help_with_high_score_system/

Quote
Problem 1: it seems like Playerprefs are only made for saving 1 variable of each type. Even though my high scores all use separate keys, if I call Playerpref Set Int it will set the int value for ALL of my Playerpref Int. Therefore, if I set a new high score in one game mode and then go play a different game mode, it will treat both high scores as 1. So I get the same high score value even though they are different game modes and using different keys.



I don't know what to do, maybe i'm setting it wrong.

https://puu.sh/CCnA8/66bec0bdf2.png


« Last Edit: January 28, 2019, 12:28:32 PM by Thiaz »

Thiaz

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Problem with playerprefs
« Reply #1 on: January 26, 2019, 05:51:22 AM »
Well maybe it's a bug from playmaker, i hope devs will adress the issue.

In the mean time i found this : way more intuitive and it works very well.
« Last Edit: January 26, 2019, 07:40:37 AM by Thiaz »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Problem with playerprefs
« Reply #2 on: January 27, 2019, 01:35:07 PM »
Thanks for posting that link.

I'll investigate to see if I can repro the problem with PlayerPrefs.

BTW, I noticed in the screenshots you're using a lot of convert actions and variables. Since 1.9.0 you can auto-convert variables in the variable selection dialog using the Convert sub-menu.

Thiaz

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Problem with playerprefs
« Reply #3 on: January 27, 2019, 05:41:57 PM »
BTW, I noticed in the screenshots you're using a lot of convert actions and variables. Since 1.9.0 you can auto-convert variables in the variable selection dialog using the Convert sub-menu.

Oh that's really nice, thanks !

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Problem with playerprefs
« Reply #4 on: January 28, 2019, 04:09:59 AM »
Hi,

 it's most definitly an issue with your logic and handling of the case.


to verify this, make a simple scene with two playerprefs, and do a simpler getter and a simple setter, and play around with this to make sure you are setting and getting the right values.

 also, I always use a Advanced PlayerPrefs (https://assetstore.unity.com/packages/tools/utilities/advanced-playerprefs-window-7070), it's a must when you start working with lots of playerprefs. you will have a proper overviews of your prefs, keys, values, etc, which will help you debug this.

PlayerPrefs are rock solid, it definitly works, if not, let me kow.

Bye,

 Jean

Thiaz

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Problem with playerprefs
« Reply #5 on: January 28, 2019, 10:08:29 AM »
Hi,

 it's most definitly an issue with your logic and handling of the case.


to verify this, make a simple scene with two playerprefs, and do a simpler getter and a simple setter, and play around with this to make sure you are setting and getting the right values.

 also, I always use a Advanced PlayerPrefs (https://assetstore.unity.com/packages/tools/utilities/advanced-playerprefs-window-7070), it's a must when you start working with lots of playerprefs. you will have a proper overviews of your prefs, keys, values, etc, which will help you debug this.

PlayerPrefs are rock solid, it definitly works, if not, let me kow.

Bye,

 Jean

Well... i mean i can't make something simplier than that :

https://puu.sh/CDBP8/9ebff45d40.png state1
https://puu.sh/CDBUn/845820ad76.png state2

yes i also tried with count = 2 instead of having 4 actions total.

You always use "Advanced PlayerPrefs" so it works for you. But i don't and at the moment the only thing i see is that playmaker's playerprefs are broken.

I'm using Unity 2018.3.2f1 but also tried on 2017.3.1f1 and the last version of Playmaker.


Plancksize

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 75
Re: Problem with playerprefs
« Reply #6 on: January 28, 2019, 11:53:51 AM »
I may be missing something, but what I notice is that your Key variables seem to be empty ( https://puu.sh/CCnkI/bb99beb08c.png ), making both KeyPF1 and KeyPF2 on that example be the same "empty" string.

(same happens here it seems: https://puu.sh/CDBUn/845820ad76.png )

You might be using empty string vars instead of strings there, leading to save values to just 1 playerpref), unless you're setting those string vars somewhere else in runtime.
« Last Edit: January 28, 2019, 12:03:21 PM by Plancksize »

Thiaz

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Problem with playerprefs
« Reply #7 on: January 28, 2019, 12:26:54 PM »
I may be missing something, but what I notice is that your Key variables seem to be empty ( https://puu.sh/CCnkI/bb99beb08c.png ), making both KeyPF1 and KeyPF2 on that example be the same "empty" string.

(same happens here it seems: https://puu.sh/CDBUn/845820ad76.png )

You might be using empty string vars instead of strings there, leading to save values to just 1 playerpref), unless you're setting those string vars somewhere else in runtime.

Well... for me it was "normal" to not add any value to the key because i wanted it to be "empty". Like if i used "DeleteAllKey" But by testing your theory i found that the "value" of the keys need to be different for each one. So the value of the key as the same use as the name of the key itself. And has to be filled.

https://puu.sh/CDF4z/ad3c469449.png both keys have the same value "NotEmpty" and they both are equal to 23

https://puu.sh/CDF80/ff7f0391f6.png differents values, it gives the correct output.

Well, thanks. That's a weird behaviour in my opinion, but hey, at least i know how it works now.

Solved, thanks Plancksize :) Glad i didn't have to pay 8 euros for a "fix" haha
« Last Edit: January 28, 2019, 12:30:25 PM by Thiaz »

Plancksize

  • Beta Group
  • Junior Playmaker
  • *
  • Posts: 75
Re: [Solved] PlayerPrefs overwrite/replace values
« Reply #8 on: January 28, 2019, 12:39:23 PM »
Glad it was solved.

It is not really a weird behavior.
Think like this: the variables you have are actually Playmaker's variables. Playerprefs are Unity's "persistent variables".
What you're doing with your playmaker variables is just tell playmaker what is the unity's playerpref key name to use. :)
I usually don't even use vars for playerprefs. I just type the playerpref key name in the action itself.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: [Solved] PlayerPrefs overwrite/replace values
« Reply #9 on: January 29, 2019, 03:04:14 AM »
Hi,

 Advanced PlayerPref doesn't do anything, it's only to monitor your values. you don't need it to work with playerprefs at all, but when you have problems in your logic flow, it's critical to be able to view the real data outside the handling logic itself, else you don't know if the problem is Unity, playerprefs, playmaker or something else.

Bye,

 Jean

 

Groo Gadgets

  • Beta Group
  • Full Member
  • *
  • Posts: 175
Re: [Solved] PlayerPrefs overwrite/replace values
« Reply #10 on: January 29, 2019, 03:13:06 AM »
Hey Thiaz,

You don't seem to understand the basic functionality of PlayerPrefs. It is essential that you name your keys, you might think it's "normal" to leave keys empty but the simple fact is that's not how PlayerPrefs values work. Leaving your key values blank makes as much sense as buying a blank house key from a locksmith and expecting it to open your house ::)

If you'd like to read a clear explanation on how PlayerPrefs work and also download an example scene showing PlayerPrefs functionality in playMaker check out this thread: http://hutonggames.com/playmakerforum/index.php?topic=18820.msg81751#msg81751

Cheers,

Simon