You are on page 1of 13

Mission 7

Threading Functions

https://roderickvella.wordpress.com

Mission Objectives
Welcome to your 7th mission. In this
mission we are going to learn how to
use threading and wait commands.

What is threading?

Lets start by understanding the wait


command:

self iPrintlnBold(Hello);

STEP1: Display Hello

wait(10);

STEP2: Pause script for 10 seconds

self iPrintlnBold(World);

STEP3: Display World

What is threading?
THIS IS NOT THREADING. FUNCTION1 HAS TO WAIT FOR FUNCTION2 TO DISPLAY MALTA
function1()
{
function2();

STEP 1: Calls function2()

self iPrintlnBold(Malta);

STEP 5: Display Malta

STEP 2: Display Hello

function2()
{
self iPrintlnBold(Hello);

STEP 3: Pause for 10 seconds

wait(10);

STEP 4: Display World

self iPrintlnBold(World);
}

What is threading?

Threading a function is calling a function


without having your script wait for the
function to finish or return a value
Threading is as simple as putting "thread" in
front of your function call:
function(); // Regular function call
thread function(); // Threaded function call

What is threading?
THIS IS THREADING. FUNCTION1 DOES NOT HAVE TO WAIT FOR FUNCTION2 TO DISPLAY
MALTA
function1()
{
thread function2();

STEP1: Calls function2() in threaded


mode
STEP 2: Display Malta

self iPrintlnBold(Malta);
}

STEP 2: Pause for 10 seconds

function2()
{
wait(10);

STEP 3: Display Hello

self iPrintlnBold(Hello);

STEP 4: Display World

self iPrintlnBold(World);
}

Example: Mission 7 Prog 1

In this example we are going to create a


MOD that shows and hides the player every
5 seconds.

Example: Mission 7 Prog 1

Step 1

Code the following:

Notes on Mission 7 Prog 1


Line 132

thread
showHidePlayer();

Calls function showHidePlayer();


using threading

Line 137

self Hide();

Hides the player from other players

Line 138

wait(5);

Waits 5 seconds before executing Line


139

Line 139

self Show();

Shows the player to other players

Step 2

Test this against your friends and notice that


the players only disappear and reappear for
few times on re-spawn.
What if we want the players to hide and
show themselves forever?
This will lead us to our next Mission: Loops

You might also like