Dynamic variables, commonly shorted to dyn vars or dynvars, is a system of data storage wherein one can store arbitrary, scoped data under slot heirarchies with arbitrary names, akin to that of an associative array. Their usage is usually found in large systems with many moving parts, but can nonetheless be useful as easy "global" values that can be changed on an object.
Usage
Dynamic variables as a whole are managed with two parts: dynamic variable spaces and dynamic variables. The DynamicVariableSpace component defines a dynamic variable space, while any one of the DynamicValueVariable, DynamicReferenceVariable, DynamicTypeVariable components define a dynamic variable. Dynamic variable spaces and names must not contain symbols, punctuation, or whitespace, except for period (.
), underscore (_
), and space (
). To check if a character is unable to be used in a dynamic variable name, one can use the Is Symbol, Is Punctuation, and Is White Space ProtoFlux nodes, taking care of the three exceptions above.
The process of a dynamic variable being associated with a given space is called binding. The VariableName
of a dynamic variable component can be one of the following two forms: VariableName
or VariableSpaceName/VariableName
. The former represents indirect binding, while the latter represents direct binding. A dynamic variable component will traverse up the slot heirarchy, including its current slot, looking for an applicable variable space to bind to. If a dynamic variable is indirectly binding, it will bind to the first dynamic variable space that does not have OnlyDirectBinding
set to True
. If a dynamic variable is directly binding, it will bind to the first dynamic variable space that matches its name. If a dynamic variable does not find a dynamic variable space that it can bind to, it will not be accessible outside of the component itself, essentially reducing to a glorified ValueField. A dynamic variable will go through this binding process every time any part of the component changes.
Dynamic Fields
It is also possible to interface with an existing IField as if it were a dynamic variable. This is done by attaching a DynamicField component (for IValue types), DynamicReference component (for SyncRef types), or DynamicTypeField component (for the Type type), then dragging the target field into the TargetField
field.
Reading and Writing
Dynamic variables can be read in any fashion one sees fit, such as copying from the variable field, sourcing the variable field, or using the Read Dynamic Variable node. Dynamic variables can be written to via the Write Dynamic Variable or Write Or Create Dynamic Variable nodes. Additionally, there exists DynamicValueVariableDriver and DynamicReferenceVariableDriver components. These can be used to drive other fields with the value of a dynamic variable.
Driving Dynvars
Driving dynamic variables must be done with caution. For one, one must drive the value field of the dynamic variable, which incurs the 2+ update delay regarding indirectly writing to dynamic variables. Additionally, if a dynamic variable is being driven, it is crucial that all instances of the dynamic variable are driven by the same value. Otherwise, clients will fight over which value is the "true" value of the dynamic variable and cause inconsistent behavior.