Loading to refresh UI

There are 4 items in a Shop, each item’s availability is 1. Every time an item is being purchased, it’s gone from the shop forever, its cloned game object gets destroyed in the scene as well.
Say I purchased two (so two items are gone from the shop) and I saved the progress in the Shop Script. When I loaded the game after doing another purchase (1 left) or when I reloaded the game and loaded the progress (4 items in the shop). The inventory was updated, but the ShopUI didn’t get updated.

The item unique numbers and quantities are being saved. The picture below shows the soldItems (print(pair); ) in the saved file.

If I want to load the ShopUI correctly, should I compare the availabilities to the sold items to Destroy the cloned game objects?

How do I use foreach(…) to compare two lists (the availabilities to the sold items) in OnEnable like MainScreenSaveLoadUI? Or should I use this approach?

Should I compare the lists in the Shop or should I compare the lists in the ShopUI, and how do I compare them?

1)Shop Script: private Dictionary<InventoryItem, int> GetAvailabilities()

2)ShopUI Script: public void RefreshUI()

Hmm… looking over the code, I see we’re not calling onChange in restoreState.

Add this to the end of RestoreState()

onChange?.Invoke();

Then if the ShopUI is open and attached to the shop, the display will update with the correct items available (or not available, as the case may be).

Thanks! Love it.

Should I do the same for questList as well? Adding onUpdate?.Invoke() ?

public void RestoreState(object state)
        {
            List<object> stateList = state as List<object>;
            if (stateList == null) return;

            statuses.Clear();
            foreach (object objectState in stateList)
            {
                statuses.Add(new QuestStatus(objectState));
            }
             // onUpdate?.Invoke() <--should I add this here?
        }

Yes, I would recommend that there as well.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms