Variables and Data Types

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

Ready for the harder stuff?

In any application, the data such as the player's stats, the high scores, the rpg character's magic powers, and amongst a bundle of other things, must be stored in memory. Memory is stored in little things known as "variables". Variables are very important and are found in every program.

Variables

There are many types of variables:

  • Integers - Integers are 16-bit(32-bit in FB) variables that hold numbers. Holds values -32768 to 32767.
  • Long - A 32-bit variable that hold even longer numbers. In freeBASIC its the same as Integers.
  • Single - A 32-bit variable that holds decimals in a relatively precise fashion.
  • Double - A 64-bit variable that holds decimals in a very precise fashion.
  • String - A variable that holds an array of characters, or words put together. This is dynamically changed according to how long the string is. A string must be enclosed in double quotes, or else the program will assume it is a variable.

There are three ways to create and initialize variables. Using the let or dim commands, or simply creating the variable. A variable must begin with a letter or underscore, cannot include math operators and cannot be the name of a command.

dim myString = "my first string"
 
or
 
let myString = "my second string"
 
or simply
 
myString = "my third string!"

Variables that are integers do not need double quotes.

Arrays

An array is a matrix consisting of numerical numbers that represent variables. Basically, they are memory locations that are used to record data that are similar. For example, let's say that you are recording team names in baseball. You could create an array that has the numeric labels 0 to 49. The variables that are created with the array are strings that contain the names. These variables are not restricted to strings; they may hold numbers too.

To create an array, you must do the following:

dim baseball_teams