Mission 8 Loops https://roderickvella.wordpress.com
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
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
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.
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.
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.
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.
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.
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?
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 don’t 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 won’t 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 player’s weapons after 10 seconds.
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 re-spawning.
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
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; }
STEP1: result is set to 5
Variable Debug Box
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;
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;
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;
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;
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;
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;
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;
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;
For Loops – Explanation 1
result = 5; for(number = 0; number<2; number++) { result = result + number; } STEP 11: Outside loop
Variable Debug Box result =6;
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.
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 doesn’t 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
while(true) { IprintLnBold( "This is a looping message" ); wait 5; }
The condition is always TRUE so it is never going to stop from looping.
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.
Step 1
Step 2
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.
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