As with any scripting language, variables are the basics. It is the way to collect, change, set and pass values into functions. There are several basic variable types available:
| TYPE | DESCRIPTION | KEYWORD |
|---|---|---|
| Integer | Whole number (positive, negative and 0) | int |
| Floating point | Number with decimal places (positive, negative and 0.0) | float |
| String | Series of characters (word, usually keyname) | str |
| Boolean | Binary true/false | bool |
| Array | Collection of variables (of the same type) | array |
You'll mostly be working with the basic types (int, float, str, bool). Arrays are explained in a separate article.
Initializing variables and assigning values[]
Before you can use a variable, you have to initialize it. The process for the simple variables is the same with each, so we'll bundle them together.
int integerVariable = 1 ; float floatVariable = 1.0 ; str stringVariable = someTextValue ; bool booleanVariable = true ;
Several naming rules of importance:
- Variable names CANNOT have empty spaces. Either use camelCase or "_"
- Don't use special characters like $, #, % etc. Use only letters and numbers*
- It is good practice to assign a value to a variable on initialization, although it isn't required
*There is one exception to this rule and that's regarding the use of the special character "$". This character is EXTRA special because it signifies the variable whose name starts with $ is a global variable and WILL BE SAVED! See Global variables for more information.
Setting variable value[]
Variables wouldn't be of much use if you couldn't store values in them. This is called assigning values and it goes like this (when the variable has already been initialized before):
int a = 5 ; a = 2 ;
While "a" was initialized with the value of 5, we changed the value to be 2 afterwards. The same can be done with the other variable types. Notice however that in the second line, "a" was NOT preceeded by the keyword "int". You CANNOT initialize the same variable twice in the same script. This produces an error. Additionally, you CANNOT use a variable before initializing it. This will also produce an error.