Introduction to Variables
Have you ever wanted to make a specific object appear in your Dream on command, keep track of inventory, make a locking door, be able to tell if a game is in progress or not, have an on and off switch with just a command, or even keep track of visitors and dream stats?
Well, variables let you do just that! Variables are really handy, and once you understand how to use them, they will open all kinds of opportunities for you with DragonSpeak in your Dream!
But if you're new to DS, make sure you've read the Beginning DragonSpeak tutorial and worked with a bit of DS first. Another good tutorial is the Intermediate DragonSpeak tutorial. Once you have familiarized yourself with basic DS manipulation, it'll be a good time to learn a bit about variables.
Table of Contents
Tutorial 2: Spawn a specific floor
Tutorial 3: Making a locking door
Tutorial 4: Utilizing coordinates
What are variables?
Most broadly, variables in DS can be thought of as a way to label and organize numbers. Think of them like a container that you put numbers in. The text that you use to describe the number is the name of the variable, just like a label you put on a container describes what is inside of it. Additionally, containers can have their contents changed and moved or shuffled around. Similarly, variables can have their numbers changed dynamically.
This metaphor of a container is important because the label you choose for it is really important to describe what kinds of things you're putting in that container. A jar that says Pens on it is great until you see you have a misplaced pencil. In this case, Writing Utensils would be a better label for that jar. You can also write Bloopersmorgasbeep on the label for the container, but then only you would know what is supposed to go in that container, and you'll probably forget later on. The same is true for variables.
So in other words, variables are just words that represent a number. Specifically, they represent integers, which just means non-fraction or non-decimal point numbers.
So, how do we make a word represent a number in DragonSpeak? You can make any string of text represent a number by beginning the text with the percent symbol %. For instance, %JoesHotdogs is a variable, and so is %amountofbones.
%JoesHotdogs can be 20, 500, or 0 if Joe doesn't have any more hotdogs left. %amountofbones is the same, it can be (almost) any number as well. These labels (variables) can keep track of what the number is.
Tutorial 1: Emit a number
To start as simply as possible, let's say you wanted Joe the Doggo to notice anything you say that has a number in it and the word hotdog, and then yell out to everyone in your Dream. He's keeping track, after all, and wants to make sure you are too!
It's tempting to start this off like this:
(0:32) When someone says something with {hotdog} in it,
(5:204) emitloud message {Joe the Doggo is worried there are only a couple of hotdogs left!} to everyone on the map.
And once this is in your DS file, every time you talk about hotdogs, he'll shout out about it to everyone. If you say: I want to eat 10 hotdogs!
He'll emit: Joe the Doggo is worried there are only a couple of hotdogs left!
But the problem is that he'll emit that there's only a couple of hotdogs every time, even if you say there's 1000 hotdogs in the fridge.
We can actually address this with DS. We can have him to keep track of how many hotdogs you're talking about. This is where variables come in handy:
(0:32) When someone says something with {hotdog} in it,
(5:314) set variable %JoesHotdogs to the number the triggering furre just said.
(5:204) emitloud message {Joe the Doggo is worried there are only %JoesHotdogs hotdog(s) left!} to everyone on the map.
Now when you put that in your DS file and reupload your Dream, you will notice that if you say something like: I think there's probably only 4 hotdogs in the fridge...
Joe the Doggo will then say: Joe the Doggo is worried there are only 4 hotdog(s) left!
He'll also repeat any other number you want to try as long as it's not a fraction -- if it is he'll just cut off the decimal points. 4.3432 becomes just 4. (And yes, he'll still be worried about how many hotdogs you have regardless of how many you say there are because he's a hungry good boy.)
Tutorial 2: Spawn a specific floor
So, where else can you use variables? Well, variables can stand in the place of most anywhere in DS where a number is expected. They work as substitutes for numbers, since they contain information about numbers. And as you learned in the Beginner DragonSpeak tutorial, there's a lot of places where you need to put a number in for the # sign. So in other words, wherever you see a # symbol in a DS line, a variable can be used instead!
So, let's say you want to be able to make a change in your Dream without having to reload it every time. You can actually do that with DS by using a variable!
Here's the DS lines as they are:
(0:32) When someone says something with {...} in it,
(5:314) set variable # to the number the triggering furre just said or emoted.
(5:1) set the floor to #.
For this example we'll change the ... to be floor, and then we'll change both of the #'s to be a variable instead of just a number. Since we want to be able to pick different types of floors, the name of our variable can be %floortype. Here's the DS lines with the words swapped:
(0:32) When someone says something with {floor} in it,
(5:314) set variable %floortype to the number the triggering furre just said or emoted.
(5:1) set the floor to %floortype.
Once you upload this Dream, you can say any number you want that exists as a floor and the DS will swap the floor for you. You can check the floor numbers if you look at the Dream Editor under the Floor category.
In your Dream, try saying something like: I want the floor to be grass, which is 1 in the Dream Editor.
Wow, the entire Dream's floors changed, didn't they! Remember, the default area in Furcadia is everywhere on the map, so if you want the floor to be in a specific place, don't forget to add in an area. Here's an example:
(0:32) When someone says something with {floor} in it,
(3:5) where the triggering furre was at,
(5:314) set variable %floortype to the number the triggering furre just said or emoted.
(5:1) set the floor to %floortype.
Now when you pick a floor to swap, it'll only change where you're standing instead of all the floors in your whole Dream. You can add more types of things to swap in your Dream as well, like items, walls, effects, regions, etc.! You'll have the next big build a home Dream before you know it!
Tutorial 3: Making a locking door
Sometimes when you're making a Dream, you want players to be able to seclude themselves in a cool room you made. Yes, usually the kitchen is a free for all, but maybe you want to be able to lock the staff room, or the bedroom! You can use variables as a way to keep track of whether a room is locked or not.
So for this example, we will create a room that can be locked at will and at any time, by anyone. In this case, a variable will serve as a virtual lock for a room within our Dream!
For this example, we will make a variable called %roomLockStatus and use its value to determine whether or not our room is locked. If %roomLockStatus is 1, the room will be locked, and if it's 0, then it's unlocked:
(0:31) When someone says {Lock},
(5:300) set variable %roomLockStatus to the value 1.
(0:31) When someone says {Unlock},
(5:300) set variable %roomLockStatus to the value 0.
With these two triggers, every time that a player says the word lock, the %roomLockStatus variable will be automatically set to the value 1. Meanwhile, every time a player says unlock, the variable will be set to 0 instead, which completes the locking and unlocking mechanism of our lock.
For this tutorial, our lockable/unlockable door is at the coordinate 8,9, and the secret room is at 16,8. Feel free to adjust these coordinates in your own map according to where you'd like your door and where you want the player to arrive.
(0:7) When someone moves into position (8,9),
(1:206) and variable %roomLockStatus is not equal to 1,
(5:15) move the triggering furre to (16,18), or to someplace nearby if it's occupied.
With this trigger, any player that tries to enter the room while it is unlocked will be automatically teleported to the interior of the room. Our variable %roomLockStatus is keeping track with the condition (1:206) and variable %roomLockStatus is not equal to 1 line of DragonSpeak, a condition that will be met as long as the %roomLockStatus variable is not equal to 1.
Now, you may be wondering about what happens when the room is locked, which the following trigger is in charge of handling:
(0:7) When someone moves into position (8,9),
(1:200) and variable %roomLockStatus is equal to 1,
(5:200) emit message {You can't enter the room, the door is locked.} to the triggering furre.
This trigger is rather similar to the previous one in its structure, however, the status of our variable-powered lock is symbolized by the condition (1:200) and variable %roomLockStatus is equal to 1, which will be met as long as the %roomLockStatus variable is equal to 1.
With the previous two triggers in place, our door now has two different sets of behaviors, one where the player will be allowed to go into the room if it is unlocked, and another where the player will be told that the room is locked and won't be teleported into the room.
Now, all that there is left to take care of is the behavior of the door when exiting the room, which, for this particular tutorial, will allow any player to exit the room freely whether the room is locked or unlocked:
(0:7) When someone moves into position (16,17),
(5:15) move the triggering furre to (8,10), or to someplace nearby if it's occupied.
Now our room is fully functional and can be locked and unlocked at will by anyone, preventing players from going into the room if it is currently locked, but allowing players to exit the room freely!
Here are the DragonSpeak lines without any modifications:
(0:31) When someone says {...},
(5:300) set variable # to the value #.
(0:31) When someone says {...},
(5:300) set variable # to the value #.
(0:7) When someone moves into position (#,#),
(1:206) and variable # is not equal to #,
(5:15) move the triggering furre to (#,#), or to someplace nearby if it's occupied.
(0:7) When someone moves into position (#,#),
(1:200) and variable # is equal to #,
(5:200) emit message {...} to the triggering furre.
(0:7) When someone moves into position (#,#),
(5:15) move the triggering furre to (#,#), or to someplace nearby if it's occupied.
Tutorial 4: Utilizing coordinates
Even though coordinates contain two sets of numbers, variables can keep track of them, too! Let's say we have a wizard themed Dream and we want our wizard players to be able to create magical waypoint portals at will as long as they cast the right spell. These portals will let others in the Dream be able to teleport to the waypoints the wizards made.
To accomplish this, we will create a magical teleportation spell that will allow anyone to set up a waypoint portal. Then, anyone can teleport to this portal if they say the word teleport!
First, we need to create the trigger that will power the magical keyword allowing the wizard to set up the coordinates for the waypoint:
(0:31) When someone says {Waypoint Wazaam!},
(5:350) set variable %waypointLocation to the X,Y position the triggering furre (moved from/is standing at).
(3:5) where the triggering furre was at,
(4:8) only where there are no items,
(5:4) place item 261.
This trigger will make it so that when a wizard says the secret spell Waypoint Wazaam! the variable named %waypointLocation will be set to the coordinates of the tile that the wizard is currently standing on. For example, if the wizard is currently standing on the tile that is at the X,Y position 8,9 of our Dream, the variable %waypointLocation will then reflect that same position. And as long as the wizard didn't pick an area that already had an item on it to put their portal, we'll also have the magic portal itself be visualized for people to teleport through.
Now we need to create the trigger that will let people teleport to the waypoint the wizard made:
(0:31) When someone says {Teleport},
(5:15) move the triggering furre to (%waypointLocation), or to someplace nearby if it's occupied.
(4:3) only where an item 261 is,
(5:4) place item 0.
This trigger will make it so that when a player says the word teleport, they will be automatically transported to the current position of the waypoint. Alakazam!
The DS lines without any modifications are:
(0:31) When someone says {...},
(5:350) set variable # to the X,Y position the triggering furre (moved from/is standing at).
(3:5) where the triggering furre was at,
(4:8) only where there are no items,
(5:4) place item #.
(0:31) When someone says {...},
(5:15) move the triggering furre to (#,#), or to someplace nearby if it's occupied.
(4:3) only where an item # is,
(5:4) place item #.
Hopefully now that you've been through some of these tutorials you have a better grasp on how to use variables in your own Dream and DS! Now that you know how they work, it may be a good time to check out the Introduction to String DS. If you'd like to see advanced uses and a full spec about variables, you can read over the Advanced DragonSpeak Reference page for more.
DragonSpeak is a wonderful way to add magic and life to your Dreams, so feel free to explore and try new things with your own ideas. If you have any further questions about it, feel free to contact the Beekins in game by saying help I need a mason!
Share this:
-
-
-