Basic4GL's variables are stricter than in most programming languages. However, don't worry, they are just as simple. If you've never programmed before, this tutorial will give you an introduction to variables. If you are an experienced programmer starting to use Basic4GL, you should read this tutorial anyway to learn how Basic4GL handles variables.
Variables in Basic4GL Tutorial
Variables are essential to any kind of programming. They are like boxes in which your game temporarily stores information while it is running. They are used to store essential information like a game character's name or hit points.
Data Types
Before getting into variables, here's a short primer on data types.
Most programming languages have three basic data types. They are :
- string
- Used for dialog, character's names, and other things that require letters. For general purposes, you can think of a string as simply text.
- integer
- An integer is a whole number such as 33, 25, -234.
- float
- A floating point number is a number with a decimal point such as 33.44, -322.001, etc. You can think of the decimal point as the "floating point."
Variables in Basic4GL are typed. That is, there are three kinds of variable and each one can hold one type of data. There is one type of variable for each data type.
Creating Variables
You create variables with Dim. Unlike most forms of BASIC, you must create your variables with Dim before you attempt to use them. Below is an example of code that will create a variable of each data type :
Dim aString$
Dim anInteger
Dim aFloat#
Notice that the string has a dollar sign at the end and the float has a # at the end. These determine the data type of the variable. If it has no data type sign, it is assumed to be an integer variable.
You can also create variables without data type signs by declaring the data type itself when you dim them :
Dim aString As String
Dim anInteger As Integer
Dim aFloat As Single
When you create your variables this way, you do not need the data type sign.
Storing Information
Variables store information. This is how you store information in a variable :
' create variables first
Dim aString As String
Dim anInteger As Integer
Dim aFloat As Single
'then store some data
aString = "Hello, I'm a String."
anInteger = 11
aFloat = 11.99
'display the contents of each variable
Printr aString
Printr anInteger
Printr aFloat
Changing Values
The value of a variable is not fixed. You can change it at any time. Below is an example with a string variable.
Dim characterName As String
characterName = "Bob"
Printr characterName
characterName = "Leroy"
Printr characterName
Conclusion
You have now learned 90% you will ever need to know about variables in Basic4GL.