Access Item for Inventory through BindExternalFunction

36 Views Asked by At

I have a player inventory which gets handled by my inventory script. And now I would like to add something to the inventory using an ink file and the "BindExternalFunction". Like if I declare a item name in the Ink file and the external function gets called it should add that specific item into my inventory. For example I currently have my keys managed by the inventory and have an Item called CaveKey or WindmillKey. I will also post how I currently handle to add an item with a chest to make things maybe easier. Any suggestions?

public void EnterDialogueMode(TextAsset inkJSON)
    {
        dialogueIsPlaying = true;
        dialoguePanel.SetActive(true);
        currentStory = new Story(inkJSON.text);

        dialougeVariables.StartListening(currentStory);

        currentStory.BindExternalFunction("getItem", (string itemName) => 
        {
            Debug.Log(itemName);
        });

        //reset portrait usw to default.
        displayNameText.text = "???";
        portraitAnimator.Play("default");
        layoutAnimtor.Play("right");

        ContinueStory();
    }

    public void ExitDialogueMode()
    {
        dialougeVariables.StopListening(currentStory);
        currentStory.UnbindExternalFunction("getItem");

        dialogueIsPlaying = false;
        dialoguePanel.SetActive(false);
        dialogueText.text = "";
    }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu]
public class Inventory : ScriptableObject
{
    public Item currentItem;
    public List<Item> items = new List<Item>();
    public int numberOfKeys;
    public int numberOfWindmillKeys;
    public int coins;

    public void AddItem(Item itemToAdd)
    {
        if(itemToAdd.isKey)
        {
            numberOfKeys++;
        }
        if(itemToAdd.isWindmillKey)
        {
            numberOfWindmillKeys++;
        }
        else
        {
            if(!items.Contains(itemToAdd))
            {
                items.Add(itemToAdd);
            }
        }
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Chest : Interactable
{
    [Header("Contents")]
    public Item contents;
    public Inventory playerInventory;
    public bool isOpen;
    public BoolValue storedOpen;

    [Header("Signals nad Dialog")]
    public SignalSender raiseItem;
    public GameObject dialogBox;
    public Text dialogText;

    [Header("Animator")]
    private Animator anim;
    
    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        isOpen = storedOpen.RuntimeValue;
        if(isOpen)
        {
            anim.SetBool("opened", true);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.E) && playerInRange)
        {
            if(!isOpen)
            {
                // Open the Chest
                OpenChest();
            }
            else
            {
                // Chest is already open
                ChestAlreadyOpen();
            }
        }
    }

    public void OpenChest()
    {
        dialogBox.SetActive(true);
        dialogText.text = contents.itemDescription;
        playerInventory.AddItem(contents);
        playerInventory.currentItem = contents;
        raiseItem.Raise();       
        context.Raise();
        isOpen = true;
        anim.SetBool("opened", true);
        storedOpen.RuntimeValue = isOpen;
        
    }

    public void ChestAlreadyOpen()
    {
       
         dialogBox.SetActive(false);
         raiseItem.Raise();           
        
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.CompareTag("Player") && !other.isTrigger && !isOpen)
        {
            playerInRange = true;
            context.Raise();
        }
    }

    void OnTriggerExit2D(Collider2D other)
    {
        if(other.CompareTag("Player") && !other.isTrigger && !isOpen)
        {
            playerInRange = false;           
            context.Raise();
        }
    }
}
0

There are 0 best solutions below