You are on page 1of 24

Mission 6

Functions

https://roderickvella.wordpress.com

Mission Objectives
Welcome to your 6th mission. In this
mission we are going to learn how to
use functions and variable scopes

Functions

A function is a block of code designed to


perform a particular task.

A function is executed when "something" invokes it


(calls it)

my_function()
{
return 5;
}

Functions

Function names cannot contain anything except


letters, numbers and underscores.
Function names cannot start with a number or
contain spaces.

my_function()
{
return 5;
}

Functions

the_number_five variable now contains the


value 5 since my_function returned the value 5
to it through the return keyword
my_function()
{
return 5;
}
the_number_five = my_function();

Functions

Parameters allow you to pass data into the


function to work with
add_five( my_parameter )
{
temp = my_parameter;
temp = temp + 5;
return temp;
}
not_yet_ten = 5;
this_is_ten = add_five( not_yet_ten );

Functions

If you want many parameters you can use


commas
add_numbers( number_one, number_two, number_three )
{
return number_one + number_two + number_three;
}
the_result = add_numbers( 1, 2, 3 );

Functions & Variable Scopes

A scope is any section between the opening


brace "{" and the closing brace "}". Any variables
declared inside this scope can only be used inside
this scope, and not anywhere outside it
function()
{
local_var = 1;
}
result = local_var;
Compile Error, local_var is declared inside function()
Therefore, it cant be called from outside.

Functions & Variable Scopes

To avoid this problem, one can use global


variables.
onSpawnPlayer()
{
self.global_variable = 0;
self iPrintlnBold(self.global_variable);
changeValue();
self iPrintlnBold(self.global_variable);

self. has to be
used to declare a
global variable.
self = an alias to
the entity that
called the script. In
this case the player
who called the
script.

}
changeValue()
{
self.global_variable = 1;
}

Example: Mission 6 Prog 1

We are going to create a MOD that calls a


function that displays text on the screen and
creates an earthquake.

Step 1

Code the following in a new MOD:

Notes on Mission 6 Prog 1


Line 133

startEarthQuake();

A function call. No arguments are used.

Line 140

Earthquake( 0.3, 30,


self.origin, 850 );

0.3 is the scale of the quake


30 is the duration in seconds
Self.origin is representing where the
earthquake is going to take place.
Which in this case is the players
position
850 is the radius of the earthquake

Step 2

Example: Mission 6 Prog 2

We are going to amend our function


startEarthQuake(). It is going to accept an
earthQuakeDuration parameter.
IF the name of the player is GODMODE
THEN the earthquake should last for 5
seconds
ELSE it should last for 10 seconds

Step 1

Notes on Mission 6 Prog 2


Line 133

playerName = self.name;

Variable playerName is storing the


players name

Line 148

startEarthQuake(earthQu
akeDuration)

Function that accepts


earthQuakeDuration as parameter

Line 138

startEarthQuake(5);

Pass argument 5 to function


startEarthQuake(earthQuakeDur
ation)

Line 143

startEarthQuake(10);

Pass argument 10 to function


startEarthQuake(earthQuakeDur
ation)

Line 148

startEarthQuake(earthQu
akeDuration)

Function that accepts and


earthQuakeDuration parameter

Line 152

Earthquake( 0.3,
earthQuakeDuration,
self.origin, 850 );

earthQuakeDuration is the value


that was previously passed.

Mission 6 Task 1
Amend the previous code. The
startEarthQuake() function should now
accept 2 parameters. earthQuakeDuration
and earthQuakeScale.

IF the name of the player is GODMODE


THEN the earthquake should last for 5
seconds with a quake scale of 1
ELSE it should last for 10 seconds with a
quake scale of 0.5

Example: Mission 6 Prog 3

We are going to create a new MOD that randomly


sets how much the player can jump. The
following command has to be used:

self setclientDvar("jump_height", 39);

How much the


player can
jump. The
larger the
value, the more
the player can
jump

Step 1

Notes on Mission 6 Prog 3


Line 132

jumpHeightValue =
changeJumpSettings(1001);

Function
changeJumpSettings() is
called with an argument of 1001.
The return value that this function
returns is storred inside
jumpHeightValue

Line 137

changeJumpSettings(maxPoss
ibleJumpHeight)

Function that accepts


maxPossibleJumpHeight
parameter

Line 139

myRandomHeight =
RandomInt(maxPossibleJumpH
eight);

Variable myRandomHeight is
storing a random number
between 0 and 1000 (both
inclusive)

Line 140

self
setclientDvar("jump_height
", myRandomHeight);

Updating the jump height to the


random value.

Notes on Mission 6 Prog 3


Line 142

return myRandomHeight;

The function is returning the


random value.

Line 133

self iPrintlnBold("Jump
Height set to: " +
jumpHeightValue);

Displaying on the screen the value


returned by the function in line
142.

Step 2

Mission 6 Task 2
As you can see in Mission 6 Prog 3 video, the
player is dying on fall when the jumping height is
set too high.
Update the program so that it wont allow the
player to die when he falls back to the ground.
Hint: Use and amend the following code:
self setclientDvar("bg_fallDamageMinHeight", 1);

You might also like