Report article RSS Feed Scripting #2: Variables

This tutorial is about variables types, and their usage.

Posted by iCore on Feb 22nd, 2013
Intermediate Server Side Coding.

Like in every programming language, we must use variables to store values, which we want to use in the future in GSC too. All of them can be identified with their unique names. Don't forget, these should always be named with English letters and numbers, and you can't start it with a number (the engine does not make difference between small and capitalized letters). Every variable can only be used in the block (between the opening and closing curly bracket pairs), in which it has been created, it is called the scope of a variable. There are some exceptions, like the level variable, which can be reached from everywhere. You can set value with an equal sign like this (assignment declares the variable automatically):

something = 5;

In this case, the value of something is set to 5. The semicolon in important at the end, because it will close every statement. You can also set a variable as the value of an other variable:

first = 4;
second = first;

After executing this, both first and second variables will have the value 4. We are also able to overwrite the current value of a variable, in this case the old value will be lost. You are able to remove a variable in the following way:

var0 = undefined;

Variables can have many types: Integer, float, string, boolean, array, vector, pointer, and structure. So let's check what do these do, and how do we use them.

Integer

We are using integers in most cases, since we can store many things with these, and it is easy to calculate with them. We have also used it in the examples above.

int_number = 14;

I don't want to write more details about this, you will be able to check the counting in the operators tutorial.

Float

It is much rarer, because we don't have to store number with high precision usually (and it is also really inaccurate). In this language, the decimal point is a dot:

frac_num = 6.58;

When needed, we can convert it to integer with the int() function (with floor rounding), we will talk about it later.

String

Storing character string is also rare, since these are usually in language-specific files, or we are printing the text directly. These must be written between two quotation marks.

text = "Some text here";

When needed, we can get the length of this by using the size parameter:

a = text.size;

The mentioned language-specific files can be found in the str files, and we can link to them by writing an [at] symbol before the first quotation mark, and we set the needed file and string name:

locStr = @"MP_SCORE_LIMIT_REACHED";

In the example above, we are referring to the SCORE_LIMIT_REACHED text in the localizedstrings/mp.str file. You can find it this way inside the file:

REFERENCE SCORE_LIMIT_REACHED
LANG_ENGLISH "Score limit reached"

The structure of an str file looks like this:

VERSION "1"
CONFIG "C:\trees\cod3\cod3\bin\StringEd.cfg"
FILENOTES ""

REFERENCE SOMETHINGTEXT
LANG_ENGLISH "Example text"

REFERENCE SOMETHINGTEXT_2
LANG_ENGLISH "Example text 2"

REFERENCE SOMETHINGTEXT _3
LANG_ENGLISH "Example text 3"

ENDMARKER


The first 3 rows must be always in the beginning of the file, don't even edit them, they are not changing anything in the display. The ENDMARKER at the end signs the game, that it reached the end of the file, it shouldn't check it for more strings, so that's why you shouldn't put anything after this. If you want to create a new localized string, then all you have to do is, to make a new REFERENCE - LANG_ENGLISH pair between the start and the end part, for example:

VERSION "1"
CONFIG "C:\trees\cod3\cod3\bin\StringEd.cfg"
FILENOTES ""

REFERENCE SOMETHINGTEXT
LANG_ENGLISH "Example text "

REFERENCE NEW_LOC
LANG_ENGLISH "New string"

REFERENCE SOMETHINGTEXT _2
LANG_ENGLISH "Example text 2"

REFERENCE SOMETHINGTEXT _3
LANG_ENGLISH "Example text 3"

REFERENCE NEW_LOC _2
LANG_ENGLISH "New string 2"

ENDMARKER


Array

An array is like multiple variables in one, which have strong connection between them. You can declare it this way:

players = [];

After this, an array, named player will be created, in which you can put any elements, with any types. As default, an array is indexed from zero, these are called indexed arrays. So for instance, if we want to store player names, then we instead of this:

player0 = "[HNS]-Joe-";
player1 = "ModBase.iCore";
player2 = "RatCompressor";
player3 = "SBB:KilleRR";

We can use this:

players[0] = "[HNS]-Joe- ";
players[1] = "ModBase.iCore ";
players[2] = "RatCompressor ";
players[3] = "SBB:KilleRR";

Maybe it doesn't look easier, but let's check why is it better:
1) We can access a given index of an array. For example, if we want to check the name of the second player, we can't do it with more variables, but we are able to do it with arrays easily:

a = players[2];

2) We can query the element number of an array. So if we want to know, how many players are stored, it would require one more variable with independent variables, but with arrays, we can get it simply with the size parameter (just like at strings):

a = players.size;

It is an important property to be able to add a new element. With variables, we don't know which is the next, which with arrays:

players[players.size] = "[HUN]TER";

We can use it this way, because if we currently have two elements in the array, then according to the meaning players.size will return two, which means, that we have a zeroth and a first elem, but not a second one; so the second will be the next one, in which we can write.
Arrays have a second type too, the associative array. The only change in it is, that we can access it with string index, instead of a number:

arr["sthhh"] = 6.33;

If you are putting elements in an array instantly when you create it, you can leave the declaration part, for example in this case, you can remove the first line:

t = [];
t[0] = "st";

Boolean

Booleans can have two values: true, and false. We can store in it, if a player owns a given thing, the bomb is planted, or anything else, for which you can answer with yes and no. It is much more optimal, than storing 0 and 1 with numbers, since it is allocating much less memory.

logical = true;

Vector

It can store a coordinate in the format of (X, Y, Z). It should always be between brackets, and it must contain three, comma separated numbers.

origin = (14, -533, 31.196202);

With this, you can set the origin or angles of any entity. You can handle these as array too, so based on the example above origin[0] = 14, origin[1] = -533, origin[2] = 31.196202.

Pointer

These are special variables, which are referring to a given entity (effect, player, model, etc):

ent = loadFX("smoke/village_dust");

or to a function:

func = ::some();

For being able to use it, you will have to understand the usage of functions, so I won't explain it deeply now.

Structure

It is very similar to the associative arrays, it does the same with a different syntax. You can declare it this way (it must be defined always before using it):

stru = spawnStruct();

After this, you can set endless keys for it, and just like array elements, these can have any type too:

stru.elem = -2;

Post comment Comments
skogkniv
skogkniv Apr 1 2013, 3:49am says:

Very helpful!

+4 votes     reply to comment
iCore
iCore Apr 7 2013, 2:46pm replied:

Good to hear, I'll try to continue it when I'll have time :P

+2 votes     reply to comment
Tonkoslav
Tonkoslav Jun 23 2013, 8:39am says:

Very helpful indeed! Keep up the good work.

+3 votes     reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Games
Call of Duty
Call of Duty Single & Multiplayer First Person Shooter
Call of Duty 2
Call of Duty 2 Single & Multiplayer First Person Shooter
Call of Duty 4: Modern Warfare
Call of Duty 4: Modern Warfare Single & Multiplayer First Person Shooter
Call of Duty: World at War
Call of Duty: World at War Single & Multiplayer First Person Shooter
Related Engines
id Tech 3
id Tech 3 GPL Released Dec 3, 1999
IW Engine
IW Engine Commercial Released Oct 22, 2005
Related Groups
Activision
Activision Developer & Publisher
Infinity Ward
Infinity Ward Developer & Publisher
Treyarch
Treyarch Developer & Publisher