playMaker

Author Topic: new action playerprefs add int / float[SOLVED]  (Read 3216 times)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
new action playerprefs add int / float[SOLVED]
« on: January 19, 2015, 06:09:44 AM »
hi,

i made a new action to add an int / float to a playerprefs int / float

so not to have to set 3 actions to do this (pp get int, int add, pp set int)

but i am trying to get it to work for multiple variables (Count)

when played i get Nullreference on line 26

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2015. All rights reserved.
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("PlayerPrefs")]
[Tooltip("Adds a value to a playerprefs int identified by key.")]
public class PlayerPrefsAddInt : FsmStateAction
{
[CompoundArray("Count", "Key", "Variables")]
[Tooltip("Case sensitive key.")]
public FsmString[] keys;
public FsmInt[] add;
private FsmInt[] ppValue;

public override void Reset()
{
keys = new FsmString[1];
ppValue = new FsmInt[1];
add = new FsmInt[1];
}

public override void OnEnter()
{
for(int i = 0; i<keys.Length;i++)
{
if(!keys[i].IsNone || !keys[i].Value.Equals(""))  ppValue[i].Value = PlayerPrefs.GetInt(keys[i].Value, ppValue[i].IsNone ? 0 : ppValue[i].Value);

ppValue[i].Value += add[i].Value;

PlayerPrefs.SetInt(keys[i].Value, ppValue[i].IsNone ? 0 : ppValue[i].Value);
}
Finish();
}

}
}

it has to do with the ppValue...
i don't have much experience with arrays and i guess somethings is wrong in that direction...

here is the code from the working "single add int to playerprefs" DO NOT USE THIS in your project as this will be changed when i got this working on multiple values (count)
Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2015. All rights reserved.
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("PlayerPrefs")]
[Tooltip("Adds a value to a playerprefs float identified by key.")]
public class PlayerPrefsAddInt : FsmStateAction
{
[Tooltip("Case sensitive key.")]
public FsmString key;
public FsmInt add;
private FsmInt variables;

public override void Reset()
{
key = "";
variables = new FsmInt();
add = null;
}

public override void OnEnter()
{
if(!key.IsNone || !key.Value.Equals("")) 
variables.Value = PlayerPrefs.GetInt(key.Value, variables.IsNone ? 0 : variables.Value);

variables.Value += add.Value;

PlayerPrefs.SetInt(key.Value, variables.IsNone ? 0 : variables.Value);

Finish();
}

}
}
« Last Edit: January 21, 2015, 06:19:41 AM by djaydino »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: new action playerprefs add int / float
« Reply #1 on: January 19, 2015, 02:07:26 PM »
Hi,

 you should be careful with this. Player Prefs is something that shouls always be decoupled from your logic, that is you get value from the playerprefs or use a default, then you work with that value during your gamePlay and save in playerprefs only at key moments.

 I can see where you are coming from with these actions, it's fine, but you should be careful nonetheless, as it is a common sign for potential trouble down the road.

CompoundArray only works with two variables, not three. I think this is your problem here.

 it's still ok to do it, but you'll have to build a custom editor for that action. If you want to go into this, check my xmlMaker, it's full of it :) and you'll have some actions where I let the user define multiple entries, it will be a good starting point for a possible way of creating these actions.

 Bye,

 Jean
Bye,

 Jean

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: new action playerprefs add int / float
« Reply #2 on: January 19, 2015, 04:09:38 PM »
yes, i'm using them only @ key moments :)
but i will add a warning in the tooltip.

i will keep them as single for now.

i will definitely check xmlMaker and array maker,
i need to learn more about  arrays :)

can i add the Action to the Ecosystem?

friendly greetings,

Dino

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: new action playerprefs add int / float
« Reply #3 on: January 20, 2015, 01:29:52 AM »
Hi,

 Yes, it's ok to put them on the ecosystem. The next development phase for the ecosystem is to allow you to define github repositories that you want to access, that's how I see the future of the ecosystem as far as contribution is concerned. So you would create your own github public repository(ies) and users would be presented with the whole to pick from when searching. this way we can credit contributors, properly track issues ( via github issue tracker), etc etc.

 I plan on doing this feature for march/april.

Currently, you can fork one of my rep and add your new actions, I'll review locally and merge your work, or you can clone it on your computer and push from their. It's up to you.

 Bye,

 Jean

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: new action playerprefs add int / float
« Reply #4 on: January 20, 2015, 09:47:56 AM »
Ok added them on the Ecosystem.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: new action playerprefs add int / float[SOLVED]
« Reply #5 on: March 02, 2015, 02:01:36 PM »
Hi,

 Great, but I can't seem to find on which rep you pushed them actions. do you have the link to the commit?

 Bye,

 Jean