Level 08 Lesson 03 [Returning Values]

In Level 07 we took a quick look at some GameMaker methods/scripts that return values back to you after you use them. In this lesson you will learn how to make your methods return values back to you. It will be an important concept to understand because in future lessons you will write scripts that do this when you are challenged with more complicated tasks in your games.

Part A – Returning Values
Watch the video: 08-03-A
DoneFile: Lv08_03_A.gmk

Challenge 1 – hpStatus and getBarColor
Watch the video: 08-03-X1Preview to preview the challenge.
Use: 08-03-A.gmk to start

Here are two tasks to practice a bit of value returning.

a) Give the player a new variable called healthStatus. healthStatus is either “healthy” (50-100 hp), “warning” (25-49 hp), or “danger!” (below 25 hp). Use the draw object to draw this variable above the health bar.
Now write a script called
hpStatus that returns the appropriate word based on the player’s hp. This script requires no arguments.
Each time you set, give, or take away hp from the player (in the givePlayerPoints and damagePlayer scripts) you should call the script so that it determines the appropriate value for
hpStatus.

Hint: you’ll use a line like healthStatus=hpStatus() somewhere in there!

b) The health bar is currently always drawn in white. Write a script called getBarColor that requires one argument; the player’s hp. It will use the argument and return a color so you can draw the health bar in an appropriate color (green if 50 or above, orange if 25-49, and red if below 25).
To return a color in your script you can use a line like this: return(c_red)

Once the script is made, can you figure out how to use it in the draw object to easily set the color of the health bar rectangle when it is drawn? If not just peek at the solution video. This is a nice example of how scripts can be used.

Challenge 1 Solution
Solution Video: 08-03-X1
Solution File: 08-03-X1Done.gmk