A variable is a place in your computer’s memory (just like a container) that can be used to store certain numbers. Let’s see a few C-Script examples:
var bullets = 7;
var health;
var lives = 3; // the player has 3
lives
These are a few short lines of code but we can use them to learn many new things:
1. Every variable must be defined before being used, using the keyword named var. If you write this line of code:
health = 100;
and you haven’t defined the var named health before using it you will get an engine error message.
2. Any variable can receive an initial value (bullets = 7, lives = 3) at game start but we aren’t forced to do that.
3. We can add our own comments to the code. Every time it encounters two slashes // the engine will ignore the words, expressions, symbols, etc that follow it. This way we can add useful comments to our code:
var car_speed; // the speed of the car
that chases the player
// @%$%&^& Ha Ha Ha! %^&**
I rule!
4. Every C-Script line needs to end with a semicolon. Many beginners forget to add ; at the end of their C-Script lines of code and this leads to many error messages.
5. C-Script uses a single type of variable (var) that can have up to six digits, with up to three digits following the decimal; therefore, a var can store numbers from -999,999.999 to 999,999.999. You can combine two or more vars if you need to use bigger numbers.
6. Every variable name must start with a letter or with an underscore _. Here are some valid variable names:
var AlohA;
var _me_too;
var go42;
var Iamb19;
var _12345;
Now let’s take a look at some bad var examples:
var #ItoldYou;
var 1_for_all;
var 12345;
I’ll let you discover what is wrong in the var definitions above.
7. Variable names aren’t case sensitive. This means that if we define a var this way:
var MyHeadShots;
and then we use it later in our code this way:
myheadshots = 5; // or
mYheadSHots = 5; // or
MYHEADSHOTS = 5;
the engine will accept it. However, it is always a good idea to set a naming convention and stick with it.
8. Finally, the variables should have significant names. While it is possible to define a pile of variables that look like this:
var x32;
var a125;
var h_34_5;
var _z34187;
it isn’t a good idea to do it this way. You will have problems trying to remember what these variables do if you look at your code a few weeks later. If you want to show your code to other people, they will have a hard time trying to figure what you wanted to do with your code. The var definitions below look much better, don't they?
var current_position; // current position
of the enemy
var number_of_goals;
var high_score;
var player_armor; // will be set to
100 when the game starts
Enough theory! Let’s use these variables!
Start the Script Editor (SED) and
then press its Open button :
Double click workshop02 and then double click the script02 file inside it to open the C-Script file.
Before continuing make sure that you have configured SED correctly:
1) Click the menu named Options and then select Preferences (or, in old versions, Configuration): you will see something similar to the following window:
If your window looks a little different, you're
lucky because you have a newer and better SED than I! Anyway, we need to
configure the two paths: 3D GameStudio Directory and script file to run.
The latter is important when we have more than one script opened in SED.
Let's click the first browse button :
Find the folder where you have installed 3DGS on your computer (mine was C:\Program files\GStudio6) and then select it and click Ok.
Now Click the second browse button and
select the script file that will run when you press the Test Run button.
(Tip: advanced users do this in a shorter way by through Debug
/ Set current file to run, or pressing Alt-F5).
Here's my C-Script configuration after finding the right path:
Don't forget that if you have installed 3DGS
in another folder your Configuration window will look slightly different.
Let's press OK and then we will try our luck with Test
Run: Click the triangle button!
The engine starts. Your screen will become blue (yes, that's my favorite color). This happens because I've set the screen color in the script to blue. Don't be afraid, you'll lrean how to set up different colors!
Let's get back to our screen; it should look like this:
This screen doesn't look too exciting just yet. Press the Tab key on your keyboard to show the console (the flashing cursor) and then type
a = 5;
and press Enter. You will notice that a = 5 and c = 5 as well! I wonder what's happening here...
Press Tab again, delete the existing text using the Backspace key and then type the following line of code:
b = 3;
After Enter the screen should look like this:
Starting to make sense of this, isn't it? It looks like c is the sum of a and b; try to type several values for a and b and you will convince yourself. Now let's close the engine window by pressing [Esc] and take a look at the miraculous piece of code that transforms our beloved engine in a simple calculator:
var video_mode = 7; // 800x600 pixels var screen_color[3] = 150,0,0; // dark blue
var a = 0;
var b = 0;
var c = 0; PANEL display_pan { digits (35, 10, "a = %d", _a4font, 1, a); digits (35, 19, "b = %d", _a4font, 1, b); digits (35, 28, "c = %d", _a4font, 1, c); flags = VISIBLE; } function main() { while (1) { c = a + b; wait (1); } }
If you think that this piece of code is too complicated, don't worry: we will only discuss the blue lines of code in this workshop. Whew... I felt quite a sense of relief when I heard that - did you?
var a = 0;
var b = 0;
var c = 0;
These are three simple var definitions. Now I know that I need to define a var this way if I want to use it later in my game.
c = a + b;
This line of C-Script code appears to be simple too; it makes c equal to the sum of a and b. Let's do a small experiment. Look in the SED window and find the line of code with our c = a + b; delete the "+" sign, replace it with "-" and the line will look like this:
c = a - b;
Now press the Test Run button again
and...
I can see the good old blue screen! Let's type same values (don't forget to press Tab):
a = 2;
b = 5;
and the result is c = -3! It really works!
You have now mastered the basics of C-Script, haven't you? Let's make a small experiment; we were told that any variable (var from now on) can receive an initial value. Is that true? Let's check that right away: edit the values for a and b in your script file to make them look like this:
var video_mode = 7; // 800x600 pixels var screen_color[3] = 150,0,0; // dark blue var a = 20; var b = 5; var c = 0;
Let's Test run
our level again, shall we?
The engine has read the new values and it has subtracted 5 from 20, displaying the correct result: 15. I know it's not a game what we're doing here, but it is pretty fun! So now we know how to add and subtract values; we can also use * to multiply two numbers or / to divide them. We could replace the 45th line of code (the one that does the calculation) with much more complex expressions but I hate math so let's discuss about something else...
Do you remember the first lines of code? I haven't forgotten about it:
var video_mode = 7; // 800x600
pixels
var screen_color[3] = 150,0,0; // dark blue
The engine knows how to deal with two types of variables: predefined and user defined vars. Some variables are already defined inside the engine by its creators; these are the predefined variables and they are controlling some features of the engine. The var video_mode above is a predefined variable; it is used by the engine to set the screen resolution (800x600 pixels in my example). Please note that video_mode can have a value between 1 and 11. screen_color is a special kind of variable - a vector - that takes 3 values and does as its name says, which is setting the screen color. We'll learn about vectors later. For a complete list of predefined variables check the reference manual.
If you plan to create the next Quake killer it is nice to know that when the big guys change the video resolution in their "Options" menu, they simply set a var like our video_mode to a new value. Feeling tired? Me too. Let's take a break.