Math and Logic in Ygdrasil
Ygdrasil is primarily a scene description language, not a procedural programming language. The building blocks in Ygdrasil are only nodes, messages, and events. It's very easy to describe arrangements of objects, sequences of events, and user interactions, but what about things like variables, functions, if/else statements, loops, and all the other standard components of a programming or scripting language? In Ygdrasil, all of these concepts are in fact still expressed through nodes, events, and messages. The logic of this system can be confusing at first, particularly for anyone accustomed to other procedural programming languages. While some ideas which are trivial in a language like C++ or Lingo are awkward or complicated in Ygdrasil, many of these concepts can be expressed in surprisingly elegant (though often non-intuitive) ways.
Let's consider a few common types of concepts or problems one might encounter. Some of these might seem bizarre, artificial, or arbitrary, but demonstrate fundamental concepts in building more complex interactions and logical systems.
- Allow the user to open a door, but only if they have picked up a key.
- If you have the key, the door opens onto a lovely meadow. If you have the wrong key, it instead opens onto either a lava pit, a Wal-Mart, or reproduction of Duchamp's last sculpture made in glistening chrome.
- Make a balloon object and an air pump object. Every time you squeeze the air pump, make the balloon get a little bigger.
- Collect objects (for example, spinning gold coins). Keep track of how many have been collected. Make something happen when all of them have been collected.
- Create a slot machine. When you hit it, it switches randomly between a number of different objects; if three of the same line up, you win.
- A random teleporter: when you walk into it, you teleport to a random location in the scene
- There are four torches in a dark room. When you light one it adds a little bit of light; but, after a certain amount of time, it will burn out again and need to be relit. Once all four are lit a larger bonfire in the center of the room ignites.
- A sliding control object - one which you can move along one axis, and only within a certain range.
- A knob control object, which you can rotate on one axis - and which affects other things in the scene, like changing sounds or adjusting the size of another object.
- Walking east makes the sky grow lighter. Walking west makes it grow darker.
- User leaves a trail of objects behind them - breadcrumbs, flowers, blood, whatever ...
- There is a troll here. You want to pick up the iron bar and hit it.
- When you drink the potion, you get smaller. (Or, alternately, everything else gets larger).
- You have a large number of objects that you want to control all at once. Say, 1,000 trees that should all grow bigger simultaneously. It's a lot of work to write out 1,000 messages, one for each - isn't there some way to change them all? (hint - yes! but it's pretty weird.)
- You pick up a seed and drop it in the ground to plant it. It grows into a strange flower. The next time your pick it up and drop it, it doesn't grow into a flower (because it already is one), but instead sings to you and demands fresh meat.
(c) Ben Chang