0
\$\begingroup\$

So I want to make a merge idle game, but I have a problem loading data from playerprefs, I have an item that I bought from the store so obj counts are increasing but at the same time I want to check another item bought from the store, for example, I buy obj1 twice, and obj2 3 times so obj counts are total of those items 2+3=5.

What I've tried:

  • I'm using for loop for that, but I am confused about how to check for another item.
  • using a nested loop, still doesn't work.

Conditions have met:

  • when I bought from the store the obj count increased.
  • when the object merged obj count decreased;
  • save current object with playerprefs.set("currItemBought+index,itemBought),
  • save current current obj index when buy obj.

What I want:

  • every time I start the game again I load data from the playerprefs then the object that spawns must be the same from I bought for example when I buy obj2 twice then when re-start, obj2 must spawn twice, or another example when I buy obj1 once and obj2 3 times so when re-start must spawn obj1 once and obj2 3 times so total obj count is 4.

Preview:

https://youtu.be/vCi0I-YFwfQ

My Script:

Load Data

    //load Data
    public void LoadData()
    {

        if (PlayerPrefs.HasKey("objCount"))
        {
            // get tier to save 

            objCount = PlayerPrefs.GetInt("objCount");
            currHighesTier = PlayerPrefs.GetInt("tier");

            Debug.Log(objCount);
            Debug.Log("tier: " + currHighesTier);

            for (int i = 0; i < objCount; i++)
            {
                if (currHighesTier == i)
                {
                    SpawnObject(currHighesTier);

                }
                else
                {
                    SpawnObject(0);
                }
            }
        }
        else
        {
            objCount = 1;
            PlayerPrefs.SetInt("objCount", objCount);

            SpawnObject(0);
        }
    }

Buy Item

public void Buy_Object(int index)
    {
        currCharacter = UI_Manager.instance.characterDB.GetCharacter(index);

        for (int i = 0; i < UI_Manager.instance.shopItemsContainer.childCount; i++)
        {
            if (index == i)
            {
                if (Coin >= characters.characters[index].basePrice * (UI_Manager.instance.shopItemsContainer.GetChild(index).GetComponent<CharacterItemUI>().itemBought + 1))
                {
                    objCount++;
                    PlayerPrefs.SetInt("objCount", objCount);

                    Add_Coin(-characters.characters[index].basePrice * (UI_Manager.instance.shopItemsContainer.GetChild(index).GetComponent<CharacterItemUI>().itemBought + 1));

                    UI_Manager.instance.shopItemsContainer.GetChild(index).GetComponent<CharacterItemUI>().itemBought++;
                    PlayerPrefs.SetInt("currItemBough" + index, UI_Manager.instance.shopItemsContainer.GetChild(index).GetComponent<CharacterItemUI>().itemBought);

                    SpawnObject(index);

                }
                else
                {
                    Debug.Log("Not enough coin");
                }
            }
        }

    }

Evolve when merging

private void Evolve()
    {
        //Set object count
        GameManager.instance.objCount--;
        PlayerPrefs.SetInt("objCount", GameManager.instance.objCount);

        UI_Manager.instance.shopItemsContainer.GetChild(tier).GetComponent<CharacterItemUI>().itemBought--;
        PlayerPrefs.SetInt("currItemBough" + tier, UI_Manager.instance.shopItemsContainer.GetChild(tier).GetComponent<CharacterItemUI>().itemBought);

        evolve_particle.Play();

        LevelProgressUI.Instance.AddXP((tier + 1) * 25);

        tier++;

        gameObject.name = "Obj" + tier;

        GameManager.instance.CheckTier(tier);

        SetObject();
    }

Check Tier

 public void CheckTier(int tier)
    {
        if (tier > highestTier)
        {
            highestTier = tier;

            PlayerPrefs.SetInt("tier", highestTier);

            StartCoroutine(UI_Manager.instance.NewTier());

            characters.characters[tier].isUnlocked = true;

            UI_Manager.instance.UpdateUI(tier);
        }
        else
        {
            PlayerPrefs.SetInt("tier", tier);
        }
    }

`Sorry for the preview I use youtube due to 1-minute videos. from the video when I restart the game the obj that was bought twice only spawned once.

\$\endgroup\$
0
2
\$\begingroup\$

Your main error is, you do not iterate over all stored objects. objCount has the total amount stored which is not really helpful for loading the data.

Here is a small example how you could improve your LoadData. Obj is not really helpful as a description. And PlayerPrefs.getInt has an overload where the second parameter is the default value that you get when the key does not exists.

enum Spawnable {Seed, Sprout, Flower, Tree}

public void Start() {
    PlayerPrefs.SetInt("Item_" + Spawnable.Seed, 3);
    PlayerPrefs.SetInt("Item_" + Spawnable.Sprout, 1);
    PlayerPrefs.SetInt("Item_" + Spawnable.Tree, 2);
    LoadData();
}

void LoadData() {
    Spawnable highestTier = 0;
    int totalSpawnCount = 0;
    foreach (Spawnable toSpawn in Enum.GetValues(typeof(Spawnable)).Cast<Spawnable>()) {
        int spawnAmount = PlayerPrefs.GetInt("Item_" + toSpawn, 0);
        highestTier = spawnAmount > 0 ? toSpawn : highestTier;
        for (int amount = 1; amount <= spawnAmount; amount++) {
            Debug.Log(toSpawn);
            totalSpawnCount++;
        }
    }
    Debug.LogFormat("Highest Tier is {0}", highestTier);
    Debug.LogFormat("Total amount of spawned items is: {0}", totalSpawnCount);
}

The improvement is, all your objects are in order in a descriptive list and your evolve function might benefit from it as well.

The initial data is set as example in Start but it is missing the Flower to demonstrate how missing values are handled. The console is telling you how many objects are spawned from each type, you can just switch it to your own instantiate logic.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .