accepting item in inventory by Lua

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
tohaMem
Posts: 21
Joined: Sat Oct 07, 2023 9:05 am

accepting item in inventory by Lua

Post by tohaMem »

Hello! I just recently started using the dialogue manager, so I'm still doing stupid things...
So! I'm trying to register items through Lua that I put in my own inventory. I followed the tutorialhttps://www.youtube.com/watch?v=55I0psnQp7Q, but for some reason it doesn't work :(
I will be glad to receive your help!

Code: Select all

public class Item : MonoBehaviour
{
    [SerializeField]
    private string itemName;

    [SerializeField]
    private int quantity;

    [SerializeField]
    private Sprite sprite;

    [TextArea]
    [SerializeField]
    private string itemDescription; 

    private InventoryManager inventoryManager;

    // Start is called before the first frame update
    void Start()
    {
        inventoryManager = GameObject.Find("InventoryCanvas").GetComponent<InventoryManager>();
    }

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Player")
        {
            inventoryManager.AddItem(itemName, quantity, sprite, itemDescription);
            Destroy(gameObject);
            
        }
    }

    private void OnEnable()
    {
        Lua.RegisterFunction("ItemPicked", this, SymbolExtensions.GetMethodInfo(() => ItemPicked(string.Empty)));
    }

    private void OnDisable()
    {
        Lua.UnregisterFunction("ItemPicked");
    }

    public bool ItemPicked(string name)
    {
        if (name == "poop") return true;
        return false;
    }
}
Image
Image
Image
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: accepting item in inventory by Lua

Post by Tony Li »

Hi,

That all looks correct so far. If you set the Dialogue Manager GameObject's Other Settings > Debug Level to Info and reproduce the issue, does the Console have a line similar to the one below?

Code: Select all

Dialogue System: Block On False Link: 'some text' Conditions: CurrentQuestState("FindCard") == "active" and (ItemPicked("poop") == true)
Or is it like below?

Code: Select all

Dialogue System: Add Link: 'some text' 
If it's Block On False Link, this means the Conditions are false. They could be false because the FindCard quest isn't active, or because ItemPicked("poop") doesn't return true.

If you see a red error that ItemPicked() is not a registered Lua function, then what GameObject is the InventoryManager script on?
tohaMem
Posts: 21
Joined: Sat Oct 07, 2023 9:05 am

Re: accepting item in inventory by Lua

Post by tohaMem »

So, i tried to create new quest, and now I got an error:
Image

InventoryManager is on Canvas
Image

and this is what's inside

Code: Select all

public class InventoryManager : MonoBehaviour
{

    public GameObject InventoryMenu;
    private bool menuActivated;
    public ItemSlot[] itemSlot;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Inventory") && menuActivated)
        {
            Time.timeScale = 1;
            InventoryMenu.SetActive(false);
            menuActivated = false;
        }

        else if (Input.GetButtonDown("Inventory") && !menuActivated)
        {
            Time.timeScale = 0;
            InventoryMenu.SetActive(true);
            menuActivated = true;
        }
    }

    public void AddItem(string itemName, int quantity, Sprite itemSprite, string itemDescription)
    {
        for (int i = 0; i < itemSlot.Length; i++)
        {
            if (itemSlot[i].isFull == false) 
            {
                itemSlot[i].AddItem(itemName, quantity, itemSprite, itemDescription);
                return;
            }
        }
    }

    public void DeselectAllSlots()
    {
        for (int i = 0; i < itemSlot.Length; i++)
        {
            itemSlot[i].selectedShader.SetActive(false) ;
            itemSlot[i].thisItemSelected = false ;
        }
    }
}
if you need it, here is ItemSlot code:

Code: Select all

public class ItemSlot : MonoBehaviour, IPointerClickHandler
{
    //ITEM DATA
    public string itemName;
    public int quantity;
    public Sprite itemSprite;
    public bool isFull;
    public string itemDescription;
    public Sprite emptySprite;


    //ITEM SLOT
    [SerializeField]
    private TMP_Text quantityText;

    [SerializeField]
    private Image itemImage;

    //ITEM DESCRIP
    public Image itemDescriptionImage;
    public TMP_Text ItemDescriptionNameText;
    public TMP_Text ItemDescriptionText;


    public GameObject selectedShader;
    public bool thisItemSelected;

    private InventoryManager inventoryManager;

    private void Start()
    {
        inventoryManager = GameObject.Find("InventoryCanvas").GetComponent<InventoryManager>();
    }

    public void AddItem(string itemName, int quantity, Sprite itemSprite, string itemDescription)
    {
        this.itemName = itemName;
        this.quantity = quantity;   
        this.itemSprite = itemSprite;
        this.itemDescription = itemDescription;
        isFull = true;

        quantityText.text = quantity.ToString();
        quantityText.enabled = true;
        itemImage.sprite = itemSprite;
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if(eventData.button== PointerEventData.InputButton.Left) 
        {
            OnLeftClick();
        }
    }


    private void OnLeftClick()
    {
        inventoryManager.DeselectAllSlots();
        selectedShader.SetActive(true);
        thisItemSelected= true;
        ItemDescriptionNameText.text = itemName;
        ItemDescriptionText.text = itemDescription;
        itemDescriptionImage.sprite = itemSprite;
       
        if(itemDescriptionImage.sprite == null) 
        {
            itemDescriptionImage.sprite = emptySprite;
        }

    }
}
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: accepting item in inventory by Lua

Post by Tony Li »

If your InventoryManager is in the Dialogue Manager GameObject's hierarchy, register your ItemPicked() method with Lua in an Awake() method instead of OnEnable(), and remove the OnDisable() method.
tohaMem
Posts: 21
Joined: Sat Oct 07, 2023 9:05 am

Re: accepting item in inventory by Lua

Post by tohaMem »

Now it's works. The problem was in Destroy(gameObject) xD
Thank you!
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: accepting item in inventory by Lua

Post by Tony Li »

Glad it's working!
Post Reply