Counters
A Counter is a variable used to count things - numbers of button presses, number of coins picked up, number of fires lit, number of mountains climbed. In most programming languages, you'd see lines like this:
// whenever you pick up a coin
numberOfCoins = numberOfCoins + 1
This is called incrementing a variable. In Ygdrasil, you can do this with the value node's increment message to easily create a counter variable.
value counter (when(changed,print("you have pressed the button $value times")))
wandTrigger (when(button1,counter.increment))
value counter (
when(changed,print("counter = $value"))
)
wandTrigger (
when(button1,counter.increment),
when(button2,counter.decrement)
)
// click the button to inflate the balloon
wandTrigger (
when(button1,counter.increment)
)
value counter (
when(changed,balloon.size($value))
)
object balloon (file("balloon.pfb"))
Min, Max, and Delta
That balloon grows awfully fast - the size goes quickly up to many times its original size with just a few button presses. One way we can make it grow a little slower is by changing the way the counter works. Instead of adding 1 with each button press, we might want it to add a smaller value, like 0.2 so that the size would go 1, 1.2, 1.4, 1.6, 1.8, 2.0, etc ...
You can do this by setting the delta for the value node - the amount to add with each increment.
wandTrigger (
when(button1,counter.increment)
)
value counter (
value(1.0),delta(0.2),
when(changed,balloon.size($value))
)
object balloon (file("balloon.pfb"))
wandTrigger (
when(button1,counter.increment)
)
value counter (
value(1.0),delta(0.1),min(1.0),max(3.0),
when(changed,balloon.size($value))
)
object balloon (file("balloon.pfb"))
Automatically Deflating the Balloon
This code will make the baloon deflate automatically after a certain amount of time. Rather than use timers, we'll do this just by using YG's Message Delays. Whenever the counter changes, wait 2 seconds and then decrement it. Note that this will cause it keep decrementing, because each decrement will trigger the changed event again!
wandTrigger (
when(button1,counter.increment)
)
value counter (
value(1.0),delta(0.1),min(1.0),max(3.0),
when(changed,balloon.size($value),decrement+2)
)
object balloon (file("balloon.pfb"))
(c) Ben Chang