Blitz: Basics 01

Blitz3D Forums/Blitz3D Tutorials/Blitz: Basics 01

poopla(Posted 2003) [#1]
Well students, this is my first tutorial for you, I hope you have a comfy chair and a drink in hand.

I'm writting this, and future tutorials to help beginners become fluent in general blitz coding practice. And witout further delay, here we go.

This tutorial is going to cover some basic memory management. An overview of the blitz data types, and how to use them!

(I'm not selling this so any of you l337 hax00r's can kiss my dimpled hind end if I make any mistakes ;).)

-VARIABLES-
These are the simplest of the data storage methods(actually, they are the only form, but I'll leave that fact be as to not confuse you!), and will be used in every program you write at some point.

-Literal Constant-
The simplest form of vairable is the "Literal Constant".
Examples:
30
100
1000000000000000000
20.5

The literal constant is any number that is explicitly writtin. By this, I mean it is not represented by a variable name(as will be explained in a few minutes).

For example, "20 + 2" is using two literal constants, 20, and 2. As shown in the example, whole numbers are not the only literal constants, and floating point numbers(fractions, decimal point) are also Literal Constants.

For a little more clarity about the name "Literal Constant" simply analyze the words. "Literal" - The number is literal, as it is written/stated in code. "Constant" - That number won't be changing unless you manually re write the number in your code.

-CONSTANTS-
These are different from literal constsnts, and it is important you don't confuse the two. A literal constant is defined by using the "Const" keyword.

(NOTE: A "KeyWord" is a word that the Blitz compiler understands to perform an action.)

Examples:
Const MY_VARIABLE = 20
Const ANUMBER# = 404.327

The Word that follows the blitz keyword "Const" is the name of the variable, and is used in place of the literal constant that it contains. You have to initialize the constant(Initialize means to set the variable to a certain value) when you define(Define means to actually create the variable) it, as show in the example "MY_VARIABLE = 20".

You may have noticed the "#" in the example "ANUMBER# = 404.327". This tells the compiler that you want that variable to be a "Floating point" number. This means it can contail fractional values rather then just whole numbers. If you don't add that# to the end of your variable, and try to assign it a floating point value, the compiler will simply round off the value and place it in the variable. So if you do something like "Const MYVAR = 1.005" MYVAR will actually equal 1, because it will round down to 1 as MYVAR is not a floating point number(No "#" on the end of the variable).

If the variable isnt a floating point number, what is it? Well, it is called an "Integer". This is simply a whole number, and an integer variable, as aforementioned, will only hold integer values.

-PROGRAM SCOPE-
I'm going to briefely jump over the concept of program scope. Program scope basically defines where a variable "lives". At the moment, any variable you define(granted you havnt started using functions), is in the "Global scope". This means all the parts of your program can use it. If you WANT all parts of your program to be able to use a variable of some type, explicitly define the variable as global by using the "Global" keyword

(Some types of variable are automatically defined as global, such as "Arrays" and Constants.)

-GENERAL VARIABLES-
These guys are the most common in any program. If you wanted to store a value that could later be changed by using math opperators, you would simply define it by using a variable name followed by the value.
EXAMPLES:
MyVar = 20
AVariable# = 100.5(Remember, the # on the end
makes it floating point)
speed = 10

This is simply going to define a variable that will be accessable to any part of your program that is in the same scope(I'll have to go over scope a bit more thoughrouly in the "functions" tutorial I plan to do. For no don't worry about it.).

-STRINGS-
You may have been wondering, "can we store words and characters"? If so the answer is yes. The data type that can store a word, or paragraph, is called the string type. To make a variable a string, you write your varialbe name folorwed(At the end of)by a "$". To place a value into the string variable, you use the "equals" sign ( = ) and follow that by the letters of the "string" enclosed in quotes.(It is called a string because the computer sees it as a string of characters).
EXAMPLES:
word$ = "lalalala"
letters$ = "WOW!"
anumber$ = "2,000,000 is a number in this string!"


Well there you have it! I will do a tutorial for advanced data types like: Data banks, Custom Types, and Arrays after ive done a tutorial on manipulating the variables I explained in this tutorial(unless one of you does first!)

Till I see you again kids, hope this helps, and keep practicing!(Practice makes perfect, or some crap like that!)