Dynamic Variables: Difference between revisions

From Resonite Wiki
fix todo
added more details to binding, weakened the warning a bit because binding may actually work (TODO: add a list of situation where it works or not)
Line 41: Line 41:
=== <translate><!--T:23--> Fields</translate> ===
=== <translate><!--T:23--> Fields</translate> ===


<translate><!--T:24--> If you want to use an existing field or reference as the contents of a dynamic variable, you can use the [[DynamicField%601_(Component)|DynamicField<T>]] or [[DynamicReference%601_(Component)|DynamicReference<T>]] components. Instead of storing something directly, they point at a field that contains a value or reference type, respectively. The specified field storing a reference or value type will automatically update it's value/reference to every other variable's value/reference of the same name and under the same variable space.</translate>
<translate><!--T:24--> If you want to use an existing field or reference as the contents of a dynamic variable, you can use the [[DynamicField%601_(Component)|DynamicField<T>]] or [[DynamicReference%601_(Component)|DynamicReference<T>]] components. Instead of storing something directly, they point at a field that contains a value or reference type, respectively.</translate>
 
<translate><!--T:25--> (TODO: clarify value vs. reference types; I think this isn't fully correct)</translate>


=== <translate><!--T:26--> Drivers</translate> ===
=== <translate><!--T:26--> Drivers</translate> ===
Line 50: Line 52:


<translate><!--T:29--> When creating a dynamic variable component, you will be given a list of "common types". If the type you seek is not in that list, you will have to enter it by hand. See [[Complex Types in Components]].</translate>
<translate><!--T:29--> When creating a dynamic variable component, you will be given a list of "common types". If the type you seek is not in that list, you will have to enter it by hand. See [[Complex Types in Components]].</translate>
=== <translate> Binding</translate> ===
<translate>Creating, duplicating, or moving a dynamic variable requires '''binding''' that variable to a space.
This is a process that happens automatically but isn't perfect yet. (see [[#Warning|Warning]])</translate>
<translate>In essence it starts searching for a matching dynamic variable space at the slot of the dynamic variable.
If there is no match, it tries the parent slot, its parent etc. until a matching space is found.
Variables with explicitly given space names only match spaces with the same name.
Variables without explicit space name match all spaces that are not <code>OnlyDirectBinding</code></translate>
<translate>The following image will demonstrate the differences in binding:
[[File:Dynamic_Variables.svg|options|caption]]
Note:
* I: <code>A</code> is connected to <code>Inner</code> because <code>Inner</code> is <u>not</u> <code>OnlyDirectBinding</code>.
* II/III: Both variables are bound explicitly.
* IV: <code>A</code> ignores <code>Inner</code> because <code>Inner</code> is <code>OnlyDirectBinding</code>.
* V/VI: Both variables are bound explicitly.
* VII/IX: There is no matching dynamic variable space. Both variables are not bound.
* VIII: The variable is explicitly bound to <code>World</code>.
</translate>


=== <translate><!--T:30--> Warning</translate> ===
=== <translate><!--T:30--> Warning</translate> ===


<translate><!--T:31--> Creating, duplicating, or moving a dynamic variable requires '''binding''' that variable to its space. This usually takes a small amount of time, before which the dynamic variable can appear to be present, but not be readable or writable. Therefore, if you create a dynamic variable using the [[Create Dynamic Variable`1 (ProtoFlux)|Create Dynamic Variable]] or [[Write Or Create Dynamic Variable`1 (ProtoFlux)|Write Or Create Dynamic Variable]] ProtoFlux Node, or cause it to be duplicated using the [[Duplicate Slot (ProtoFlux)|Duplicate Slot]] ProtoFlux Node, or cause it to be moved using the [[Set Parent (ProtoFlux)|Set Parent]] ProtoFlux Node, you may find it necessary to add an [[Updates Delay (ProtoFlux)|Updates Delay]] or [[Updates Delay With Value (ProtoFlux)|Updates Delay With Value]] ProtoFlux Node afterwards in order to ensure the dynamic variables have been bound by the time you use them. A delay of 1 to 3 updates usually suffices.</translate>
<translate><!--T:31--> In a few use-cases binding may take a small amount of time, before which the dynamic variable can appear to be present, but not be readable or writable. Therefore, if you create a dynamic variable using the [[Create Dynamic Variable`1 (ProtoFlux)|Create Dynamic Variable]] or [[Write Or Create Dynamic Variable`1 (ProtoFlux)|Write Or Create Dynamic Variable]] ProtoFlux Node, or cause it to be duplicated using the [[Duplicate Slot (ProtoFlux)|Duplicate Slot]] ProtoFlux Node, or cause it to be moved using the [[Set Parent (ProtoFlux)|Set Parent]] ProtoFlux Node, you may find it necessary to add an [[Updates Delay (ProtoFlux)|Updates Delay]] or [[Updates Delay With Value (ProtoFlux)|Updates Delay With Value]] ProtoFlux Node afterwards in order to ensure the dynamic variables have been bound by the time you use them. A delay of 1 to 3 updates usually suffices.</translate>

Revision as of 21:03, 19 March 2024


This article or section is a Stub. You can help the Resonite Wiki by expanding it.


Overview

Dynamic variables allow you to read and write data by name. This makes it easy to manage data in large systems; every bit of data is clearly labeled, and you can break data into multiple spaces to keep your systems separate.

Naming Restrictions

When using Dynamic Variables there are some restrictions and limitations on naming both spaces and variables within those spaces.

Space and Variable Names cannot contain:

Usage

Optionally, the name can be prefixed by the name of a space, terminated by a /, to choose a specific variable space to use. This is useful to distinguish between unrelated systems that all use dynamic variables.

Some valid names include:

  • Health -- no space defined, named Health
  • World/Color -- named Color, in the World space
  • MyCoolSystem/Score -- named Score, in the MyCoolSystem space

Spaces

Dynamic variables can live anywhere in or under the slot containing a DynamicVariableSpace component.

So, a variable space on the world root can be used from anywhere, whilst a variable space on your avatar can be used from anything stored in your avatar.

Variables

The simplest way to use dynamic variables is to use the DynamicValueVariable<T> and DynamicReferenceVariable<T> components. These are for value (int, float, String, etc.) and reference (Slot, User, etc.) types, respectively.

These components store a value or reference directly. If two variable components have the same name, then they will have identical contents.

Fields

If you want to use an existing field or reference as the contents of a dynamic variable, you can use the DynamicField<T> or DynamicReference<T> components. Instead of storing something directly, they point at a field that contains a value or reference type, respectively.

(TODO: clarify value vs. reference types; I think this isn't fully correct)

Drivers

You can use the contents of a dynamic variable to drive a field or reference, using the DynamicValueVariableDriver<T> and DynamicReferenceVariableDriver<T> components.

Unlisted types

When creating a dynamic variable component, you will be given a list of "common types". If the type you seek is not in that list, you will have to enter it by hand. See Complex Types in Components.

Binding

Creating, duplicating, or moving a dynamic variable requires binding that variable to a space. This is a process that happens automatically but isn't perfect yet. (see Warning)

In essence it starts searching for a matching dynamic variable space at the slot of the dynamic variable. If there is no match, it tries the parent slot, its parent etc. until a matching space is found. Variables with explicitly given space names only match spaces with the same name. Variables without explicit space name match all spaces that are not OnlyDirectBinding

The following image will demonstrate the differences in binding:

caption

Note:

  • I: A is connected to Inner because Inner is not OnlyDirectBinding.
  • II/III: Both variables are bound explicitly.
  • IV: A ignores Inner because Inner is OnlyDirectBinding.
  • V/VI: Both variables are bound explicitly.
  • VII/IX: There is no matching dynamic variable space. Both variables are not bound.
  • VIII: The variable is explicitly bound to World.


Warning

In a few use-cases binding may take a small amount of time, before which the dynamic variable can appear to be present, but not be readable or writable. Therefore, if you create a dynamic variable using the Create Dynamic Variable or Write Or Create Dynamic Variable ProtoFlux Node, or cause it to be duplicated using the Duplicate Slot ProtoFlux Node, or cause it to be moved using the Set Parent ProtoFlux Node, you may find it necessary to add an Updates Delay or Updates Delay With Value ProtoFlux Node afterwards in order to ensure the dynamic variables have been bound by the time you use them. A delay of 1 to 3 updates usually suffices.