You are on page 1of 15

CScript

Session 5: CScript

Learning Outcomes
In this session, you will be able to:
● Describe the different aspects of game programming

● Describe the basic concepts of CScript

● Explain the elements of a program

● Explain the application of CScript


This session will guide you through the different aspects of game programming, the basic elements of a program
and programming with CScript.

5.1 CScript

5.1.1 Introduction to Game Programming


Behind every pixel that moves, there is a hand of a programmer. Programming allows you to create applications.
Some commonly used categories of applications are:
● Music and video players
● Business applications
● Mail applications

● 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).

5.1.2 Introduction to CScript


Game programming can be done in many languages. However, in the game engine that you are learning, you can
code in the CScript, Lite-C, C, or C++ languages.

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.

Aptech Software Ltd. - Project Private 


Games Development

5.2 Elements of a Program

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
*/

5.2.2 Include Statement


If you observe the .wdl files created earlier, you will find a statement called Include. The Include statement
includes all the content of the file where it is called. It is like copying and pasting.
The syntax for the include statement is:
#include “movement.wdl”; //include Movement function
#include “venture.wdl”; //include Venture script
Upto 40 files can be included when a game is generated using the Publish option. The number of includes is
not restricted when a game is generated using the Resource option. Script names must not be longer than 15
characters and must not contain spaces or nonstandard characters.
Another command that is used frequently is the PATH directive. All resources are first scanned in the work folder
and then in the path mentioned in the PATH directive. You can use the syntax for PATH directive as:
PATH “files\\models”; //some models are there in the folder “files\models”

The following rules exist with the PATH command:


● Give backslashes in C notation as double backslashes to mention directories.
● Do not give absolute paths such as “C:\\mywork\\myprojects\\files\\models”, but use relative paths such
as “\\files\\models”. The above path command suggests that the .wdl files resides a level higher than the
files directory.
● Do not use any special characters in folder names.

 Aptech Software Ltd. - Project Private


CScript

5.2.3 Main() Function


The main() function is executed while starting the game. The main() function is executed before the game engine
and video device are initialized. It is usually the location where all the variables are initialized and assigned default
values.

Example:
function main()
{
//set some common flags and variables

//freeze all entity functions

freeze_mode = 1;

}

5.2.4 Variables and Strings

■ 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.

Type Size (bytes) Range Precision


var 4 -999999.999–999999.999 0.001
long, int 4 -2147483648–2147483647 1
short 2 0–65536 1
char 1 0–256 1
float 4 -3.4•10 –3.4•10
38 38
1.2•10-38
double 8 -1.8•10308–1.8•10308 2.2•10-308
Table 5.1: Types of variables and its size, range, and precision

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.

Aptech Software Ltd. - Project Private 


Games Development
For example,
var freeze_mode; //freeze_mode is global variable
function main()
{
//set some common flags and variables

//freeze all entity functions

freeze_mode=1;

var gametype=0; //local variable

static var keypattern=0; //static variable}

In the above lines of code:


● freeze_mode is a global variable.
● gametype is a local variable.
● keypattern is a static variable.

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.

5.2.5 Functions – How to


Functions are a list of instructions called by a particular name. Functions can be defined by the developer or can
be an internal game engine function. There are a lot of predefined functions in the template scripts. A function is
defined in the following manner:

function name (parameter1, parameter2, parametern)


{
instruction 1;

instruction 2;

instruction 3;

}

 Aptech Software Ltd. - Project Private


CScript
For example,
function beep3()
{
beep();

beep();

beep();

}
This function makes the game engine beep thrice. You can call the function in the following manner:
beep3();

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

}

Aptech Software Ltd. - Project Private 


Games Development
Another function is the wait() function. This function pauses the current function for a given number of cycles or
seconds. This function takes only one parameter, time. If used as a positive number, this function considers it as
frames and if used as a negative number, this function considers it as time in seconds.
For example,
action rotator()
{
my.ambient=100; //increase brightness

wait(-0.5); //wait 0.5 seconds

my.ambient=0; //normal brightness

while (1)

{
my.pan+=3*time; //rotate entity by 3 degrees per tick
wait(1); //wait one frame, then repeat
}
}

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.

5.2.6 Control Statements

■ 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;

}

 Aptech Software Ltd. - Project Private


CScript
■ while, do loop
This loop is used for repeating a sequence of instructions if a particular condition is found true.
You can use the following syntax for the while loop:
while(condition)
{
statements;

}

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;

}

Aptech Software Ltd. - Project Private 


Games Development
For example,
var choice;

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?

5.3 Application of CScript

5.3.1 Programming Doors


In the following topics, the basics of handling elements are described through examples.
In the previous session 4 (Creating a Game), you saw several examples using the behavior panel.
Now, you can create an object shaped like a door by using Model Editor (MED) and import it inside the game
level you built. Then open the .wdl file associated with the level and write the following lines of code, where the
main_quit() function ends:
action mydoor
{
while(1)

{
while(my.pan<90)
{
my.pan+=3*time;
wait(1);
}
wait(16);
while(my.pan>0)
{
my.pan-=3*time;
wait(1);
}
wait(16);
}
}

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.

Aptech Software Ltd. - Project Private 


Games Development
5.3.2 Picking up Keys
You shall create another model like a key in MED and import it in WED. Your motive should be to restrict the user
from opening up the door without the key.
Now, you can begin by writing the following lines of code:
var indicator=0;
var my_pos[3];
var my_angle[3];
define _counter skill25;
var key1=0;
function scan_me()
{
my_pos.x=camera.x;

my_pos.y=camera.y;

my_pos.z=camera.z;

my_angle.pan=camera.pan;

my_angle.tilt=camera.tilt;

temp.pan=120;

temp.tilt=180;

temp.z=200;

indicator=1;

c_scan(my_pos,my_angle,temp,SCAN_ENTS);

}

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;

}

10 Aptech Software Ltd. - Project Private


CScript
The key_pickup function is associated to the key action. The my.event function suggests that the key_pickup
function will be called whenever an event occurs. The Enable_scan function enables the scanning of an entity.
The ent_remove() function removes an entity from the world. Add the following lines of code below the end of the
key action:
function door_event()
{
if(indicator!=1)

{
return;
}
if(key1!=1)

{
return;
}
if(my._counter<=0)

{
while(my._counter<90)
{
my.pan-=3*time;
my._counter+=3*time;
wait(1);
}
my.pan+=my._counter-90;
my._counter=90;
}
else
{
while(my._counter>0)
{
my.pan+=3*time;
my._counter-=3*time;
wait(1);
}
my.pan+=my._counter;
my._counter=0;
}
}
action mydoor2
{
my.event=door_event;

my.enable_scan=on;

my._counter=0;

}
on_e=scan_me();

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.

5.3.4 Changing Levels


At this level, you are going to create a portal. A portal is an opening to another part of the level or another level
itself opens up through a portal. The user is normally transported to another location in the map.
You can create a door in the level and assign the action you created earlier. Now, assign the mydoor2 action.
Add the following lines of code to the .wdl file:
function moveto_level2()
{
wait(2); //time take for message to be visible

my=null;

wait(1); //time for level to be loaded

level_load(“level2.wmb”); //load the eg5.wmb level

}

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.

12 Aptech Software Ltd. - Project Private


CScript
For example,
function door_event()
{
if(indicator!=1)

{
return;

}
if(key1!=1)

{
return;
}
if(my._counter<=0)

{
while(my._counter<90)
{
my.pan-=3*time;
my._counter+=3*time;
wait(1);
}
}
moveto_level2();

my.pan+=my._counter-90;

}
You should have level2.wmb in the same directory as your current file.

You need to run the file to view the output.

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.

Aptech Software Ltd. - Project Private 13


Games Development

5.4 Summary

In this session, you have learned:

● The different aspects of game programming

● The basic concepts of CScript

● The elements of a program

● The application of CScript

14 Aptech Software Ltd. - Project Private


CScript

5.5 Exercise
. All uninitialized variables are automatically initialized to 0. (True/False)

. Arrays are .................

. 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.

5. The ......... function enables the scanning of an entity.

Quick Test 1
. Objects and functions

. 40

. True

. Lists that store a lot of data

. wait() function

4. c_scan

5. Enable_scan

Aptech Software Ltd. - Project Private 15

You might also like