Playmaker Forum

PlayMaker Help & Tips => PlayMaker Tips & Tricks => Topic started by: LordShaggy on June 15, 2011, 11:02:09 AM

Title: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: LordShaggy on June 15, 2011, 11:02:09 AM
Oh well, I have a moment.  So I will explain how playerPrefs work. I will make a screen cast as soon as we get our website situation under control.


((WARNING, YOU WILL START SAVING DATA WITH THIS SO READ THROUGH THE WHOLE INSTRUCTIONS BEFORE GOING BUCK WILD!!  THIS IS LONG EXPLANATION))
-----THIS EXPLANATION ASSUMES YOU HAVE SOME KNOWLEDGE OF PLAYMAKER---

So playerprefs represents 10mb of saved data that you can generate and access at any time and will persist after the game has been shut down (( EVEN IN DEV MODE or PUSHING PLAY ON UNITY))

To get a hand of using them.  Here is a nice test you can do.

-Create a blank scene
-Make a sphere
-Put a FSM on it
-Name this state "PrepCheck"
-Create a bool variable.
-Now go to the action bar under playerprefs
-Add, "PlayerPrefs HAS Key"
--Put "1337" in the key field. (( this can be any number you want but you gotta start keeping track of them I have not figured out if the number holds any meaning other than an ID but it is perminate and all of them need to be unique))
--Make the variable under it the Bool you just made.
-Now go to the action bar and add a bool test.
-BEFORE YOU DO ANYTHING ELSE
-Go back to playerPrefs and add the "PlayerPrefs Set String" action
-In the "PlayerPref Set String" action, the "COUNT" represents how many individual strings you want to store.
--put 1 in there for now.
--You will notice that a KEY/VALUE will pop up.
---KEY = UNIQUE ID that represents where this information is stored in memory
---VALUE = the information
-So put in the key "1337"
-And put in the value "Player1"

(BREATHER TIME... YAY...)

-Alright lets get back to it.
-Now create 2 other stats label 1 "IHAVE"  the other "IHAVENOT"
-In your "PREP" state Make 1 event transitions((WHATEVER EVENTS YOU WANT BUT YOU NEED 1 Unique one... I suggest   SAVED))
-Now go back to your  BOOL TEST under PLAYER PREFS HAS KEY
-If it is true -> throw SAVE event which will lead to "IHAVE"
-if false -> Do nothing let it fall into the "Player Prefs set string action" Make the second transition "FINISH"


--- NOW BEFORE YOU GO GET CRAZY---

-ADD ONE MORE STATE
-call it "RemoveData"
-Give it a global transition
--rlight click the state its a choice that should be in one of the pop downs it makes a grey one pop above it.
-Set that to "MOUSE ENTER"
-Within the state add action ("Player prefs Delete all")


LAST to make it easy to understand.
-Go into your "IHAVE" state and add the "BLINK" action in default settings.


-----------------------------------------------------------------------------
THERE YOU GO YOU HAVE EVERYTHING YOU NEED

Intended behavior.

When you turn your game on with play, it will check to see if you already have that saved data.  If you do not, it will set the player prefs string and finish to the "IHAVE NOT" state.

Turn the game off (unclick play) then hit play again.  If it did it right, your object should start to blink.  Because this time when it ran the check you have that information and it has been stored.

Move your mouse over the object for it to delete all the player prefs.  Turn your game off and on again, and this time it should not blink.


VWALA THE POWER OF PLAYER PREFS is all yours.     I am sure you can come up with great ways to use it from here, but if you get that all working you should understand the power and usefullness... sorry for the LONG explination.
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: artician on June 15, 2011, 01:33:34 PM
That is great man.  Thank you very much for taking the time to make this explanation.  I look forward to the screencast too (sorry about the trouble that's kept you from putting them online yet). 
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: LordShaggy on June 15, 2011, 01:37:32 PM
All good bro =).  Hopefully we can get it sorted soon.
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: Darnen on July 19, 2011, 08:02:21 PM
This is very helpful, I have a question though. This saves a single set of data correct (the objects state that the fsm is connected to). To have a save game type function would I have to do this with every object I want to save in a particular instance for example the player saves at 40% into the game and has triggered numerous traps and opened boxes etc. How would I go about saving everything that the player has done thus far, by doing what you mentioned only once on a single game object or would I have to put this script on every object state I want to save?
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: artician on July 19, 2011, 08:12:03 PM
This is very helpful, I have a question though. This saves a single set of data correct (the objects state that the fsm is connected to). To have a save game type function would I have to do this with every object I want to save in a particular instance for example the player saves at 40% into the game and has triggered numerous traps and opened boxes etc. How would I go about saving everything that the player has done thus far, by doing what you mentioned only once on a single game object or would I have to put this script on every object state I want to save?

Mostly: Yes.  There are a number of workarounds though.  ways to cheat the registry keys into holding more data than you'd expect.  Someone suggested using a string of characters, and each character in the string represents a different piece of data you want to save.

I tried doing this with a long float, and using each int as a 0 or 1 boolean, but it was just too much work.  Instead I opted to set ~63+ keys in the users reg.  <shrug>  :)
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: giyomu on July 19, 2011, 08:21:28 PM
http://www.unifycommunity.com/wiki/index.php?title=ArrayPrefs2 (http://www.unifycommunity.com/wiki/index.php?title=ArrayPrefs2)

there is those little functionality to save more massive amount of data at a time in one spot using playerpref, if some feel motivated enough to include it in playmaker :)

after , well you can go with XML + serialized data or similar things with your own parser.  
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: Darnen on July 20, 2011, 10:13:23 PM
Thanks for the reply, I guess I will use what information I got and see what comes of it.
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: meaphly on November 22, 2011, 12:02:51 PM
This is actually a frustrating guide to a more simple task. You could have done way better.
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: artician on December 18, 2011, 05:17:30 PM
This is actually a frustrating guide to a more simple task. You could have done way better.

This helped me out quite a bit when it was posted.  Care to provide an alternative? 
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: tobbeo on December 18, 2011, 08:14:16 PM
This is actually a frustrating guide to a more simple task. You could have done way better.

This is actually a rude post. You could have been way more respectful.
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: Damian on February 14, 2012, 10:43:04 AM
I do use PlayerPrefs in my games.
It is very easy to use in scripts, but so far I have not used it in Playmaker.
If you use this make sure you do NOT use it in the game all the time.
It take time to use it and that can slow down the game a lot.
If you use IOS or Android you NEVER use it in the game, only when you save the data you want.
In the game you use other normal data variable to save the data and the flush them into the PlayerPrefs when the time is right. So it should not be used for a source to store live data in the game.
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: Red on February 05, 2013, 10:39:11 AM
This is very helpful, I have a question though. This saves a single set of data correct (the objects state that the fsm is connected to). To have a save game type function would I have to do this with every object I want to save in a particular instance for example the player saves at 40% into the game and has triggered numerous traps and opened boxes etc. How would I go about saving everything that the player has done thus far, by doing what you mentioned only once on a single game object or would I have to put this script on every object state I want to save?

Mostly: Yes.  There are a number of workarounds though.  ways to cheat the registry keys into holding more data than you'd expect.  Someone suggested using a string of characters, and each character in the string represents a different piece of data you want to save.

I tried doing this with a long float, and using each int as a 0 or 1 boolean, but it was just too much work.  Instead I opted to set ~63+ keys in the users reg.  <shrug>  :)

Just posting here that this is a decent method as well... though, if you want to cut down on a lot of stuff you can even use a regular int and converting it to binary can give you the "on/off" states as well.

but, decoding it takes a while to set up if the value is large. thing is, and this is with playmaker as well, if you're only using this to store the value and update/convert it to the appropriate values and have a sort of "boolean" system to store whether key items or such things have been unlocked, you can extract the information and leave it in that system and only re-compress it when you're ready to store it as a player-pref value.

but yeah... setting up multiple keys is probably the simpler and less headache-inducing method.

(*to illustrate the binary system i mentioned.*)
real values, not binary... adding to the stack as you go.

Door key = 1
rucksack = 2
pickaxe = 4
shovel = 8
enchanting book = 16
(if you go up, you can simply add these values to an int and the resulting binary value will still be the right value...

so, say you get all five items... the binary number will read "11111" and that will equal 31.
say you get the first and third items, the binary will read "00101" and that will equal 5.

This is theoretical, however... and i do not know how to implement a system such as this with Playmaker. i'm sure it's do-able but i haven't pushed myself to do it (since i'm using player prefs to just store the values of the weapons since each player only will ever have six and two are automatically given when you start the game.)
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: bloodymin on March 03, 2013, 10:54:22 AM
I have  some question on your description

Quote
-Create a bool variable.
- what name should it be in this FSM.

Quote
--Make the variable under it the Bool you just made.

- what kind of variable should i make? is it Bool or other? if it should be a bool what name should i use it?

Quote
-Add, "PlayerPrefs HAS Key"
- what should i do with this action? should i store result or do something? can you tell me more about this action?

I write this because it seemed didn't work on my fsm.  can you please explain more on this?
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: Boom7Games on October 13, 2014, 02:14:47 AM
It does not work
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: jeanfabre on October 15, 2014, 01:21:12 AM
Hi,


Quote
-Create a bool variable.
- what name should it be in this FSM.

-- It can ba any name, but don't be confused by the name of the FsmBool variable, and the "key" of the playerPref you will save this variable, I think a lot of people are getting confused between the two.



Quote
--Make the variable under it the Bool you just made.

- what kind of variable should i make? is it Bool or other? if it should be a bool what name should i use it?

you can use ANY name, there is no restriction here.

Quote
-Add, "PlayerPrefs HAS Key"
- what should i do with this action? should i store result or do something? can you tell me more about this action?

This action check that you have a "key" defined in your player prefs, this is very important typicall during the initial phase of the game, when the user is playing for the first time. no player prefs will be saved yet, and so you need to use a default variable instead, then when the user returns, you will have likely saved that key and so you will rely on its value instead of the default one.


I write this because it seemed didn't work on my fsm.  can you please explain more on this?

Have you check working samples? typically, I have created some PlayMaker port of M2h simple games, you should definitly study how they work:

https://hutonggames.fogbugz.com/default.asp?W880

and typically, study for example the jump game, where I use playerprefs to record the score:

https://hutonggames.fogbugz.com/default.asp?W895

PlayePrefs are working very well, you need to grasp how it works, so it's good you ask specific questions here, once you will understand the mechanism, it will be all very simple to use.

 Bye,

 Jean
Title: Re: PlayerPrefs Exposed!!! and Unsensored!!! =P
Post by: jayf on January 02, 2015, 09:02:21 AM
Hi. Just some questions about this example. The sphere starts blinking immediately after hitting play the first time. So I guess I did something wrong :)

The thing I don't quite understand is, if I'm not supposed to use the FSM Bool variable what do I use instead? Secondly, in the example I'm supposed to add a Bool Test action, yet it is never used. Do you mean perhaps I should use the "Player has key" to compare the bools?