This tutorial will introduce you to variables.
What is a Variable?
Variables are containers that hold data while your game is running.
Variables are commonly used to store a character's hitpoints and position on the screen.
Creating a Variable
To create a variable, just give it a value.
The code below puts the number 42 in the variable secretNumber.
secretNumber = 42
Print secretNumber
Sync
WaitKey
Setting a Variable
When you create a variable, you are also setting it's initial value using the assignment operator =.
To change the value of a variable, just use the assignment operator again.
secretNumber = 42
Print secretNumber
secretNumber = 24
Print secretNumber
Sync
WaitKey
Variables can also store a calculated value :
mathResult = 4 + 2
Print mathResult
Sync
WaitKey
Variable Types
There are three primary kinds of variables, distinguished by the data they can hold.
Integer Variable
An integer variable contains a number without a decimal fraction.
Integer variables have no symbol after their name.
integerVar = 42
Float Variable
A float variable contains a number with a decimal fraction.
Float vars have the # symbol after their name.
floatVar# = 42.24
String Variable
A string variable contains a string, that is, a bunch of characters and symbols you'd use in the name of something or a message to the player.
String variables have the $ after their name.
stringVar$ = "I'm a string!"
Next Steps
How To
| Categories: Beginner Tutorials : Data : Data Structures : Programming Tutorials : Tutorials : Variables |