Forum rules - please read before posting.

OnInventoryHover Messes with Combine Interactions

edited February 9 in Technical Q&A

From the title, it seems that when I try to combine two inventory items, it causes a null pointer issue.
The last debugging statement logged in the code below, when I combine two inventory items, is "invInstance is AC.InvInstance" and then it crashes.

Is there a way to prevent this behavior? I'm attaching my code below, any guidance / help is appreciated!

using UnityEngine;
using AC;
using UnityEngine.TextCore.Text;
using System.Collections.Generic;

public class InventoryAutoExamine : MonoBehaviour
{

    // the description of the hotspot to manually set
    // when an inventory item is selected.
    private string description;

    private void OnEnable() { EventManager.OnInventoryHover += OnInventoryHover; }
    private void OnDisable() { EventManager.OnInventoryHover -= OnInventoryHover; }

    private void OnInventoryHover(InvCollection invCollection, InvInstance invInstance)
    {
        // auto-run examine interaction if no inventory item selected
        if (KickStarter.runtimeInventory.SelectedItem != null && invInstance != null)
        {
            Debug.Log("GCG IT BECAME NULL");
            // combine interactions with inventory 
            description = "Combine " +
            KickStarter.runtimeInventory.SelectedItem.label +
            " with " +
            invInstance.ItemLabel;

            //TODO: HANDLE THE CASE WHERE THE PLAYER COMBINES THE ITEM WITH ITSELF.
            if(invInstance.ItemLabel == KickStarter.runtimeInventory.SelectedItem.label)
            {
                Debug.Log("LABELS MATCH");
            }
            // set description to combine interaction
            GlobalVariables.GetVariable("HotspotDescription").SetStringValue(description);

        } else if (KickStarter.runtimeInventory.SelectedItem == null && invInstance != null)
        {
            Debug.Log("invInstance is " + invInstance);
            if(invInstance == null)
            {
                Debug.Log("IT BECAME NULL WHEN DESELECTINFG");
                return;
            }
            GlobalVariables.GetVariable("HotspotDescription").SetStringValue(invInstance.GetProperty(0).TextValue);
        }
    }

    public void deselectInventory()
    {
        KickStarter.runtimeInventory.SetNull();
    }

}

Comments

  • edited February 9

    I should clarify -- this only happens when combining two inventory items( A and B ) produces a third item (C).
    This interaction should remove A and B and give the player C.

  • edited February 10

    Is the Property with ID = 0 available for all Items?

    One thing to note about the InvInstance class: it can be non-null but still invalid, if the amount it represents is zero.

    Rather than a null-check, use its IsValid static function instead, i.e.:

    if (InvInstance.IsValid (invInstance)) {}
    
  • Ah yes it's the first property for all the times.
    I think that was it actually -- the way I was doing the check -- thank you for the heads up on that front!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.