| script command: Set Variable |
What this does
| This command stores some text in a variable. It can contain any word, phrase, or number you want. It can also be set to true or false to turn things on or off. |
What is variable? When you want the game to remember something, you give that information a name. Then you can use that name to recall what was stored. | What are variables for? Variables are used to remember stuff, such as a character's name, their HP, or whether a story event has occurred. Variables can even change the game's settings and behavior, such as how fast the player walks.
For example?
What if you want the game to remember the player's name, and re-use it later. This is useful in case you ever want to change the name later, without having to change all the text in the game. |
To do this, you click on the button that looks like this. Then on the right under variable, you type a name for this information. And under value, you type in something for it to remember. From that point on, you'll be able to use the variable name to recall what you typed in. | You'll still be able to change the variable's contents later or even erase the variable itself if you want. To recall the word you typed, you put the variable's name in brackets. So if you want to display a character's name in a text box, you write this: Hi, my name is [name1]. Assuming that "name1" is what you named the variable.
When you play the game, it'll look like this:
Just make sure that you type the variable's name EXACTLY as it was written. Variable names are case-sensitive, | so it's generally a good idea to always make them lowercase. The contents of the variable, on the other hand, can be whetever you want.
Rules for a variable's name
A variable's name should never contain any spaces. Also, the name should not start with a number. Finally, don't use weird characters or punctuation. |
Where can I use variables?
Most script commands allow you to use variables if you type the variable's name with [] brackets around it. Basically, anywhere you can type text. |
Advanced usage:
Using objects:
The set-variable command can also create objects, which can help you organize variables by grouping related ones together. | An object is like a box that can contain a bunch of variables. You need to create an object before you can create variables inside of it.
You create an object like this:
And then you create variables inside of it like this:
To read the value of a variable inside of an object, you type the object's name, a period, and end it with the name of a variable inside it: | [myObject.myVar]
You probably won't need to use objects very much, but accessing a setting or sprite in the game usually requires digging into a pre-existing object. | For example: [SPRITES.player._x]
Likewise, it can also be altered like so:
Doing this would make the player instantly jump to a spot that's 100 pixels away from the left side of the map. And as you can see, objects can contain other objects. |
Pre-existing objects:
The RPG game engine already contains many variables and objects. I won't go over all of them, but the more you know about the game's code, the more you can mess with it. These are the main objects in the game engine: Some of these are actually movieClips. So you can place SWF files into them using the swf script command. For example, you would typically put a menu system into the HUD movieClip this way. The contents of HUD will remain even when you teleport to another level. The PANORAMA, UNDERLAY, and OVERLAY movieClips are automatically cleared between levels. | RAM ROM RANORAMA UNDERLAY SPRITES OVERLAY HUD TRANSITION MUSIC SOUND
Deleting variables or objects:
The set-variable command can delete both variables or objects. You specify the thing to delete, and set the value to \delete, which can be selected from the list. |
Missing variables:
If the variable you're trying to access doesn't exist, the word undefined will be displayed as the result. | If you set an existing variable to the word undefined, it will still exist, but the game will pretend it doesn't. So this isn't a fool-proof way to detect missing variables.
Changing numbers within variables:
If a variable contains a number, you can increase or decrease its value without knowing what the value is. To do this, you create a setVariable command in the script, type the name of the variable, then select the + or - symbol from the box in its settings. The number you type into the value box, will be added to the number currently stored in the variable. Also, adding a negative number is the same as subtracting. |
Calling functions:
Functions are pieces of programming that can be triggered. They can only be created in Actionscript, but the rpgSprite can trigger pre-existing functions, including many that are built into flash itself. For example, you can generate a random number by putting this in the value field of the set-variable command: [Math.random()] The resulting number will get stored in the variable you specified. This also works in text boxes. Not all functions spit out a result, so you can leave the variable box empty if you want. A function is accessed by name the same way a variable is. The difference is that you put paranthesis after the name to trigger its code. If the name you specified isn't actually a function, adding paranthesis won't do anything. | Furthermore, it is possible to send parameters to a function like this: [Math.round(1.6)] This would spit out the number 2. And fnially, it's possible to use a variable as a parameter like this: [Math.round([myVar])] In this case, the value of myVar will get rounded by the function. In the RPG game engine, the most common way to use this feature is to trigger a transition, such as fading out, without having to teleport or leave the current map. Fade-out [TRANSITION.start(transition_fade)] Fade-in [TRANSITION.fadeIn()]
Built-in sprite variables:
Characters and objects in the game have some built-in variables that control their behavior. You access these variables using the word this followed by a period, and then the variable name. this.noCollide If you set this variable to true, the player will be able to walk through this character or object. This variable is normally set to false. | this.overAll If you set this variable to true, then this character will always overlap all other characters, no matter what its position is. This variable is normally set to false. Normally, characters overlap each other based on their vertical position on the map. This variable overrides this.
| Last updated: December 27, 2014 |