-
Notifications
You must be signed in to change notification settings - Fork 34
Delegates
Delegates are a way to create and invoke methods by reference. As the name suggests, a Delegates main intention is to delegate the behavior amongst one or many other methods. This allows one variable to bind and unbind other matching methods, then invoke them based on that single reference. These can be created and bound at runtime, to switch and assign new behavior to variables and third party scripts. This is perfect for modularity and abstraction!
You may have seen some inaccessible ports before. Perhaps, labeled as "callback", "getter", "setter", "On Click", and so forth. The only way to utilize these, are to create a C# script for them. We have developed a set of units and a wrapper that does this for you. You create a Delegate Asset that can generate native C# wrappers that the delegate units use.
There are two types of delegates:
Actions - A delegate with no return values. These must be a type of void.
Funcs - A delegate with a return value.
Each delegate has a Delegate Unit, an Invoke Unit, and a Bind and Unbind Unit. These units are auto populated into the Community > Control > Delegates
folder in the Fuzzy Finder, under the delegate types Namespace.
To ensure all delegates work the same on all platforms, it was decided, generating to C# was the best option. In order to utilize a delegate, you must first create a new delegate asset to compile a wrapper, for use in graphs. Selecting a type will be based on what units you are using, and their requirements. You will be able to find them by inspecting the unit port for its type.
In the Project Window, right click and select Visual Scripting > Community > Delegate
to create a new Delegate Asset.
If a type has generic parameters, you will be required to fill out each parameter below the type. You are not required to know the types allowed for the constraint. They are automatically filtered based on the constraint type.
If you are satisfied with what you've done, you may compile using the Compiler Utility. All Delegates C# scripts, are compiled into "Assets/Bolt.Addons.Generated/Scripts/Delegates"
folder. You may move the C# file out of the folder and delete the asset if you choose. The scripts folder is deleted every compilation, and rewritten again. Therefore any manual changes will not retain unless moved.
You do not need to regenerate units. All uses of the wrappers and the populated fuzzy finder, are dynamic.
To create a delegate of your wrapped delegate type, it will be labeled as the type name, then the parameters in parenthesis. For instance, an Action<int, Vector2> is displayed in the fuzzy as Action (int, Vector2). You may change the units type be selecting the unit, and looking at the inspector, then choosing a new one from the drop down.
- Overview
-
Unit Reference
- Control
- Editor
- Events
- Graphs
- Logic
- Time
- Variables
- C# Generation
- Editor Windows
- Graph Context
- Utilities
- Custom Unit Creation Guide
- Tips & Tricks