A bit of a Quiver Slot UI Problem... [Out of Course Content]

OK so I’m currently cleaning up a few bugs, and one bug that’s really really annoying me, is the Quiver Slot UI. For some reason, it may or may not update in the QuiverSlotUI.cs slot, and it’s driving me crazy as of why. For context, here’s the ‘QuiverSlotUI.cs’ script:

using System;
using GameDevTV.Core.UI.Dragging;
using GameDevTV.Inventories;
using RPG.Inventories;
using TMPro;
using UnityEngine;

namespace GameDevTV.UI.Inventories {

    public class QuiverSlotUI : MonoBehaviour, IItemHolder, IDragContainer<InventoryItem> {

        [SerializeField] InventoryItemIcon icon = null;
        [SerializeField] private GameObject itemCounter;
        [SerializeField] private TextMeshProUGUI number;

        private Quiver quiver;

        private void Awake() 
        {
            quiver = Inventory.GetPlayerInventory().GetComponent<Quiver>();
            quiver.quiverUpdated += RedrawUI;
        }

        void RedrawUI()
        {
            icon.SetItem(GetItem());
            itemCounter.SetActive(GetNumber() > 0);
            number.text = $"{GetNumber()}";
        }

        public InventoryItem GetItem() 
        {
            return quiver.GetItem();
        }

        public int GetNumber() 
        {
            return quiver.GetAmount();
        }

        public void RemoveItems(int number) 
        {
            quiver.RemoveAmmunition(number);
        }

        public int MaxAcceptable(InventoryItem item) // Part of the 'IDragContainer<InventoryItem>' Interface
        {
            if (item is AmmunitionItem ammunitionItem) 
            {
                return Int32.MaxValue;
            }

            return 0;
        }

        public void AddItems(InventoryItem item, int number) // Part of the 'IDragContainer<InventoryItem>' Interface
        {
            // This inherited function from the drag and drop system checks if
            // the item we are dragging and dropping to our Quiver slot is the same as the
            // item that already exists. If so, just add the numbers up (stackable items). If not, replace

            if (item is AmmunitionItem ammunitionItem) {
                
                if (object.ReferenceEquals(ammunitionItem, quiver.GetItem())) {

                    quiver.AddAmmunition(ammunitionItem, number + quiver.GetAmount());

                }

                else quiver.AddAmmunition(ammunitionItem, number);

            }

        }

        public void TryHandleRightClick(Inventory inventory) {

            if (GetItem() is AmmunitionItem ammunitionItem) {

                AmmunitionItem equippedAmmunition = quiver.GetItem();

                if (inventory.HasSpaceFor(equippedAmmunition)) {

                    inventory.AddToFirstEmptySlot(equippedAmmunition, GetNumber());
                    quiver.RemoveAmmunition();

                }

            }

        }

    }

}

I also went to ‘InventorySlotUI.cs’ and tried calling the ‘Quiver.TriggerUpdated()’ function (basically invoke the Quiver Updated function), where it should be invoked:

            else if (item is AmmunitionItem ammunitionItem && inventory.GetGameObject().TryGetComponent(out Quiver quiver))
            {

                // If there is a "Quiver" Equipment slot, and what we clicked on is a Rangers' arrow,
                // then we shall transfer the arrow to the dedicated slot

                Debug.Log($"{ammunitionItem} is ammo, transferring to {quiver}");
                TransferToQuiver(ammunitionItem, quiver, number);
                quiver.TriggerUpdated();
            }

But still, it does not 100% get (visually) updated when I open my Equipment tab. Sometimes (Most of the time) a Re-equipment will be necessary

Any idea how to fix this? It’s a minor bug, but it’s quite annoying tbh

(I also have to mention that this slot is a stacked slot. In other words, it accepts stacked items (and it’s the only one in my game of that kind), if that makes a difference)

There’s a similar problem with the amount of money in my player’s purse, but I will get to that another day. For now, I want to fix this one first

Edit 1: Apparently I just learned that for that event to be called, the ‘Equipment’ Window slot has to be opened at least one when the game starts. I want to end this curse :sweat_smile: - for every other slot in the equipments tab, this is not a problem at all (the problem isn’t with the ‘RedrawUI()’ function. It’s with whoever is calling that function, somehow not recognizing it when it’s first clicked when the game is open again!).

After a little bit of further understanding, I noticed it’s because ‘QuiverSlotUI.Awake()’ ONLY GETS CALLED WHEN THE QUIVER SLOT IS ENABLED (FOR THE FIRST TIME, WHEN A NEW GAME IS CREATED). In other words, when you start the game, ‘Awake()’ in ‘QuiverSlotUI.cs’ is NOT ENABLED UNTIL YOU OPEN THE EQUIPMENTS TAB, hence the annoying problem

This risks a tight coupling, so I’ll try go the clean way and use an Interface instead

Edit 1: Interface failed… I mean technically I got it to work, but that Quiver Slot UI is disabled when the game starts, and this makes things quite hard :sweat_smile:

(What’s really REALLY driving me nuts, is how does the Equipment and Armour system have this working, but the Quiver does not)

Fixed it. I was missing this in ‘QuiverSlotUI.cs’:

        private void Start() 
        {
            RedrawUI();
        }

Lesson learned: “Start()” will run the code AS SOON AS THE SCRIPT IS ENABLED (which is what I needed in my case). “Awake()”, on the other hand, will only run the code as soon as the game starts

This whole “Let me try fix this problem before @Brian_Trotter sees this thread” game is really teaching me a lot :stuck_out_tongue: - but still, sometimes there are concepts where I actually don’t know what to do and do need a professional’s help :slight_smile:

Just under the wire! You’ll find you’ll need this particular arrangement on just about any state based UI you have. I always think of that Start() as priming the pump. Great Job!!

learning new things each day, through a lot of try and error, and keeping it simple (i.e: delete what doesn’t work before trying new stuff) :slight_smile:

And regarding the Purse issue, the output was false because I was getting the balance in ‘Start()’ instead of ‘Awake()’. In other words, the value wasn’t being called before the Refresh UI (i.e: not “early enough”), so it got mixed with the numbers and wrote inaccurate results

But something is extremely wrong, because if we start from the main menu, although the assigned balance is $500, we start off with $400 (invisible taxes, or a bug? Invisible taxes! :stuck_out_tongue:)

@Brian_Trotter Wanna hear an absolute joke? It starts as $400 instead of $500 BECAUSE I ACCIDENTALLY FORGOT THE PURSE IN MY CHARACTER GENERATOR SCREEN VALUE’S PURSE SET AT $400 INSTEAD OF $500 :face_palm: (I ended up deleting whatever is un-needed in the character generator when it comes to the scripts)

At this point in time, I believe that at 100% accuracy, anything can be solved with a little bit of persistence - I hope this leads me to the end of the project :slight_smile:

Anyway, back to Object Pooling, whilst I try and go fix the absolute final bug

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

Privacy & Terms