Variables

Variable Guide

Variables hold information (data) about a video game and the game objects in it. Things like a character's name or hit points. Anytime you need you need to reserve computer memory to store a single number or single piece of text, you will store them in a variable.

' Example in FreeBASIC
dim pname as string
pname = "Soliztan Margot"
print pname
sleep
end

Declaration of Variables

In some BASIC dialects, Dim is used to declare (or create) a variable. This associates the identifier with a memory location. In layman's terms, it is simply creating a variable to hold stuff.

Implicit Declaration

Many programming languages allow you to ''implicitly'' declare variables, that is, to have them created during the first time they are used. For example, when you use the assignment operator without first declaring a variable, it is created when this code is run :

Basic4GL has never allowed the implicit declaration of variables.

Explicit Declaration

Some programming languages force you to ''explicitly'' declare variables, that is, to create them before you use them. This is done with DIM.

' FreeBASIC Example
dim location as string
location = "The Grim Hall"

Explicit declaration of variables is usually done at the beginning of your game code.

Explicit and Implicit Declaration

Some programming languages expect you to use implicit declaration by default (especially modern BASICs) and allow you to explicitly declare variables as you so choose. Most of these languages, such as FreeBASIC and Brutus2D allow you to force the explicit declaration of variables if you so choose. In that case, OPTION EXPLICIT can be used.

If OPTION EXPLICIT is used, then any variable not explicitly declared will result in an error. This saves bugs from misspellings because if you misspell the variable's name, OPTION EXPLICIT will cause an error because the misspelling has not been explicitly declared.

Data Type

(Full article : data type)
The "data" stored in variables usually has to be of a specific type. The most common data types are strings and numbers.

String variables are used for holding messages and other things dealing with ''text''.

Numeric variables are used for math-related operations such as hit points.

Local and global variables

Some programming languages allow specification of local and global variables.

Local variables are available for read and write only within given procedure ( function, sub ).

In CBM BASIC there is no such thing as local and global variables. There is basically no variable scope at all.

Global variables are available for read and write from any part of the program.

GLOBAL myGlobalVar AS INTEGER

SUB Procedure1()

  ' -- I can assign to global variable from any where
  myGlobalVar = 1

END SUB

SUB Procedure2()

  ' -- I can assign to global variable from any where
  myGlobalVar = 2

END SUB

Swapping Variable Values

If you want to swap (or switch) the value of two variables, you will need one more variable as a "buffer". First, here's the wrong way to do it :

var1 = 1
var2 = 2
var1 = var2
var2 = var1

This will result in var1 and var2 having the same value (2). The value in var1 was overwritten in line 3. So, to make this work properly, you must store the value of var1 in a kind of variable "buffer".

var1 = 1
var2 = 2
varb = var1
var1 = var2
var2 = varb

This will accomplish the task of switching two variable's values.

Many programming languages make swapping of variables more easy, using SWAP command:

var1 = 1
var2 = 2

SWAP var1, var2

Now var1 equals to 2 and var2 to 1

Uses for Variables

Accelerator Variable

An accelerator variable is a variable that represents the speed of acceleration of something. It works by adding the value of the accelerator variable to the one being accelerated each game loop.

For example gravity is a very common accelerator variable. It is added to the vertical speed of the object :

vSpeed = vSpeed + gravity

When this is done, vSpeed is accelerated at a constant rate - the rate of gravity.

Accelerator variables do not always have to be related to physics. In a forest fire simulation, an accelerator could represent the acceleration of the spread of the fire.

' acres = acres burned per game loop
acres = acres + spreadOfFire

Some programming languages allow shorter syntax for this type of addition, using += operator. So the examples above could be written as:

vSpeed += gravity

acres += spreadOfFire

Accumulator Variable

An accumulator variable is any variable used to accumulate a total.

Accumulator variables are used like buckets. If you put a pint of water into a bucket and then put another pint of water in the bucket, you have ''accumulated'' two pints of water in your bucket.

Some applications for accumulator variables :

Others

Related Pages

Backlinks

These pages link back to this page. You may find them helpful.

Links

Ask a Question

Ask a question about variables.

This page is 'effin screwed! It's probably Shawn's fault — but can you please try to help rewrite the page?

Categories: Programming : Variables
page_revision: 20, last_edited: 1236459980|%e %b %Y, %H:%M %Z (%O ago)