Lambda - GrandeurHammers/Overwatch-Script-To-Workshop GitHub Wiki
In the workshop, EffectRev (and other similar values) cannot be stored. The example below allows different components of a project to store EffectRevs using the StoreEffectRev
enum then use ConvertRevToRaw
to convert the StoreEffectRev
to the workshop constant EffectRev
.
rule: "Make Effect"
Event.OngoingPlayer
if (IsButtonHeld(EventPlayer(), Button.Interact))
{
// Get a random color.
StoreEffectRev randomRev = <StoreEffectRev>RandomInteger(0, 12);
// Create the effect.
ConvertRevToRaw(randomRev, (EffectRev rev) => {
CreateEffect(AllPlayers(), Effect.Sphere, Color.White, EyePosition(EventPlayer()), 1, rev);
});
}
void ConvertRevToRaw(ref StoreEffectRev rev, BlockLambda<EffectRev> make)
{
// Convert StoreEffectRev to the workshop constant EffectRev.
switch (rev)
{
case StoreEffectRev.VisibleToPositionAndColor: make.Invoke(EffectRev.VisibleToPositionAndColor); break;
case StoreEffectRev.VisibleToAndPosition: make.Invoke(EffectRev.VisibleToAndPosition); break;
case StoreEffectRev.Position: make.Invoke(EffectRev.Position); break;
case StoreEffectRev.PositionAndColor: make.Invoke(EffectRev.PositionAndColor); break;
case StoreEffectRev.VisibleToAndColor: make.Invoke(EffectRev.VisibleToAndColor); break;
case StoreEffectRev.Color: make.Invoke(EffectRev.Color); break;
//...
}
}
enum StoreEffectRev
{
VisibleToPositionAndColor, VisibleToAndPosition, Position, PositionAndColor, VisibleToAndColor, Color
}
The 3 lambda types BlockLambda
, ValueLambda
, and MacroLambda
can all be executed by running the Invoke
method. The amount of parameters Invoke
requires depends on how many parameters the type is defined with. For the example above, BlockLambda<EffectRev>
takes one parameter of type EffectRev
. More parameter types can be added by separating the types with commas, for example: BlockLambda<define, Effect, EffectRev>
.
Lambdas are constant types. This means that variables with any of the lambda types cannot be changed, similarly to workshop constants like EffectRev
. If this is something you need to do, consider using the listener pattern instead of lambdas.
There is no limit to the amount of parameters a lambda can have. For the examples below, only 2 are used.
A block of code that does not return a value.
() => {
}
// Without parameters
BlockLambda parameterLess = () => {
};
// With parameters
BlockLambda<t1, t2> withParameters = (t1 p1, t2 p2) => {
};
A block of code that returns a value.
() => {
return expression;
}
// Without parameters
ValueLambda<returnType> parameterLess = () => {
return expression;
};
// With parameters
ValueLambda<returnType, t1, t2> withParameters = (t1 p1, t2 p2) => {
return p1 + p2;
};
An expression that returns a value. This is useful for reevaluation.
() => expression;
// Without parameters
MacroLambda parameterLess = () => expression;
// With parameters
MacroLambda<returnType, t1, t2> withParameters = (t1 p1, t2 p2) => expression;
Lambdas are constant workshop types, meaning lambda variables cannot be changed. The pattern below introduces a different strategy.
void Example()
{
ListenerBase alternateLambda = new UpdateListener();
// 50% chance to change the "lambda".
if (RandomInteger(0, 1) == 0)
alternateLambda = new AnotherUpdateListener();
// Call the lambda.
alternateLambda.Call();
}
class ListenerBase
{
public virtual void Call() "Call Listener" {}
}
// Potential lambda 1
class UpdateListener : ListenerBase
{
public override void Call()
{
SmallMessage(AllPlayers(), "Update called!");
}
}
// Potential lambda 2
class AnotherUpdateListener : ListenerBase
{
public override void Call()
{
SmallMessage(AllPlayers(), "Another update called!");
}
}