Variables in Blitz

A short tutorial on variables in BlitzBasic.

Variables:

What is a variable? Well, a variable can be a lot of different things, but in Blitz a variable is a word that has a number or string assigned to it. A string is anything enclosed in “”. In other words “Good Golly it Works” is a string.

How do you assign variables? Well its actually very simple. It works something like this.

age% = 14
meters# = 5.6
expression$ = “Good Golly it Works”

Now those words can be used in the place of what their assigned too, and you can now change what their assigned too. For example:

expression$ = “Good Golly it Works”

Print expression$
Delay 5000

This program is exactly the same as the first one.

Now you might think this is pointless, in this program it is, but what if you have to use a number a 100 times. If you use a variable, then you don’t have to go through the entire program to fix all 100 numbers, you just change the variable.

Variables however aren’t just used to save you time, you can also use them because they can change, and change is how your game works. Take this for example.

Lives = 1
Lives = Lives + 1
Print Lives
Delay 3000

As you can see, the second line changed lives by adding one. You can do this with -,*, and / as well. There is some more advanced math too but that’s in the next chapter.

At this point I would like to go over some different types of variables.

First there are Global and Local variables. The variable we declared in the previous program was Local. A local variable will only work in the main program, and will not work in functions, which are later in the book. It also works vice versa, where a Local variable in a function only works in the function.

When you are declaring a Global variable, you simply declare it like this.

Global Lives = 1

Just the variable name automatically makes it Local, however in a function you must call the variable as.

Local Lives = 1

And again we will go over functions later.

There are also constants; these involve declaring a variable that must stay the same. They are declared.

Const Lives = 1

Well, that’s about it for variables. Let’s move on to the “if” statement.

The “If” Statement:

The “If” statement is the bulk of most of your programs. It allows you to say if something happens, do something. For example you might say in your game.

If player hits up,
Move the character up

And this is all very good and fine, except that if you tried to compile the above lines, you would get a very large amount of errors. So how would you write the above bit in Blitz.

If KeyHit(200) Then
playery = playery – 5
EndIf

For write now, I would like you to ignore the KeyHit command, but concentrate more on the “if” part. As you can see, it started with an “If”, all statements start with an If. Then you put in your tester. So in this case KeyHit(200) is your tester. Then comes the “Then”, this is not required; the following code is perfectly legal.

If KeyHit(200)
playery = playery – 5
EndIf

However, the code with the “Then” is usually a bit easier to read.

All “if” statements must end with and “EndIf”, if not it will produce an error. This can become very tricky also when you start nesting your “if” statements, nesting means you put an if statement inside another, for example.

If KeyHit(57) ;spacebar
If ammo > 0
;shoot bullets
EndIf
EndIf
Now there are about three new concepts here, so let’s go over them one at a time.
First let’s talk about commenting, commenting is the part of your program that only humans can read, the computer can not read it. To write a comment simply use a semi-colon before you write the comment, anything after the ; is not included in your program when you run it, but any person who looks at your code can see them. It is a good habit to write yourself comments even in small programs because when they get big, you need comments.
Next let’s talk about the nesting in the previous example above. As you can see each “If” had a corresponding “EndIf” with it. It is a good idea to line up each “If” and “EndIf” so you don’t leave one out.
Finally I would like to talk about operators. We already went over a few in the variables section. These were mathematical operators, they included +,-,*,and /. However, there are also the relational and logical operators.
Relational Operators are the main power for your “If” statements, they include <,>,=,<=,>=, and <>. <> stands for not equal too. You use these statements to compare variables and see if something should or should not be done. Example.

If do > 0 Then
;do whatever
EndIf

Pretty simple huh, you’ll learn how to use them as you go, that’s really the best way to learn them.
Logical Operators are used to perform more complex testing for your “if” statements; you can test whether two statements are true, at least one is true, or if it isn’t true. True is 1; False is 0. You can even use the words true and false instead of 1 and 0. The three logical operators are And, Or, and Not.

And is used to see if two statements are true, like this.

If x = 400 And y = 300 Then
Centerscreen = True
EndIf

Or is used to see if at least one of the statements is true.

If health = 0 Or fall = 1 Then
Dead = 0
EndIf

Not is a bit different, Not returns true while the statement is false. So if:
alive = 1

If Not alive = 1
dead = 1
EndIf

In this case, it is testing if alive isn’t one; But alive is one, so dead isn’t one.

Alright, there is one more thing you should know about “If” statements, and that is that If statements can have extensions added on to them, these are called If…Then…Else statements. They work like this:

If alive = 1
;do alive stuff
Else
;do dead stuff
EndIf

You can only add one else, but you can also add Else If statements, which work the same, except you must restate each statement.
Example:

If alive = 1
;do alive stuff
Else If hospital = 1
;do hospital stuff
Else If dead = 1
;do dead stuff
EndIf

Simple, I hope so. Anyway, that’s all there is to “if” statements, but there is another similar statement I’m going to go over. It is called a Select…Case.

In a Select…Case statement, the there is one variable, and several possible choices for example, say you were picking a direction.

Select direction

Case 1
;up
Case 2
;right
Case 3
;down
Case 4
;left
Default
;you can go that way
End Select

As you can see it’s pretty straight forward. You select a variable to use, and then if the case matches, it does the action programmed their. The “Default” command is used when none of the cases match the variable. “Default” is not required, but a good habit.

Two quick things before we move on to Arrays.

First the Goto command, it’s rather useless, but should be covered nonetheless. Here’s an example.

Goto here

;this code is skipped.

.here

As you can see, Goto skips over code to the label that is made by placing a period before the word. There is almost always a better way to do the thing that you want to do. I’d recommend using it as least often as possible.

Also there is a command called “Input”, assign a variable to it and the user can enter in values. For example, type this in and run it.

Name$ = Input$(“What is your name “)

Print Name$
Delay 5000

The Input command allows the player to enter his name, which is then printed.

comments powered by Disqus
Loading