Parameters
Templates can accept parameters which is the preferred way to get data into them. Parameters can be either declared
directly in a template or using a from
declaration.
There are two types of parameter declarations: required
and optional
. Required parameters cannot define a
default value, but optional parameters can. If a required parameter is not provided when a template is used the
compiler will show an error.
Parameters can either define a new field on a template or use a feature called from
to alias another expression.
Required parameters must be defined before optional ones. The order in which you define your parameters is also
the order in which a caller must provide them if not explicitly referring to them by name. When using implicit
parameter passing, the rules follow those of C# Named Arguments
except that we use an =
instead of :
like C# does.
template ParameterExample {
required Vector3 vec; // define a required parameter
optional float value; // define an optional parameter
optional string name = "EvolveUI"; // define an optional parameter with a default value
optional float valueX from stateVector.x; // map this parameter to stateVector's x field
optional float valueY from stateVector.y = 3.14159f; // map this parameter to stateVector's y field, with a default value
state Vector3 stateVector;
// private parameters are not visible outside of the template definition. A caller can still pass them
// into the template but they cannot be used with the `sync` or `onChange` declarations and cannot be extruded
optional:private string secret;
}
template ParameterUsage {
render {
// with explicit parameter passing
ParameterExample(vec = new Vector3(), valueX = 10, valueY = 11);
// with immplicit parameter passing (vec3, value, then explicitly valueY because it skips over other optional parameters)
ParameterExample(new Vector3(), 10f, valueY = 11);
}
}