3

I have a payment system for my game set up here's my code :

 void Start()
 {
     T55.interactable = false;
     Tiger2.interactable = false;
     Cobra.interactable = false;
 }

 public void ProcessPurchase (ShopItem item)
 {
     if(item .SKU =="tank")
     {
         StoreHandler .Instance .Consume (item );
     }
 }

 public void OnConsumeFinished (ShopItem item)
 {
     if(item .SKU =="tank")
     {
         T55.interactable = true;
         Tiger2.interactable = true;
         Cobra.interactable = true;
     }
 }

Now each time the player buy something in the game the intractability of my 3 buttons goes to true; but the problem is each time he closes the game the intractability goes back to false how.

Should I save the process so the player doesn't have to buy again to set them back to true?

help-info.de
  • 6,695
  • 16
  • 39
  • 41
Arash Devil
  • 55
  • 1
  • 1
  • 3

6 Answers6

8

PlayerPrefs does not have an overload for a boolean type. It only supports string, int and float.

You need to make a function that converts true to 1 and false to 0 then the the PlayerPrefs.SetInt and PlayerPrefs.GetInt overload that takes int type.

Something like this:

int boolToInt(bool val)
{
    if (val)
        return 1;
    else
        return 0;
}

bool intToBool(int val)
{
    if (val != 0)
        return true;
    else
        return false;
}

Now, you can easily save bool to PlayerPrefs.

void saveData()
{
    PlayerPrefs.SetInt("T55", boolToInt(T55.interactable));
    PlayerPrefs.SetInt("Tiger2", boolToInt(T55.interactable));
    PlayerPrefs.SetInt("Cobra", boolToInt(T55.interactable));
}

void loadData()
{
    T55.interactable = intToBool(PlayerPrefs.GetInt("T55", 0));
    Tiger2.interactable = intToBool(PlayerPrefs.GetInt("Tiger2", 0));
    Cobra.interactable = intToBool(PlayerPrefs.GetInt("Cobra", 0));
}

If you have many variables to save, use Json and PlayerPrefs instead of saving and loading them individually. Here is how to do that.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
4

This is much faster

var foo = true;
// Save boolean using PlayerPrefs
PlayerPrefs.SetInt("foo", foo?1:0);
// Get boolean using PlayerPrefs
foo = PlayerPrefs.GetInt("foo")==1?true:false;

Source from here

Mohamed Saleh
  • 2,881
  • 1
  • 23
  • 35
1

I write this answer because there are people who need this answer yet, and I think is even easier than the others above.

To save:

PlayerPrefs.SetInt("Mute_FX", mute ? 1 : 0);

To load:

PlayerPrefs.GetInt("Mute_FX") == 1 ? true : false;

If you don't understand what's happening, I recommend you read about ternary operator in C#.

Edit: I didn't see Mohamed Saleh answer, but it's the same.

Lobsang White
  • 97
  • 1
  • 1
  • 14
  • 3
    Your second line of code does not need the conditional operator. You can just use: `PlayerPrefs.GetInt("Mute_FX") == 1` – Chris Dunaway Jan 11 '19 at 18:27
0

Writing:

bool val = true;
PlayerPrefs.SetInt("PropName", val ? 1 : 0);
PlayerPrefs.Save();

Reading:

bool val = PlayerPrefs.GetInt("PropName") == 1 ? true : false;
Utopia
  • 663
  • 7
  • 8
0

Also smooth

private int BoolToInt(bool val)
{
    return val ? 1 : 0;
}

private bool IntToBool(int val)
{
    return val == 1;
}
5er
  • 2,506
  • 7
  • 32
  • 49
0

I don't know why someone didn't suggest a wrapper since this would be a much cleaner way to extend PlayerPrefs.

public class PlayerPrefsWrapper : PlayerPrefs
{
    public static void SetBool(string key, bool value)
    {
        SetInt(key, value ? 1 : 0);
    }

    public static bool GetBool(string key)
    {
        return GetInt(key) == 1;
    }
}

using this you can write

using PlayerPrefs = PlayerPrefsWrapper;

PlayerPrefs.SetBool("key",true);
bool value = PlayerPrefs.GetBool("key");