Вы находитесь на странице: 1из 51

Mission 8

Loops

https://roderickvella.wordpress.com

Mission Objectives
Welcome to your 8th mission. In this
mission we are going to learn how to
use loops.

Loops
Are used when you want to run the
same code over and over again
Loops allow you to repeat a process a
set amount of times, or a limited
amount of times.
Loops can be used to monitor variables,
print messages or check players.
Anything that needs constant attention
or repeating.

While Loops
The while loop loops through a block of
code as long as a specified condition is
true
while (condition) {
code block to be executed
}

While Loops
Explanation 1
number = 1;
while( number < 5)
{
number = number + 1;
}

Keeps repeating IF number is


smaller than 5. IF the
statement is true.

For how many times does the above loop run?

While Loops
number = 1;
while( number < 5)
{
number = number + 1;
}

number = 1;
while( number < 5)
{
number = number + 1;
}

STEP1: number is set to 1

STEP2: Is number smaller than 5? Yes


statement is true. Then go inside the
loop.

While Loops
number = 1;
while( number < 5)
{
number = number + 1;
}

number = 1;
while( number < 5)
{
number = number + 1;
}

STEP3: number is set to 2

STEP4: Is number still smaller than 5?


Yes statement is true. Then go inside
the loop.

While Loops
number = 1;
while( number < 5)
{
number = number + 1;
}

number = 1;
while( number < 5)
{
number = number + 1;
}

STEP5: number is set to 3

STEP6: Is number still smaller than 5?


Yes statement is true. Then go inside
the loop.

While Loops
number = 1;
while( number < 5)
{
number = number + 1;
}

number = 1;
while( number < 5)
{
number = number + 1;
}

STEP7: number is set to 4

STEP8: Is number still smaller than 5?


Yes statement is true. Then go inside
the loop.

While Loops
number = 1;
while( number < 5)
{
number = number + 1;
}

number = 1;
while( number < 5)
{
number = number + 1;
}

STEP9: number is set to 5

STEP10: Is number still smaller than


5? No statement is false. Then dont
go inside the loop.

Therefore the loop runs for 4 times.

While Loops
Explanation 2
flag = true;
number = 1;

while(flag)
{
number = number + 1;
if(number >= 3)
{
flag =false;
}
}

Keeps repeating if flag is


true. IF the statement is true.

For how many times does the above loop run?

While Loops
flag = true;
number = 1;
while(flag)
{
number = number + 1;
if(number >= 3)
{
flag =false;
}
}

STEP1: Set flag variable to true


STEP2: Set number variable to 1

While Loops
flag = true;
number = 1;
while(flag)
{
number = number + 1;
if(number >= 3)
{
flag =false;
}
}

STEP3: Is condition true? Yes, it is.


flag is true.

While Loops
flag = true;
number = 1;
while(flag)
{
number = number + 1;
if(number >= 3)
{
flag =false;
}
}

STEP4: Set number to 2

While Loops
flag = true;
number = 1;
while(flag)
{
number = number + 1;
if(number >= 3)
{
flag =false;
}
}

STEP5: Is number greater or equal


to 3? No number is still 2 so dont
go inside the if..statement

While Loops
flag = true;
number = 1;
while(flag)
{
number = number + 1;
if(number >= 3)
{
flag =false;
}
}

STEP6: Is condition true? Yes, it is.


flag is true.

While Loops
flag = true;
number = 1;
while(flag)
{
number = number + 1;
if(number >= 3)
{
flag =false;
}
}

STEP7: number is set to 3

While Loops
flag = true;
number = 1;
while(flag)
{
number = number + 1;
if(number >= 3)
{
flag =false;
}
}

STEP9: Is number greater or equal


to 3? Yes it is. Then go inside the
if..statement

While Loops
flag = true;
number = 1;
while(flag)
{
number = number + 1;
if(number >= 3)
{
flag =false;
}
}

STEP10: flag is set to false

While Loops
flag = true;
number = 1;
while(flag)
{
number = number + 1;
if(number >= 3)
{
flag =false;
}
}

STEP11: Is condition true? No, it is


not. flag is false. Therefore the
execution wont go inside the
loop.

Therefore the loop runs for 2 times.

Example: Mission 8 Prog 1


In this example we are going to create a
MOD that takes the players weapons
after 10 seconds.

Example: Mission 8 Prog 1

Step 1

Notes on Mission 8 Prog 1


Line 132

thread
noWeaponsInTenSeconds();

Calls function
noWeaponsInTenSeconds();
using threading

Line 137

self endon ("disconnect"); This command is used to


automatically destroy the function
when the player disconnects. It is
important to write it when using
threaded functions. If not written,
the player can end up with multiple
instances of the same function upon
reconnecting.

Line 138

self endon ("death");

This command is used to


automatically destroy the function
when the player dies. It is important
to write it when using threaded
functions. If not written, the player
can end up with multiple instances
of the same function upon respawning.

Notes on Mission 8 Prog 1


Line 140

Do not execute the rest of the


self
waittill("spawned_player"); code in function
noWeaponsInTenSeconds()
until the player has fully spawned
on the map. Some commands
work after the player has
spawned.

Line 150

self takeAllWeapons();

Remove all the weapons of the


player

For Loops

The for loop has the following syntax


for (statement 1; statement 2; statement 3) {
code block to be executed
}

Statement 1 is executed before the loop (the code block)


starts.
Statement 2 defines the condition for running the loop (the
code block).
Statement 3 is executed each time after the loop (the code
block) has been executed.
Each statement is separated by a semicolon

For Loops Explanation 1


result = 5;
for(number = 0; number<2; number++)
{
result = result + number;
}

Variable Debug Box

STEP1: result is set to 5

For Loops Explanation 1


result = 5;
for(number = 0; number<2; number++)
{
result = result + number;
}

Variable Debug Box


result =5;

STEP2: number is set to 0

For Loops Explanation 1


result = 5;
for(number = 0; number<2; number++)
{
result = result + number;
}

Variable Debug Box


result =5;
number =0;

STEP3: Is number smaller


than 2? Yes it is.

For Loops Explanation 1


result = 5;
for(number = 0; number<2; number++)
{
result = result + number;
}

Variable Debug Box


result =5;
number =0;

STEP4: result is 5+0 = 5.


Therefore result should
become 5.

For Loops Explanation 1


result = 5;
for(number = 0; number<2; number++)
{
result = result + number;
}

Variable Debug Box


result =5;
number =0;

STEP5: number++ is
equivalent to number =
number +1; Therefore
number should become 1

For Loops Explanation 1


result = 5;
for(number = 0; number<2; number++)
{
result = result + number;
}

Variable Debug Box


result =5;
number =1;

STEP6: Is number smaller


than 2? Yes it is.

For Loops Explanation 1


result = 5;
for(number = 0; number<2; number++)
{
result = result + number;
}

Variable Debug Box


result =5;
number =1;

STEP8: Increase result


by adding number

For Loops Explanation 1


result = 5;
for(number = 0; number<2; number++)
{
result = result + number;
}

Variable Debug Box


result =6;
number =1;

STEP9: number = number +1

For Loops Explanation 1


result = 5;
for(number = 0; number<2; number++)
{
result = result + number;
}

Variable Debug Box


result =6;
number =2;

STEP10: Is number < 2? No,


then dont go inside the loop.

For Loops Explanation 1


result = 5;
for(number = 0; number<2; number++)
{
result = result + number;
}

Variable Debug Box


result =6;

STEP 11: Outside loop

Example: Mission 8 Prog 2


In this example we are going to create a
MOD that after 10 seconds the player is
given an airstrike for free.

Example: Mission 8 Prog 2

Step 1

Notes on Mission 8 Prog 2


Line 142

for(i=0;i<10;i++)

Repeat for 10 times.


From 0 to 9.

Line 148

giveHardpointItem("airstrike_mp"); Gives an airstrike to the


player

Infinite Loops

Infinite loops are used a lot in COD Script


and games
Infinite loops are loops that continue forever
For Loops and While Loops can be used to
produce an infinite loop. It doesnt matter
which one you choose.
Be very careful with infinite loops. They can
crash your game if not used in threaded
functions.

Infinite Loops

While Loop Infinite Loop


The condition is always
TRUE so it is never going
to stop from looping.
while(true)
{
IprintLnBold( "This is a looping message" );
wait 5;
}

Infinite Loops

For Loop Infinite Loop

for(;;)
{
IprintLnBold( "This is a looping message" );
wait 5;
}

Example: Mission 8 Prog 3


In this example we are going to create a
MOD that eliminates the player that has
been camping for 10 seconds using an
infinite loop.

Example: Mission 8 Prog 3

Step 1

Step 2

Notes on Mission 8 Prog 3


Line 148

self.origin

The players position

Line 152

distance2d(oldPosition,newPosition);

Returns the distance


between two points,
ignores height
difference.

Notes on Mission 8 Prog 3


Line 155

timeSpentCamping++;

Equivalent to
timeSpentCamping =
timeSpentCamping +1;

Line 166

self suicide();

Kills the player immediately as


a suicide

Mission 8 Task 1
Update the code we created in our last mission.
Make the player hide and show himself using an
infinite loop

Вам также может понравиться