Sei sulla pagina 1di 20

Mission 5

If/Else Statements, Switch Blocks


& Random Numbers

https://roderickvella.wordpress.com

Mission Objectives
Welcome to your fifth mission. In this
mission we are going to learn how to
use if conditions and random
numbers.

If/Else Statements

Perform different actions for different


decisions
Use if to specify a block of code to be
executed, if a specified condition is true
Use else to specify a block of code to be
executed, if the same condition is false
Use else if to specify a new condition to test,
if the first condition is false
Use switch to specify many alternative
blocks of code to be executed

The if Statement
my_num = 5;
if( my_num == 5 )
{
self IprintLnBold( "The number is five!" );
}
else
{
self IprintLnBold( "The number is NOT five!" );
}

The if Statement
my_name = "John";
if(my_name == "John" )
{
self IprintLnBold("Name is John");
}
else
{
self IprintLnBold("Name is not John");
}

Multiple levels of If/Else statements


num = 20;
if( num == 20 )
{
self IprintLnBold( "Num equals 20" );
}
else if( num > 20 )
{
self IprintLnBold( "Num is greater than 20" );
}
else
{
self IprintLnBold( "Num is less than 20" );
}

Switch Blocks
my_number = 5;
switch( my_number )
{
case 5:
self IprintLnBold(
break;
case 6:
self IprintLnBold(
break;
case 7:
self IprintLnBold(
break;
default:
self IprintLnBold(
break;
}

"The number is 5" );

"The number is 6" );

"The number is 7" );

"The number is not 5, 6, or 7" );

Random Numbers

RandomInt( <max> )
Returns a random integer between 0 and max-1
inclusive
myRandomValue = RandomInt(5);

myRandomValue can hold from 0 to 4

Example: Mission 5 Prog 1

We are going to create a MOD where the user can


have a random limitation on spawn. The possible
limitations are:
1. No Jumping
2. No Sprinting
3. No Aim Down Sight

Step 1
Create a new mod like we did in Mission
3 and name it Mission 5

Code the following

Step 2

Notes on Mission 5 Prog 1


Line 122

myRandomValue

A variable that is holding a random


number

Line 122

RandomInt(3)

Produces a random number. Possible


values: 0,1,2

Line 124

if(myRandomValue == 0)

If the random number produced is 0


then do not allow jumping.

Line 126

//Do not allow jumping

A comment. Code which is not


executed.

Line 127

iPrintlnBold("^2Jumping
Not Allowed");

^2 means green colour

Line 128

AllowJump(false)

Accepts true/false. Allows/Disallows


the user from jumping.

Line 133

AllowSprint(false);

Accepts true/false. Allows/Disallows


the user from sprinting.

Line 138

self AllowAds(false);

Accepts true/false. Allows/Disallows


the user from using aim down sight.

Run server with MOD

Step 3

Practical Tasks

Mission 5 Task 1
Create the same MOD we did in Mission 5
Prog 1. This time use a Switch instead of an If
statement. Test it with your friends.

Mission 5 Task 2
Create a MOD that checks your name.
IF your name is GODMODE THEN
1. On the screen the following message
should appear: You are in
GODMODE
2. The player should have a lot of health.
ELSE
1. On the screen the following message
should appear: You are not in
GODMODE

Mission 5 Task 2
Hint: Use the below code for maximum health:
self.maxhealth = 90000;
self.health = self.maxhealth;

Mission 5 Task 2
Testing: Change your name to GODMODE

Mission 5 Task 2
Testing: Test your code by throwing a grenade at
yourself

Mission 5 Task 2
Testing: Play against your friends and test your
new abilities.

Potrebbero piacerti anche