Professional Documents
Culture Documents
Session 5: CScript
Learning Outcomes
In this session, you will be able to:
● Describe the different aspects of game programming
5.1 CScript
● Web applications
● Computer games
Just modeling, sound, or animation would not create games. Programming plays a major role in game
development.
Programming is a technique of running a set of instructions, which are formed into a particular format and run.
It handles different actions such as taking user input, drawing polygons on the screen, managing sounds, and
running Artificial Intelligence (AI).
The most used editor is Script Editor (SED). It has syntax, help, and other tools that make programming easy.
SED is available from the GameStudio6 menu on the Program menu.
As discussed earlier in Session 4 (Creating a Game), you begin by opening the files that have been added to your
programs.
A script consists of objects and functions. Objects are the components in the game. Text, models, and images are
all objects. Functions control objects by modifying their properties and in turn changing their behaviour.
5.2.1 Comments
To comment a line in all C++ based languages, use the following syntax.
For a single line comment, use two forward slashes.
For example,
//This is a single line comment
For multiple line comments, use the /* pattern to start a comment and */ pattern to finish a comment.
For example,
/*
This is a multiple line comment
Any line that is a comment will be ignored by the compiler
*/
Example:
function main()
{
//set some common flags and variables
//freeze all entity functions
freeze_mode = 1;
}
■ Variables:
In CScript, variables store numbers. There are a lot of predefined variables provided by the game engine. To
define your own variable, you can use the following syntax.
var name; //uninitialized variables
var name=number; //initialized variable
Variables can be redefined at any place within the script. All uninitialized variables are automatically initialized
to 0.
Table 5.1 shows the types of variables and their precision. Range describes the upper and lower limits of data.
There are three categories of variables. They are:
● Global Variables: Variables defined outside functions are called global variables. These variables are
available throughout the code.
● Local Variables: Variable definitions placed within functions are called local variables. These variables
are not available outside the function.
● Static Variable: A variable, if declared static, maintains its state between calls to the function in which
the variable has been declared.
You can group several variables together into arrays. Arrays are lists that store a lot of data. This data can then be
accessed by specifying an index, which in turn specifies the location at which the value resides in an array.
var name[n]; //uninitalized array
var name[n]={value_1, value_2, value_3, value_n}; //initialized array
For example,
var my_array[5]={6,2,7,3,111};
This creates an array of length 5 with 6 at index position 0 and 111 at index position 4.
■ Strings:
Strings are a sequence of alphanumeric characters (letters, numbers, or symbols). Strings are mostly used for
menus or text panels. They are defined in the following manner:
STRING* name=“characters”;
For example,
STRING* playername=“Player1”;
STRING* welcomestring=“Welcome to our game\n\n Press any key to begin”;
The above statements create a string. In the second line, \n creates a new line.
For example,
function ent_init(ent)
{
my=ent;
my.bright=on;
ent_animate(ent,”walk”,50,0);
}
ent_init(you);
In the above example, a parameter is used for the function and is passed with the keyword you. This parameter is
then accepted by the ent_init function as the local variable ent.
Functions can be made to return values or in other cases function execution can be terminated too. The keyword
return helps in doing the following two tasks:
Task1:
function compare(a,b)
{
if(a>b)
{
return(1);
}
else
{
return(0);
}
}
In the above task, return is called after finding whether a>b or not. This function can be implemented in the
following manner:
Task2:
if(compare(5,4)==1)
{
do something
}
else
{
do something else
}
Action is a special type of function available in the behavior panels and is assigned only to entities. In this
function, you will notice that the first wait call makes the game engine wait for 0.5 seconds.
For example,
wait(-0.5); //wait 0.5 seconds
while
wait(1); //wait one frame, then repeat
In the above example, the call to the wait() function makes it one frame and then the while loop continues.
■ if condition
These conditions will be used for comparisons.
You can use the following syntax for the if condition:
if(condition)
{
statement
}
else
{
statement
}
The else section can be, however, omitted.
For example,
if (((x+3)<9) || (y==0)){
z = 10;
}
else
{
z = 5;
}
Or
do
{
statements;
}
while(condition)
For example,
x=0;
while(x<100); //repeat while x is lower than 100
{
x+=1;
}
■ for loop
The for loop is used to run a set of instructions in a loop.
You can use the following syntax for the for loop:
for(initialization; condition; increment)
{
statements;
}
For example,
var i;
for(i=0; i<100; i++)
{
my.pan=i; //rotating the entity with the value i and stopping once the rotation is
100
}
■ switch case
A switch statement allows for comparison and execution based on many conditions.
You can use the following syntax for the switch statement:
switch(condition)
{
case label:
break;
}
switch(choice)
{
case 0:
printf(“Zero!”);
break;
case 1:
printf(“One!”);
break;
case 2:
printf(“Two!”);
break;
default:
printf(“None of them!”);
}
■ break statement
The keyword break will terminate the loop. It can be used in the following manner:
while (x<100)
{
x+=1;
if(x==50) { break; }; //loop will be ended prematurely
}
■ continue statement
It jumps to the beginning of the while loop.
For example,
while (x<100)
{
x+=1;
if(x%2) //only odd numbers
{
continue; //loop continuing from the start
}
y+=x; //all odd numbers up to 100 will be sum
}
■ goto statement
It jumps to a particular location and proceeds from there on. A label is defined by writing a text followed by a colon
(:) mark.
For example,
loop:
x=x+1;
if (x<100)
{
goto(loop); //jump to label “loop”
}
Aptech Software Ltd. - Project Private
CScript
Quick Test 1
. A script consists of ...........
. In Publish option how many files can be included when a game is generated?
Once the above steps are done, save the .wdl file and open up the World Editor (WED) and assign the
behaviour mydoor to the door you just imported.
In the above lines of code:
● The keyword my refers to the entity to which the action is assigned.
● The pan property is used for rotation.
This code suggests that the door will rotate till 90 degrees and then rotate back continuously.
Then, you compile and run the program. Using the character, you can move near the door to observe the rotation.
This code has vectors called my_pos and my_angle defined for storing the position and angle of the entity. Tilt
is another property of the entity like the pan property. The c_scan function is used for scanning entities within
the entities area. You can pass the position, angle, and temp (also called the UP vector). The last parameter is
SCAN_ENTS, which denotes that this function should scan only entities. Add the following lines of code, where
the scan_me() function ends:
function key_pickup()
{
if(indicator!=1)
{
return;
}
key1=1;
ent_remove(my);
}
action key
{
my.event=key_pickup;
my.enable_scan=on;
}
The door_event function indicates that if the user has collected the key, the door shall function or the function
shall quit. The user can open the door and close it by using the E key on the keyboard.
This is defined by,
on_e=functionname();
You need to assign the key action to the key model and the mydoor2 action to the door. Then, build and run the
whole project. You will notice that opening the door without the key will not result in anything.
Aptech Software Ltd. - Project Private 11
Games Development
5.3.3 Displaying Fonts
To display fonts, copy and paste the msgfont.pcx file from the template directory into your directory.
Open up the .wdl file to write the following lines of code:
FONT standard_font2,<msgfont.pcx>,12,16;
text my_text=
{
pos_x=20;
pos_y=40;
font=standard_font2;
string (“Hello how r u”);
}
function roll_my_text()
{
my_text.pos_y=480; //move the text below the screen
my_text.visible=on; //make it visible
while(my_text.pos_ y>0)
{ //as long as text still is on-screen
my_text.pos_y-=1; //roll upwards one line of pixels
wait(1); //then wait for one frame
}
wait(16); //time enough to read the last line
my_text.visible=off; //shut off the text
}
on_p=roll_my_text;
In the above lines of code, the first line defines the font and its file name. The number 12 indicates the width
and 16 indicates the height of each character in the image. All fonts and images must have either 128 or 256
characters, and all characters must occupy the same height and width.
Upon pressing the P key, the text starts rolling. The text object my_text sets up the text initial position and the
string holding the text.
The level_load function takes a parameter of .wmb to load. The currently running .wmb file is replaced by the one
passed as a parameter.
You have modified the door_event() function written earlier to make a call to the moveto_level2() function you
just created. The code now states that, once the door is fully opened, the moveto_level2 function will be called.
}
You should have level2.wmb in the same directory as your current file.
Hands-on Project
Create a multilevel game with opponents and portals, which take you from level to level.
The portals can be part of the level and also serve as a continuation to the next level. The user should be able to
travel between levels.
Every level needs to sport at least five enemies strategically placed in the level.
Work on lights and sound. One of the rooms should be a dark room while another level should be an outdoor
level. Customize the nexus size as discussed in the earlier session.
Work on a concept for the entire game.
5.4 Summary
5.5 Exercise
. All uninitialized variables are automatically initialized to 0. (True/False)
. Which function is used for pauses the current process for a given number of cycles or seconds?
4. The ......... function is used for scanning entities within the entities area.
Quick Test 1
. Objects and functions
. 40
. True
. wait() function
4. c_scan
5. Enable_scan