Top Banner
Mission 8 Loops https://roderickvella.wordpress.com
51
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mission8.pdf

Mission 8 Loops

https://roderickvella.wordpress.com

Page 2: Mission8.pdf

Mission Objectives

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

Page 3: Mission8.pdf

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.

Page 4: Mission8.pdf

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 }

Page 5: Mission8.pdf

While Loops

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?

• Explanation 1

Page 6: Mission8.pdf

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

STEP1: number is set to 1

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

STEP2: Is number smaller than 5? Yes statement is true. Then go inside the loop.

Page 7: Mission8.pdf

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

STEP3: number is set to 2

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

STEP4: Is number still smaller than 5? Yes statement is true. Then go inside the loop.

Page 8: Mission8.pdf

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

STEP5: number is set to 3

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

STEP6: Is number still smaller than 5? Yes statement is true. Then go inside the loop.

Page 9: Mission8.pdf

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

STEP7: number is set to 4

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

STEP8: Is number still smaller than 5? Yes statement is true. Then go inside the loop.

Page 10: Mission8.pdf

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

STEP9: number is set to 5

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

STEP10: Is number still smaller than 5? No statement is false. Then don’t go inside the loop.

Therefore the loop runs for 4 times.

Page 11: Mission8.pdf

While Loops

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.

• Explanation 2

For how many times does the above loop run?

Page 12: Mission8.pdf

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

Page 13: Mission8.pdf

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.

Page 14: Mission8.pdf

While Loops

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

STEP4: Set number to 2

Page 15: Mission8.pdf

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 don’t go inside the if..statement

Page 16: Mission8.pdf

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.

Page 17: Mission8.pdf

While Loops

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

STEP7: number is set to 3

Page 18: Mission8.pdf

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

Page 19: Mission8.pdf

While Loops

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

STEP10: flag is set to false

Page 20: Mission8.pdf

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 won’t go inside the loop.

Therefore the loop runs for 2 times.

Page 21: Mission8.pdf

Example: Mission 8 – Prog 1

• In this example we are going to create a MOD that takes the player’s weapons after 10 seconds.

Page 22: Mission8.pdf

Example: Mission 8 – Prog 1

Page 23: Mission8.pdf

Step 1

Page 24: Mission8.pdf

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

Page 25: Mission8.pdf

Notes on Mission 8 Prog 1 Line 140 self

waittill("spawned_player"); Do not execute the rest of the 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

Page 26: Mission8.pdf

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

Page 27: Mission8.pdf

For Loops – Explanation 1

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

STEP1: result is set to 5

Variable Debug Box

Page 28: Mission8.pdf

For Loops – Explanation 1

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

STEP2: number is set to 0

Variable Debug Box result =5;

Page 29: Mission8.pdf

For Loops – Explanation 1

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

STEP3: Is number smaller than 2? Yes it is.

Variable Debug Box result =5; number =0;

Page 30: Mission8.pdf

For Loops – Explanation 1

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

STEP4: result is 5+0 = 5. Therefore result should become 5.

Variable Debug Box result =5; number =0;

Page 31: Mission8.pdf

For Loops – Explanation 1

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

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

Variable Debug Box result =5; number =0;

Page 32: Mission8.pdf

For Loops – Explanation 1

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

STEP6: Is number smaller than 2? Yes it is.

Variable Debug Box result =5; number =1;

Page 33: Mission8.pdf

For Loops – Explanation 1

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

STEP8: Increase result by adding number

Variable Debug Box result =5; number =1;

Page 34: Mission8.pdf

For Loops – Explanation 1

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

STEP9: number = number +1

Variable Debug Box result =6; number =1;

Page 35: Mission8.pdf

For Loops – Explanation 1

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

STEP10: Is number < 2? No, then don’t go inside the loop.

Variable Debug Box result =6; number =2;

Page 36: Mission8.pdf

For Loops – Explanation 1

result = 5; for(number = 0; number<2; number++) { result = result + number; } STEP 11: Outside loop

Variable Debug Box result =6;

Page 37: Mission8.pdf

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.

Page 38: Mission8.pdf

Example: Mission 8 – Prog 2

Page 39: Mission8.pdf

Step 1

Page 40: Mission8.pdf

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

Page 41: Mission8.pdf

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 doesn’t matter which one you choose.

• Be very careful with infinite loops. They can crash your game if not used in threaded functions.

Page 42: Mission8.pdf

Infinite Loops

• While Loop – Infinite Loop

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

The condition is always TRUE so it is never going to stop from looping.

Page 43: Mission8.pdf

Infinite Loops

• For Loop – Infinite Loop

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

Page 44: Mission8.pdf

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.

Page 45: Mission8.pdf

Example: Mission 8 – Prog 3

Page 46: Mission8.pdf

Step 1

Page 47: Mission8.pdf

Step 2

Page 48: Mission8.pdf

Notes on Mission 8 Prog 3 Line 148 self.origin The player’s position

Line 152 distance2d(oldPosition,newPosition); Returns the distance between two points, ignores height difference.

Page 49: Mission8.pdf

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

Page 50: Mission8.pdf

Mission 8 – Task 1

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

Page 51: Mission8.pdf