Variables are a very powerful thing. They are what give a programming language it's overall flexibility. Without variables, programming languages wouldn't know what to do with themselves, LITERALLY.
What is a variable?
A variable is basically a container. It can contain just about anything. It can hold numbers, strings, etc. Then you have things like boolean variables, which are true and false switches.
What can you do with a variable?
You can do a lot with variables. In stencyl specifically, you can use them to store values that get passed around from behavior to behavior. In my last post, I showed how you could create a basic power up that boosted the players movement speed for 10 seconds. We achieved this by passing values back and forth in the form of variables.
You could also use variables to create a level up system for you characters. In fact, you couldn't achieve something like that without variables.
The list is endless.
Understanding variable scope
Depending on what programming language you are using, there are several different scopes to consider. In stencyl, we only need to be concerned with two - Local and Global.
A local variable is something that usually only affects one small part of the script. For us, that could be one actor. If you create a local variable inside of an actor behavior, it can only affect that actor. In fact, in the stencyl program, you wouldn't even be able to access that variable outside of that actors behavior editor.
A Global variable is a variable that we can create that we can apply to several aspects of the game. We can take that variable and apply it to any number of characters or scenes in our game.
How do I create global variables?
In Stencyl, global variables are referred to as Game Attributes. To create them, click on the Game Attributes tab, then click the grey button that says "create new game attribute".
Now, you should see this pop up window:
As you can see, there are a handful of options here to choose from. Take your time naming variables. I can't even express to you how many times I have forgotten what a variables intended use was. Make sure that you name yours something that you will remember what they are used for!
The next thing to do is to be sure that you click the right button based on what you intend for your variable to contain. If it is meant to hold a number value, then make sure that you click number. If it is meant to hold your characters name, then make sure that you click text. You get the point.
How to use Game Attributes
Now, that you know how to create a Game Attribute, I'll give you a quick guide on how to use them. I will also expand on this in the next couple of lessons.
This is the block that I used in my last post. It shows how you can use a boolean game attribute as a trigger. In this example the trigger was pulled when the player ran over a power up. Once it was triggered it doubled the players movement speed for 10 seconds.
Now you know how to create and use game attributes. In the next couple of lessons, I will show you more advanced ways to use these variables to create more flexible games. I'll see you in the next post!
If you find these Stencul tutorials helpful then please feel free to subscribe to my feed burner. If you have a lesson that you would like me to create then please let me know in the comments.
thx
ReplyDelete