I'm very new to events/delegates so sorry if I use the incorrect terminology.
I'm using an inventory script for Unity, that uses C# events/delegates to subscribe to a right click event on an item slot.
The problem is when I dynamically add new item slots, I need to add the event handlers to the new slots. If I just run UpdateEvents(), the ones that were there in the first place, now has duplicate triggers.
The current code is using a lambda syntax, and I've studied these threads on how to create a delegate instance:
- How to unsubscribe from an event which uses a lambda expression?
- Unsubscribing from anonymous delegate event
Here's the original lambda subscription:
// This is the lambda expression that I want to unsubscribe to
ItemSlots[i].OnRightClickEvent += slot => EventHelper(slot, OnRightClickEvent);
Here's what I tried, and I marked with ** on the parts that my IDE highlights as wrong:
// Try 1
EventHandler lambda = slot => EventHelper(slot, OnRightClickEvent);
ItemSlots[i].OnRightClickEvent += lambda;
// Try 2
EventHandler handler = (sender, e) => EventHelper(sender, OnRightClickEvent);
ItemSlots[i].OnRightClickEvent += handler;
// Try 3
var myDelegate = delegate(sender, e) { EventHelper(**e**, OnRightClickEvent); };
ItemSlots[i].OnRightClickEvent += myDelegate;
I also tried converting it without using lambda, but it doesn't work like it should. I'm not sure what "slot" refers to in the lambda. Is it the instance triggering the event? Here's what didn't work, but didn't give any errors:
// Try without lambda
ItemSlots[i].OnRightClickEvent += OnRightClickEvent;
Here's a shortened version of the complete code. I don't fully understand how the EventHelper()-method works, but it seems to be some shortcut to check for null.
using System;
using System.Collections.Generic;
using UnityEngine;
public abstract class ItemContainer : MonoBehaviour, IItemContainer {
public List<ItemSlot> ItemSlots;
// There are really 8 event here, but I simplified it
public event Action<BaseItemSlot> OnRightClickEvent;
protected virtual void Awake() {
UpdateEvents();
SetStartingItems();
}
public virtual void UpdateEvents() {
for (int i = 0; i < ItemSlots.Count; i++) {
// This is the lambda expression that I want to unsubscribe to
ItemSlots[i].OnRightClickEvent += slot => EventHelper(slot, OnRightClickEvent);
}
}
private void EventHelper(BaseItemSlot itemSlot, Action<BaseItemSlot> action) {
if (action != null)
action(itemSlot);
}
}