Question about inventory/action bar

I am currently doing lots of polishing, and extending of systems, before showing off my “Final” project… However, I ran into an issue that I can’t quite figure out…
I have created a new Door/Lock system. Basically a door can be unlocked, or locked, if the player has a key in their inventory, The door will unlock and open, if the player does not have a key in their inventory, then a console message appears, saying there is no key in the inventory… Thats all great and working fine…
However, I have set the keyItem.cs up as Stackable, and Consumable, as it inherits from ActionItem.cs…
I can drag the key Item onto the action bar, and click on it, and it uses the key correctly, though the only thing it does is print “Key Used”, because there is no door to unlock… The key is consumed… the quantity is updated, as expected.
However, when I open the door, the key used, the console prints the Key Used Message, The door unlocks, and the key is still in my inventory, and the quantity is not updated. Ive searched through the API, gone to previous lecture in the other course, and cannot figure out why the key is not being consumed when it is marked as Consumable…

Can anyone provide me are refresher on accessing the Quantity in the inventory? The system has access, its checking that the player has a key… but I need help remembering how to remove the item, because its only working when you click the item in the action bar… Thank you.

After more reading and watching and experimenting… I realized the Action Item is pretty specific to the action bar… and finally found what I am looking for… and it was super simple. Here is my new KeyItem.cs.

namespace RPG.Inventories
{
    [CreateAssetMenu(fileName = "Key", menuName = "RPG/Keys/Make Key Item")]
    public class KeyItem : InventoryItem
    {
        
        
        public void UseKey(Inventory playerInventory, int numberOfKeysRequired)
        {
            var slot = playerInventory.FindSlot(this);
         
            playerInventory.RemoveFromSlot(slot, numberOfKeysRequired);
        
        }
    }
}

and the DoorController.cs

namespace RPG.Core
{
    public class DoorController : MonoBehaviour, IRaycastable
    {
        [SerializeField] Transform interactionPoint;

        [SerializeField] bool isLocked = false;
        [SerializeField] Animator animator;
        [SerializeField] BoxCollider interactionCollider;

        [SerializeField] KeyItem key;
        [SerializeField] int numberOfKeyRequired = 1;
        

        public void InteractWithDoor(PlayerController player)
        {
            if (!isLocked)
            {
                player.GetComponent<CharacterAnimation>().ChangeAnimationState(Helpers.activateAnimation);
                animator.SetTrigger("Open Door");              
                interactionCollider.enabled = false;
            }
            else
            {
                print("Door is locked");
                if(CheckForKey(player))
                {
                    player.GetComponent<CharacterAnimation>().ChangeAnimationState(Helpers.activateAnimation);
                    animator.SetTrigger("Open Door");
                    interactionCollider.enabled = false;
                    
                   
                }
                else
                {
                    print($"Player does not have a key.");
                }

                
            
            
            }
        }

        private bool CheckForKey(PlayerController player)
        {
            if(Inventory.GetPlayerInventory() != null)
            {
                Inventory playerInventory = Inventory.GetPlayerInventory();
                if(playerInventory.HasItem(key))
                {
                    print($"Door unlocked with {key.name}");
                    key.UseKey(playerInventory, numberOfKeyRequired);

                    return true;
                }
            }
            return false;
        }

        public CursorType GetCursorType()
        {
            return CursorType.Door;
        }

        public bool HandleRaycast(PlayerController callingController)
        {
            return true;
        }

        public Transform InteractionPoint()
        {
            return interactionPoint;
        }

        public RaycastableType RaycastType()
        {
            return RaycastableType.Door;
        }
    }
}
1 Like

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

Privacy & Terms