What are Delegates?
Before we begin scripting our actions, I wanted to explore the concept of Delegates, as they are an important part of the process. You don’t need to completely master the subject to reap the Input System Package's benefits, but I recommend having a sense of them.
Objective: Learn the definition and syntax of a Delegate, Delegate Signatures, and how to use them.
Table of Contents
Definition and How to Create
Delegates are Variables that store methods. They are declared by using the “delegate” keyword.
We must also include a method signature, which defines the type of method being stored. A method signature consists of the method name and its parameter(s).
In this example, we’re creating a method to change an object’s color.
How to Use Delegates
To use a Delegate, we must create a variable of type Delegate that matches the Delegate’s method name.
- ChangeColor is the delagate variable type
- onColorChange is the new variable that will allow us to use the delegate
Keep in mind that delegates can only be assigned to functions with a matching parameter. Let’s create a function with those requirements in mind.
Now you can simply assign your delage just as you would with a variable.
This newly initialized delegate UpdateColor, can be called just as you would with a regular method.
To verify that our delegate is working, we can call the UpdateColor method and print the results. This won't visibly change any colors, but if everything is functioning correctly, it will output a value of 1 for the corresponding RGB channel—indicating that the delegate executed successfully.
Hope this article was helpful! Check out my portfolio! Looking for opportunities in the video game industry:)