I found a way to solve this.
Solved: No Need to Pass the Index!
I was trying to pass an ItemIndex parameter to determine which item to purchase, but I couldn’t get it to work. Instead of forcing the index, I found a better solution: removing the need for it entirely.
Solution:
Instead of passing an index, I store the necessary item data inside an instance of my ItemPurchase class. Now, when the purchase function is called, it already knows which item to process.
How I Fixed It:
Create an instance of
ItemPurchase
with the item data:
ItemPurchaseInstance := ItemPurchase{Item_Data := Item, ItemConditional := ItemConditional}
Subscribe the
ItemPurchase
function to the button click event:
PurchaseButton.OnClick().Subscribe(ItemPurchaseInstance.Purchase)
Modify the
Purchase
method to work without an index:
ItemPurchase := class:
Item_Data : ItemData
ItemConditional : conditional_button_device
Purchase(Message: widget_message) : void =
Player := Message.Player
Item_Price := Item_Data.ItemPrice
KeyIndex := 0
ItemConditional.SetItemCountRequired(KeyIndex, Item_Price)
if (ItemConditional.HasAllItems[Player]):
Print("Purchase Item!\nPrice = {Item_Price}")
ItemConditional.Activate(Player)
Item_Data.ItemGranter.GrantItem(Player)
else:
Print("Unable to Purchase Item!\nPrice = {Item_Price}")
Why This Works?
No need to pass an index—each instance already knows which item it handles.
The code is simpler and easier to maintain.
The purchase function works directly without extra parameters.
Hope this helps others facing the same issue!