Automatic Event Generations for Delegates/Events

tedw4rd 3 years ago updated by Lazlo Bonin (Lead Developer) 7 months ago 4

We already have automatically generated Units for functions on different MonoBehaviours. Why not Events for events and delegates? For example, I have the following MonoBehaviour:

    public class InventoryOwner : MonoBehaviour
        public Inventory Inventory;         public delegate void StoreHandler(Item itemToStore, Grabbable grabbable);         public delegate void DispenseHandler(Item dispensedItem);         public event StoreHandler DoStore;
        public event DispenseHandler DoDispense;         public void Store(Item item)
            Grabbable grabbable = item.GetComponent<Grabbable>();
            if (DoStore != null)
                DoStore(item, grabbable);
        }         public void Dispense(Item item = null)
            if (item == null)
                item = Inventory.Items.RandomElement();
            Inventory.Remove(item);             if (DoDispense != null)

I'd like to be able to trigger some FlowMachine behavior off of DoStore and DoDispense using a Bolt Event. My current workaround for this is to make a custom Event that attaches itself to DoStore and calls Trigger when DoStore is invoked. That works for me for now, but it would be awesome if those Events were auto-generated like the Units for Store and Dispense are.

Bolt Version:
Unity Version:
Scripting Backend:
.NET Version (API Compatibility Level):
Under Review

That's a very interesting idea, but I'm not sure it's possible.

I'd have to look into generating "catch-all" delegates for any event signature that would allow Bolt to hook into them.

If that's possible (and I'm not sure it is), it would probably also require a new type of AOT stubbing to work properly on AOT platforms.

I'll do some research!

Thanks for considering it! Obviously this workaround suits me well, but it could potentially simplify a lot of things.

FWIW, you can create generic delegates (https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-delegates) and use reflection to get the inputs and outputs of the delegate's Invoke method (https://stackoverflow.com/questions/429552/can-i-get-the-signature-of-a-c-sharp-delegate-by-its-type). I won't dare to postulate how that would fit into the workings of this feature (or how that would solve the AOT issue), but I figure it might be useful information. :)


Getting the types of the delegates' parameters is the easy part — creating a function to hook into that delegate signature is hard! But I have a few tricks up my sleeve. ;) 


(Sorry for the bump/necro, I'm doing some roadmap grooming following the Unity acquisition!)

This is now supported in Bolt 2 Alpha via reflected member events and delegates.