- 2Chapter 1 – Variables & Operators
- 3Chapter 2 – Functions & Loops
- 4Chapter 3 – Arrays & Keywords
- 5Chapter 4 – If/Else & Switch
- 6Chapter 5 – Lets start Scripting
Jun 11, 2010 READ / / / Had to audioswap:( Just a short tutorial vid. When I updated my game to 1.7, it stills said 1.0 (but in multiplayer it said 1.7) So I was like '.
Hi, I am Tristan. I am the creator of www.InfinityTutorials.com. I love programming, modding,gaming and best of all teaching others. That’s why I strive to create a trusted modding &mapping community for everyone to join, gain experience & knowledge from.
This book should help you to understand the fundamentals of CoD Script programming for Call ofDuty. By the end of it, you should feel comfortable with CoD Script, its layout, and how it works.I hope you take away as much as you can from this book, and if you need help on any topic, ormore advanced topics, please visit the website, and we’ll be more than happy to help you.
So get ready, get modding and enjoy!
Welcome
Welcome to the InfinityTutorials CoD Script Handbook. By reading this book from start tofinish, you’ll expect to be up and running making mod’s for CoD in no time! Scripting isn’t veryhard, and as long as you’re patient and you read closely, you’ll be a CoD Script master in a fewdays with a little practice.
Alright, to start your programming adventure, you’ll need a few tools. Even though these areoptional, they will make your programming experience a better & more organized one. The firstis a syntax highlighting text editor. I recommend Programmer's Notepad.Using a program like this, you can create & organizeprojects, edit & create your scripts, and the best of all, it include syntax highlighting.Syntax highlighting helps you to distinguish between variables, operators,strings and keywords, which we will cover later in the book.
The second tool, is a Zip compressor & extractor. Now, tools like Win-Zip and RAR will work,but what happens if you don’t have them, and don’t want to spend the money buying them?Well there is a great free alternative called 7-Zip.
Variables
Variables are your best friend. They can hold numbers, strings or even simple things such asTrue and False. I don’t know of a single script that doesn’t contain at least 1 variable in it. Thisis how important they are. Variables are much simpler in CoD Script than in similar languages likeC or C++. This also makes them a little less error prone, and easier to use. Here’s someexamples of variables and their properties:
The variable my_number now holds the digit value 1. A practical use for this type of variablewould be, for example, number_of_lives which would hold the number of lives a player has.
The variable my_double now holds the double digit value 2.0. You could use this to store aplayers points for example.
The variable my_message now holds the string 'Hello World'. You could use this type ofvariable to hold a custom HUD element message, or a welcome message.As you can see, the general format of a variable is:
Remember to always end your lines & declarations with semi colons ';' or you will receive weirdlooking console error messages. In CoD Script the semi colon lets the compiler know that you’refinished with one line and that you’re about to start another. However, there are cases whereyou don’t end a line with a semi colon, this is with loops, if/else statements etc.
Operators
Operators are the little things that manipulate variables. You can use operators to add,subtract, divide & multiply variables. They are extremely useful, and you’re bound to use themin tons of scripts. Here are some examples of operators in use:
In this example, number_one and number_two are added together, and the result is placed inthe variable the_result. Thus, the variable the_result now holds the value 3.
In this example, number_one is subtracted from number_two, and the result is placed in thevariable the_result. Thus, the variable the_result now holds the value 1.
In this example, number_one and number_two are multiplied, and the result is placed in thevariable the_result. Thus, the variable the_result now holds the value 2.
In this example, number_one and number_two are divided, and the result is placed in thevariable the_result. Thus, the variable the_result now holds the value 2.
In this example, the ‘++’ simply means, increase the value by 1. You can also decrease a valueby 1 using ‘--‘, an example: number_one--;
Now, what happens if you want to add 2, 3, 4 etc to a variable? Or want to multiply thevariable’s value by a certain number? Well, you can do it like this:
In the above example, number is given the value 1, then, it’s set to equal itself, plus 2. Whichsimply means, that in the end, number now equals 3.A shorter way of doing this is:
number = 1;number += 2;
This shorter version above achieves the same result as the previous example. This shortcutworks with all the operators. So for example, if you wanted to divide a variable by 2, thenmultiply it by 5, this is how it would be set-up:
At the end of this, number would now hold the value 25.
Functions
Functions are a vital part of any programming language, and CoD Script is no different. Functionshelp organize your code and allow you to perform more advanced tasks.So lets dive right in, here is an example of a small function:
As you can see, functions start with a name to uniquely identify them. Their names cannotcontain anything except letters, numbers and underscores. Their names cannot start with anumber or contain spaces. They then continue with a left bracket & a right bracket. Then theycontinue with a left open curly brace. After this brace you type the code you want executedwhen your function is called. You then end the function with a right closed curly brace. Onething to note is that CoD Script isn’t picky about how your code is laid out, so the following iscompletely acceptable:
Now, you may be asking, what does return do? Well, it’s a CoD Script keyword, and in thisexample it returns the value 5. This value is sent to what ever function called it. So, a workingexample would be:
In this example, the_number_five now contains the value 5 since my_function returned thevalue 5 to it through the return keyword.
An important thing to note is that not all functions need to return a value, these functions areusually called Void functions.
Functions can perform a wide verity of operations when called. One very special feature offunctions are Parameters. Parameters allow you to pass data into the function to work with.
So lets take a look:
As you can see, you can place parameters between the two brackets next to the functionname. The parameter names follow the same rules of the function names. Lets see an exampleof this function in use:
In this example, not_yet_ten is initialized to 5. Then we declare the variable this_is_ten andmake it equal the function add_five, and we pass not_yet_ten as an argument. Looking intothe function, we see it takes not_yet_ten and places it into the variable my_argument. Then,a new variable temp is created, and it’s set to equal my_argument.. 5 is then added to tempand then the function returns temp which in this case is now equals to 10 since the argumentpassed into the function was equal to 5. At the end of this whole procedure, the variablethis_is_ten, equals, you guessed it, 10!
Ok, after that long winded example, you should have a clear understanding of how parameters& arguments work. Now, another point to note is that you can have more than one parameterlike so:
All that’s needed to use more parameters is that you separate them with comma’s… simple. Inthat example the final variable the_result would equal 6.
There’s one mistake a lot of people make though, and that’s using variables out side of theirscope. A scope is any section between the opening brace '{' and the closing brace '}'. Anyvariables declared inside this scope can only be used inside this scope, and not anywhereoutside it. However, you can declare global variables which can be used out side of theirscopes. Here’s an example of a classic mistake:
The example above would cause a compile error. This is because the variable local_var isdeclared in the functions scope, and then used outside of it’s scope when assigning it toanother variable. However, if you do want to perform an action like this, you can declare aglobal variable:
In CoD Script the above is perfect code and will not generate any errors. In that example,we used the keyword self which will be explained later.
Loops
Ahh, loops, my favourite little function ?. Loops allow you to repeat a process a set amount oftimes, or a limited amount of times. Loops can be used to monitor variables, print messages,check players etc, anything that needs constant attention or repeating.
There are two types of loops. The first we’ll look at is the while loop. Lets take a look at anexample:
In this example, number is given the value 1. A while loop works by checking the argument,which in this case is 'number < 10' to see if its true or false. In this statement, it’s basicallysaying, 'Loop While Number Is Less, But Does Not Equal 10'. If your argument is true then theloop performs the actions between the braces. When it gets to the last brace, it jumps back tothe top, and checks your statement again. If the statement is still true, then it will perform theactions between the braces again. This process will continue until the statement is false whichis this example would be when the variable number equals ten.
At the end of that loop, if we were to check the value of number we would see that it’s nowequal to 9.
While loops can also be set to loop forever. This is done by simply by adding an argument thatwill never return false, like this:
OR
Since 1 is 1, there is nothing to check, it will always be true. Even better, just put true in there.
Here’s an example of an infinite while loop in use:
In this example, the message 'This is a looping message' will be displayed every 5 secondsand will never end. If you’re unsure what some of those commands are, don’t worry, they willbe covered later.
The second loop we will cover is the for loop. Here is a example of one:
As you may have noticed, the for loop takes 3 arguments, the first is a declaration of a variableor counter variable, the second is what to check for (same as the true/false for the while loop),the third is what to do to the counter variable when 1 loop is finished. You must separate thearguments with semi-colons.
In the example above, when the loop finishes (number = 10) the variable result will equal 45.You can also make infinite for(;;) loops, but it’s a better choice to use a while(true) loop in thatcase.
Arrays
Arrays are possibly the greatest type of variable alive in my eyes. Arrays are a special type ofvariable that can hold multiple entries of data, like a list of names, maps, scores etc.Alright, lets look at how to make an array:
We start arrays with the variable name, but then, instead of assigning a value to it, we assign []to it (Two opening & closing square brackets). This declaration tells the compiler that thevariable my_array is now an array. We then add data to the array by typing the variable name,followed by a subscript. A subscript tells the compiler which part of the array to use/assign.Array subscripts ALWAYS start with zero. This is one thing you must learn, as it’s the case inmany programming languages. Learn it now, and it will save you grief later on. When you addmore data to the array, you must increase the subscript by 1, and not 2 or a fraction etc. Hereis an example of an error:
In this example error, you see that we started the array subscript with 1, and then incrementedit with 2 instead of 1. This is bad, don’t try this at home.Now, arrays work great with for loops. Here’s an example:
In this example, awesome_array is initialized, then we step into a for loop, where it sets thefirst 9 subscripts of awesome_array to the value of int plus 5. So, if we had to check the valueof awesome_array[0] it would equal 5, awesome_array[1] would equal 6, awesome_array[2]would equal 7 etc. This array would only go up to awesome_array[9], so trying to access anysubscript like 10 or higher would cause an error, or no error would be produced, but you’ll bewondering why you’re script is acting funny.
Keywords
Alright, now that you know the essential basics of CoD Script, lets move on to some CoDspecific keywords.
- wait = the time to wait in seconds
- self = an alias to the entity that called the script
- level = an alias to the level (the main script)
- waittill = wait until another script notify’s a keyword
- notify = used to trigger waittill
- endon = used to kill a function on a notify
- delete = delete an entity
- destroy = used to destroy structs & hud elements
These are just some of the few keywords available. Below I will give you examples of each.
wait | this will loop every 5 seconds
self | the player who called this script will be killed after 5 seconds
level | grab an array of all the players
waittill | this script will wait until 'start_me' is notified
notify | this will trigger any waittill’s with the same keyword, like the one above
endon | this functions dies when 'Kill_Me' is notified
delete | this will delete an entity
destroy | this will destroy a hud elem
For a full list of all the available functions & keywords, you can go to Scripting Reference (Wiki) or InfinityWard Scripting index
If/Else Statements
If/Else statements are conditional statements that are very, very useful. They can check thestate of a variable, make decisions based on data and many other cool things.
If/Else statements are actually very easy to learn. So lets dive right in:
In the above example, we initialize a variable called my_num to 5. Then we use an IF()statement to check if the number is equal to 5. If it is, we print the message 'The number isfive!', and if the number is not 5, we then display the message 'The number is NOT five!'. If werun the script like it is, we would see the first message, however, if we changed the variablemy_num at the top, to another number, the second message would be displayed because theIF() check would see that my_num does not equal 5, and so it wall fall back to the elsestatement.
You can also do multiple levels of If/Else statements. These are If/Else If/Else statements. Theylook like:
Firstly, if you are wondering why the Curly Brackets have all disappeared, its because there’s aneat trick with If, Else If, Else, For Loops and While Loops, and that is… if your statement willonly contain 1 line of code to execute, you don’t need to put curly brackets in. However, if it ismore than one line of code to be executed, then you have to put the curly brackets in.
We see in the above example that the code will now check to see if num is equal, higher orlower than 20.
Another neat trick with If, Else If, Else, For Loops and While Loops is that if you’re variable is aBoolean variable, meaning it’s either true or false, you can just type in the variable name tocheck if it’s true, or put the bang character '!' in-front of the name to check if it’s false. Seebelow for an example.
In the above example, the if( is_dead ) is checking if is_dead equals true, and the else if(!is_dead ) is checking whether is_dead is false, and in this case it is, so the player wouldreceive the message 'YAY! You’re not dead'.
In most programming languages, zero is considered false, and anything non-zero is true.Some good operators for if/else statements are && (Meaning AND), || (Meaning OR), (Meaning EQUALS) and != (Meaning DOES NOT EQUAL)
Switch Blocks
Switch blocks are usually used when you need to do multiple checks where and If/Else Ifstatement would be too long, or messy to type out.Lets take a look at a switch statement:
Now, don’t get too worried about it’s looks, yea, they look mean, but they can be a lot moreuser friendly to use that huge blocks of If/Else statements.
In the example, we start by declaring a variable my_number. We then start the switch blockwith the keyword switch and then in brackets what variable we will be comparing. Then, insidethe curly brackets, we start the comparisons. We start one comparison with the keyword casefollowed by what we want to compare it too, then end it with a colon. Under that, we placethe code we want executed if the variable equals the case, we then follow that with thebreak; keyword to let the script know we’re done inside the switch block. At the very bottom,we add a keyword called default followed by a colon. Under here we put the code that will beexecuted if the variable doesn’t match any of the cases, again, followed by a break;
Let’s Start
All right, you feel like making a mod yet? Well I hope you do!To follow along with this book, you need to install the mod tools for Call of Duty 4. These canbe found here CoD4 Mod Tools.
With the mod tools installed, you’ll have access to all the raw files needed to start scripting amod for the game.
If you’re mainly wanting to script for CoD4, you will only need to turn your attention to therawmaps folder. This is where all the raw scripts for Call of Duty 4 are located.Whenever you see [root] below, its an alias for you Call of Duty 4 installation directory, whichis defaults too
C:Program FilesActivisionCall of Duty 4 - Modern Warfare
OK, you’re ready to go!
Example Anti Camp Script
For our first task, I’ll teach you how to script an anti camp function. From now on, I’ll refer tothe anti camp function as the ACF.
So what is needed in an ACF? Well, we need to monitor the players to make sure they are notsitting in one spot for too long. If they are sitting in one spot for too long, lets warn them thatthey are camping & give them 10 seconds to move. If they don’t move within those 10 seconds,we’ll punish them by killing them.
So already we can see we need a couple of things:
- A while loop that constantly monitors the player
- A function to somehow see how far a player has moved in a certain amount of time
- A function to punish the player
- A timer to check against.
So, lets start with our variables:
- my_camp_time will be the time in seconds the player has sat in one spot.
- have_i_been_warned will either be false if they haven’t been warned about camping, or true if they have been warned.
- max_distance is the distance the player needs to move within the camp_time limit to not be considered a camper.
Now, we need to somehow check how far the player has moved on a constant basis. If theyhaven’t moved further than the max_distance we need to increment the my_camp_timevariable. Then we need to check my_camp_time against camp_time, and if they are equal, weneed to warn the player & set the have_i_been_warned to true. We then give the player 10seconds to move, and if he doesn’t. bam! We kill him.
So, I head over to Scripting Reference and look for a distance function, and what doyou know? They have one called distance() and distance2d(). The distance2d() looks like theone we need. So lets set-up our while loop:
Now, that alone won’t do anything, so, lets throw it in a custom function called _AntiCamp().
Path 1:OK, if you have the mod tools installed, go to [root]rawmapsmpgametypes.
Path 2:Now open a new window, and navigate to [root]mods. Inside that folder, make a newfolder called MyModTest. Inside that folder, build a folder structure like this: mapsmpgametypes.
Now, take dm.gsc from Path 1, and place it in MyModTestmapsmpgametypes. Now open it in Programmers Notepad.
With it open, copy & paste your function to the bottom of the file, after all the other functions.Then, find the function onPlayerSpawn(), and at the bottom of the function type:
What this does is 'threads' our function. Threading means the function will be called & thecode will carry on without it’s return happily. If we don’t place the 'thread' keyword there, thescript would stop and wait until _AntiCamp() has finished, and then continue.
You can find the working source code here.To run a mod, start up cod4, then go to mods, select your mod and hitlaunch.
To test this mod out, make a new Free-For-All server, sit still for 30 seconds and watch youranti camp script in action!
Well I hope you enjoyed the Handbook, and that I have inspired you to start scripting!Thank you for reading.
Copyright (C) 2008 Tristan S. Strathearn.
All Rights Reserved
Original title: Quake-C CoD Handbook
Retrieved from 'https://wiki.zeroy.com/index.php?title=Call_of_Duty_4:_CoD_Script_Handbook&oldid=16480'