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