[ Solved ] Basic Shop Outline - PlayerPrefs Question

To bring you up to speed, I’m creating a very basic shop outline for my game, now I’ve worked with game arrays before, but this is the first time I’m working with arrays that have multiple values, for instance, I have the following values applied per each actor available to buy :

[System.Serializable]
public class Character {
    public GameObject actorPrefab;
    public string actorLabel;
    public int actorPrice;
    public bool purchased = false;
}

Now, I have the full shop system working fine, the only thing missing now is the PlayerPrefs code for the purchased game objects.

When you purchase, the amount is taken from the coinsTotal ( this is saved in PlayerPrefs ), I then hide the BUY button and replace it with a SELECT button, if the player then presses SELECT, this becomes the default player model ( this is saved in PlayerPrefs ).

Now, when you purchase an actor, I set the actorPrice to 0 and boolean purchased to true, and I’m really unsure as to the best way to SAVE this information to the PlayerPrefs, or at least go about it, is there a special way to address an object from an array with multiple values or ? Would appreciate any advice going forward.

my buy Button function :

    public void actorBuy() {
        // Check we have enough coins to purchase
        if (coinsTotal >= actors[index].actorPrice) {
            // Then purchase actor
            coinsTotal  = coinsTotal - actors[index].actorPrice; // Reduce coinsTotal by actorPrice ( save this to PlayerPrefs )
            textMoney.text = "Coins : " + coinsTotal.ToString(); // Update our coin amount display
            actors[index].purchased = true; // Set purchased to be true ( save this to PlayerPrefs )
            actors[index].actorPrice = 0; // Set actorPrice to be zero ( save this to PlayerPrefs )
            // Update screen ui elements
            actorPurchased();
            textReferences();
        }
        if (coinsTotal < actors[index].actorPrice) {
            Debug.Log ("Not Enough Coins!");
            // Then do something else here
        }
    }

Ok, slightly further ahead

I’m tackling the boolean value first as to whether an actor is purchased or not

I have the following boolean conversion ( from a tutorial ) :

    // Boolean to Integer
    int boolToInt(bool val) {
        if (val)
            return 1;
        else
            return 0;
    }

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

EDIT : So, after some time, I think ( assume ) I’m actually able to save my playerprefs data for the two values using : -

PlayerPrefs.SetInt("Purchased" + index, boolToInt(actors[index].purchased));
PlayerPrefs.SetInt("actorPrice" + index, 0);
PlayerPrefs.Save();

And reading the value back in, although I’m sure there is a much better way of doing this :

    // actorPurchased - Check if actor purchased
    private void actorPurchased() {
        if (PlayerPrefs.HasKey ("Purchased" + index)) {
            actors[index].purchased = intToBool(PlayerPrefs.GetInt("Purchased" + index));
            if (actors[index].purchased == true) {
                buttonSelect.SetActive(true);
                buttonBuy.SetActive(false);
            }
            if (actors[index].purchased == false) {
                buttonSelect.SetActive(false);
                buttonBuy.SetActive(true);
            }
        }

        if (actors[index].purchased == true) {
            buttonSelect.SetActive(true);
            buttonBuy.SetActive(false);
        }
        if (actors[index].purchased == false) {
            buttonSelect.SetActive(false);
            buttonBuy.SetActive(true);
        }
    }

    // Text References
    private void textReferences() {
        textLabel.text = actors[index].actorLabel;

        if (PlayerPrefs.HasKey ("actorPrice" + index)) {
            actors[index].actorPrice = PlayerPrefs.GetInt("actorPrice" + index);

            textPrice.text = actors[index].actorPrice.ToString();
            if (actors[index].actorPrice <= 0) {
                textPrice.text = "";
            }
        }

        textPrice.text = actors[index].actorPrice.ToString();
        if (actors[index].actorPrice <= 0) {
            textPrice.text = "";
        }
    }