The Value Node
The value node is the key to all of these concepts. The value node itself is used to store data - single numbers, arrays or lists of numbers, or text strings. It serves the same purpose as a variable in most other programming languages - to be able to store and retrieve information. In Ygdrasil, the value node is also the foundation for a large number of other nodes which are used for specific math and logic related functions, such as random number generation, if/then statements, and basic arithmetic.
The core functions of the value node are the value () message, and the when(changed) event. The value() message stores a value in the node, and the when(changed) event can be used to retrieve whenever it changes.
Using the Value Node
In this example, each of the three wand buttons stores a different number in the node. Every time it changes, it prints out the current value in the terminal.
value A (value(0),when(changed,print("current value is $value")))
wandTrigger (
when(button1,A.value(10)),
when(button2,A.value(20)),
when(button3,A.value(30))
)
Let's examine this code. The value node is named "A". The name can be anything, just as for other types of YG nodes. Many of these examples will use node names like "A", "B", "X" and "Y", etc, since they are just dealing with basic math. We use the
value(0) message to
store '0' in the node to begin with. Now, when you press button 1, the wandTrigger tells A to store the number 10 instead. This causes the changed event to happen, and in response, the value node prints out "current value is 10".
The way it retrieved the number 10 is interesting. The value node has a variable named $value, which contains the actual current value stored in the node. Recall the variables which are part of other nodes, like $xpos, $ypos, and $zpos in the userPosition node. This $value variable is similar - it is used to transfer data out from the node when responding to an event. It's important to note that you can only use it when responding to an event. This is very unlike the usual way we think of variables, where we can pull data out from them at any time and from anywhere. The YG messaging structure just doesn't include this possibility.
// in another language, you might say C = A + B, to add two variables and save the
result in a third. Alas, this won't work
value A (value(10))
value B (value(20))
value C (value(A.$value + B.$value))
we'll get to basic arithmetic in a bit; but first, a few other things that you can do with the Value node by itself.
(c) Ben Chang