Lambda Commands
In most cases, you should add commands using the [Command]
attribute which can be used on methods, properties, fields and delegates, however there may be cases where you want to generate commands at runtime from a lambda. In this case, the LambdaCommandData
type may be used to create a command at runtime from a lambda.
Tip
The MonoTargetType
does not need specifying, as the command will hold a reference to the lambda's target, which will support capturing variables just like lambdas normally do.
Once a LambdaCommandData
has been created, it can be added to Quantum Console using QuantumConsoleProcessor.TryAddCommand
.
The following is an example that creates 10 unique lambda commands, each capturing a different value.
using QFSW.QC;
...
for (int i = 0; i < 10; i++)
{
int value = i;
Func<int, int> func = x => x + value;
LambdaCommandData command = new LambdaCommandData(func, $"lambda-add-{i}");
QuantumConsoleProcessor.TryAddCommand(command);
}
Binding Non-static Methods
Lambda commands can also be used to bind non-static methods into commands with the invoking instance automatically captured. Lambda commands can be created like this just like when using lambdas.
Tip
Due to limitations in the C# type system, the function must be cast to a strong delegate type like Action
or Func
first even if the editor states that it is redundant.
Warning
Due to limitations in the C# type system, lambda commands, unlike standard commands, cannot be created directly from generic methods.
using QFSW.QC;
using UnityEngine;
public class LambdaMethodCommandExample : MonoBehaviour
{
private void Start()
{
LambdaCommandData command = new LambdaCommandData((Func<Vector2>)GetPosition, $"get-pos-{name}");
QuantumConsoleProcessor.TryAddCommand(command);
}
private Vector2 GetPosition()
{
return transform.position;
}
}
Adding Suggestor Tags
In order to use the suggestor tags system, the lambda command should be created from either a non-static method or a local method; using either of these over an anomynous lambda will allow you to apply suggestor tags to the parameters as normal.