Reference to interface parameter

Hi, could use some help with my ResourceGenerationSystem and how i want to show it in the UI.

I Got multiple ItemGenerator.cs who create different Items every few seconds and i want to use one UI Element to show the basic stuff: Icon, name, _storedIemCount

I Click on my Gameobject with the ItemGenerator on it and the UI with ItemGeneratorUi enables.

public class ItemGenerator : MonoBehaviour, IItemStorage, IPointerClickHandler
{
    public event EventHandler OnItemStorageCountChanged;

    public ItemSO ItemGeneratorItem;

    private float _productionTimer;
    [SerializeField] private int _storedItemCount;

    public static UnityAction OnItemGeneratorUIRequest;

 

    private void Update()
    {
        if (ItemGeneratorItem != null && _storedItemCount <= ItemGeneratorItem.maxStackAmount - 1)
        {
            _productionTimer -= Time.deltaTime;
            if (_productionTimer <= 0f)
            {
                _productionTimer += ItemGeneratorItem.productionTimer;

                _storedItemCount += 1;
                OnItemStorageCountChanged?.Invoke(this, EventArgs.Empty);           
            }
        }
    }

    public ItemSO GetMiningResourceItem()
    {
        return ItemGeneratorItem;
    }


    public int GetItemStoredCount(ItemSO filterItemSO)
    {
        return _storedItemCount;
    }

more Code blub blub

public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
        {
            //Debug.Log("We Clicked");
            OnItemGeneratorUIRequest?.Invoke(); //opens the UI
        }       
    }

it uses the IItemStorage Interface:

public interface IItemStorage
{

    event EventHandler OnItemStorageCountChanged;

    int GetItemStoredCount(ItemSO filterItemSO);
    bool TryGetStoredItem(ItemSO[] filterItemSO, out ItemSO itemSO);
    bool TryStoreItem(ItemSO itemSO);
    ItemSO[] GetItemSOThatCanStore();

}

My Problem is when i want to show the GetItemStoredCount that returns _storedItemCount as a int in the UI:

private ItemGenerator _itemGenerator;
private void UpdateText()
    {
        _generatedItemText.text = _itemGenerator.GetItemStoredCount(ReferenceToClickedItemGenerator).ToString();
    }

I am not sure what parameter (ReferenceToClickedItemGenerator) i have to put in here…to have the right instance of the ItemGenerator referenced.

Help is much appriciated

I’m a bit confused about your question. Isn’t that your code and your interface? If not where did you get the system from?

Obviously GetItemStoredCount is a general function that is supposed to return the number of a particular item type stored in that item storage. So you have to pass the actual item type you want to know the amount of. If your implementation of the IItemStorage can / should only provide / hold a single item type, your implementation of GetItemStoredCount should look something like this:

    public int GetItemStoredCount(ItemSO filterItemSO)
    {
        if (filterItemSO == ItemGeneratorItem)
            return _storedItemCount;
        return 0;
    }

So the method would return 0 whenever the method is called with any other item type other than the actual item in this IItenStorage. If you played Minecraft with mods, think of JABBA barrels, storage drawers or deep storage units which can only store a single item type.

As to answer your question, the method is supposed to be used to query a particular item type. We don’t know where or why you want to query the item type at the point where you use this method. Though it’s up to you to pass the right item type. This method should not blindly return a certain item count as it is expected to tell you the item count for a particular item type. It probably should be

_itemGenerator.GetItemStoredCount(_itemGenerator.ItemGeneratorItem)

Thanks for the reply.
Iam not a professional programmer, so i had to gather the code as i went. I learned about interfaces when researching how to implement the same functionality on multiple scripts/subscribers. I have ItemGenerator and i have ItemProducer. ItemProducer uses two Items and makes another Item with them. Thats also why i thought i needed GetItemStoredCount(ItemSO filterItemSO) with the parameters, so i can differentiate on the ItemProducer which Items i have when i return they stored count.

Back to my problem:
I think i have to get a reference of the ItemGenerator i have clicked on and open the UI with the Infos of that particular ItemGenerator.
Because every variant and also with yours (_itemGenerator.GetItemStoredCount(_itemGenerator.ItemGeneratorItem)) i get an “NullReferenceException: Object reference not set to an instance of an object” with reference to the line of code where talking about here.

Got this working with firing a static Action OnItemGeneratorUIRequest when i click on the ItemGenerator Gameobject. The Action sends the Instance of the ItemGenerator (OnItemGeneratorUIRequest?.Invoke(this); ) and that way i can get the information across to my UI.

Yes, that is a good feeling!