playMaker

Author Topic: Saving Array to PlayerPrefs  (Read 7178 times)

dudebxl

  • Hero Member
  • *****
  • Posts: 602
    • View Profile
Saving Array to PlayerPrefs
« on: April 08, 2015, 08:52:49 PM »
I had a dream.. My dream was to save an array to playerprefs so here we go..  ::)

Description
Save any type of array to playerprefs using the playerprefsx extension. Tested with bool + int + color array. Should work with all other variables (have not yet tested   :( ) so pm if something is wrong.

Usage
  • PlayerPrefsx Set Array List: Setup same as any Array action. Start/End Index: off = values set to 0. They are used to save specific range of index into playerprefs. Caution: do not use 'Start/End Index' if you do not already have your full array saved or it will mess it all up. Clean pass: if your array is not a fix length, when true it will reset playerprefs saved array length to align with the data. Does not work when Start/End Index >0. So set to False if Array is a always a fix length. Bool + events are self-explanatory.
  • PlayerPrefsx Get Array List: Reference has to be equal to saved reference + array reference. Array Auto Prefill: Set to True when you went the array type + length to be set by action. For example, I have 5 array int saved and my Array on my Gameobject is set to color with a length of 10. The reference name (+obj path) is the same as action so the action will reset it to int / 5 length (same as saved data). Start/End Index: Same as above. Array Prefill Count: is the total size/length of saved array.
  • PlayerPrefsx Delete Array List: Will delete the saved data by array reference name. Also Destroy Array list: Will also deleted the array on GameObject.

Requirements
source: https://hutonggames.fogbugz.com/default.asp?W714

Also compatible with PlayerPrefs encryption (also found in add-ons) if you use the Set/Get Encrypt String action on string array. Encrypt strings(and/or converted data to strings) into string array then save array.

Note: Tested on Unity 4.x only and this is not a replacement to XML / JSON. PlayerPrefsX isn't intended to turn PlayerPrefs into a database system. If you have large amounts of data, you should use standard disk IO functions instead.

Note: Need to save 300+ keys then use http://hutonggames.com/playmakerforum/index.php?topic=10081.0

Update: all actions + updates are on ecosystem or https://snipt.net/dudebxl/

Hope it helps someone as much as it has helped me..
« Last Edit: May 10, 2016, 11:40:39 PM by jeanfabre »

mdotstrange

  • Hero Member
  • *****
  • Posts: 551
    • View Profile
    • Can't code? Who cares! Make games anyway!
Re: Saving Array to PlayerPrefs
« Reply #1 on: May 14, 2015, 07:37:55 PM »
I used this in my game today- works great- thanks so much for this and all your actions  :)
Indie game dev and instructor at the Strange School. Learn Playmaker at the Strange School!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14703
  • Official Playmaker Support
    • View Profile
Re: Saving Array to PlayerPrefs
« Reply #2 on: June 03, 2015, 12:02:19 AM »
Hi,

 yeah, this is great to see all the work you are putting in. Keep dreaming! :)

 Bye,

 Jean

miketolsa

  • Playmaker Newbie
  • *
  • Posts: 22
  • Just love playmaker !!!
    • View Profile
    • 4Buffs Productions
Re: Saving Array to PlayerPrefs
« Reply #3 on: September 01, 2015, 04:46:05 AM »
some how I am getting playerprefx not in current context error everytime I am trying to get the unity package installed in current project using unity 5.1.1f1 with latest playmaker & arraymaker installed .....

dudebxl

  • Hero Member
  • *****
  • Posts: 602
    • View Profile
Re: Saving Array to PlayerPrefs
« Reply #4 on: September 02, 2015, 03:52:00 AM »
Hi,

Can you give a break down of how u install everything.. Here is how u should do it.. Install arraymaker then install playprefs x then these actions.. When do u get the error?

szomaza

  • Sr. Member
  • ****
  • Posts: 253
    • View Profile
Re: Saving Array to PlayerPrefs
« Reply #5 on: February 10, 2019, 06:26:39 AM »
Thank you dudebxl for this, just what I was looking for now, but I have a problem I can't seem to figure out.

The PlayerPrefsx Set Array List works once (goes to Finished Event) but then the next time I use it, trying to update the values in player prefs, it goes to Failure Event with the following error message:

Code: [Select]
Index is less than 0 or more than or equal to the list count.
Parameter name: index
10
UnityEngine.Debug:Log(Object)
HutongGames.PlayMaker.Actions.PlayerPrefsxSetArrayList:GetItemAtIndex() (at Assets/PlayMaker Custom Actions/Array PlayerPrefsX/PlayerPrefsxSetArrayList.cs:161)
HutongGames.PlayMaker.Actions.PlayerPrefsxSetArrayList:OnEnter() (at Assets/PlayMaker Custom Actions/Array PlayerPrefsX/PlayerPrefsxSetArrayList.cs:85)
HutongGames.PlayMaker.FsmState:ActivateActions(Int32) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:205)
HutongGames.PlayMaker.FsmState:OnEnter() (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:175)
HutongGames.PlayMaker.Fsm:EnterState(FsmState) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2768)
HutongGames.PlayMaker.Fsm:SwitchState(FsmState) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2715)
HutongGames.PlayMaker.Fsm:UpdateStateChanges() (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2643)
HutongGames.PlayMaker.Fsm:UpdateState(FsmState) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2784)
HutongGames.PlayMaker.Fsm:Update() (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:1995)
PlayMakerFSM:Update() (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/PlayMakerFSM.cs:579)

Any idea what could be causing this?

szomaza

  • Sr. Member
  • ****
  • Posts: 253
    • View Profile
Re: Saving Array to PlayerPrefs
« Reply #6 on: February 22, 2019, 09:36:54 AM »
The error message sounds like it does not restart from index 0 when I use it the 2nd time.
(my array is 10 long)
Code: [Select]
Index is less than 0 or more than or equal to the list count.
Parameter name: index
10

How do I get it to always restart from index 0?
Is that even how it should work?

Please share any idea I should try to figure this out.

Thanks,
szomaza


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14703
  • Official Playmaker Support
    • View Profile
Re: Saving Array to PlayerPrefs
« Reply #7 on: March 04, 2019, 10:29:35 PM »
Hi,

 this action needs a reset option like we did on other similar iterating actions.

This is blind coding, but try the action attached. Add a state prior to your state with that action, and set a bool variable called  for example "Reset iterator" to true, then reference in that save array action this bool variable. the action will check it and if true will reset itself so you can iterate once again from the beginning.

if that doesn't work, can you send me a simple project that I can test and debug?


szomaza

  • Sr. Member
  • ****
  • Posts: 253
    • View Profile
Re: Saving Array to PlayerPrefs
« Reply #8 on: March 09, 2019, 01:20:09 AM »
Thank you Jean!

It works perfectly now with the reset option: Using it again and again in the same state, it never goes to failure event now, but saves the values correctly to playerprefs.