Creating an NPC in GameMaker
Most game have NPCs (Non-player characters) that act independently. NPCs could be companions or enemies or just
background characters to the game. In most cases such NPCs will move around the screen on their own power. Most
Game Engines have tools to manage the behavior of NPCs. In GameMaker one of the tools is to create a Path and then
assign the NPC to the path when the game starts. A Path in GameMaker is an object that consists of a set of points on
the screen in a particular order. When an NPC is assigned to a Path it will automatically follow the path as the game
runs.
Using a Path consists of:
A. Creating a Path object
B. Assigning the Path object to an NPC object
C. Setting the path variables.
When the game runs, each time through the game loop, the object assigned to the path will move along the path. The
default Path actions for NPCs include moving and performing a defined action when the end of the path is reached.
Along with setting an NPC on a path, the game programmer can also control the actions of the NPC while they are on the
path. This is done by adding a scripting component to the object and checking, each time through the game loop, what
percent of the path has been completed. The events that the NPC needs to listen for are:
Create Event – happens when the game starts and the NPC object is created
Step Event – happens each time the game loop is executed
The actions behind each of these events include:
Create Event → initialize the object and assign the Path object to the NPC object and start the NPC on the path
Step Event → check the position of the object on the path and perform actions based on position
Using GameMaker 8.1
Using GameMaker Studio 2.0
NPCs in GameMaker 8.1
1. Download and unzip the BraidGraphics file.
2. Start a new GameMaker document.
3. Add the following sprites located in the indicated folders in the braidgraphics folder
animation-set/greeter-walk-50-w.gif – name this sprite dinoleft
animation-set/monstar-walk-w.gif – name this sprite monsterright
icons_128/door1.png – name this sprite door
icons_128/flower.png – name this sprite flower
4. For each sprite, make sure you click the Center button to set the sprite origin.
5. Create a background by loading the background image named bg_4_02.jpg – name this level1back
6. Create a room.
7. Click the settings tab and name the room level1 and set the size as 1280 by 720.
8. Click the background tab and add the level1back background.
9. Create objects for each sprite and name the objects dinoobj, monsterobj, door, and flower.
You will next add a path and behaviors for some of the objects.
10. Right click the Path folder and choose Create path.
11. In the path window click the show room button and choose the room show the room.
12. Set the Snap X and Snap Y values as 32 if they are not set to this value.
13. Click points on the screen to set positions. Lines will automatically be drawn between position. Point to a path
position and click and drag to resize the path.
14. Change the path name from Path0 to MonsterPath and click the green check mark to save the path.
15. Open the Monster object and click Add Event.
16. Choose the Create Event.
17. Click the Path button and drag it into the Actions area
18. In the Set path dialog box choose the Monster Path, set the speed to 4, and set the At End action to reverse.
19. Click OK to set the path for this monster. This path will be automatically loaded when the monster is created (at
the start of the game).
20. Open the Dino object. Add events Press Left, Press Right, Release Left, and Release Right.
21. For the Press Left and Press Right add the move fixed button and move left/right and set a speed 5
22. For the Release Left and Release Right add the move fixed button and set the speed 0
23. Open the room and click the Objects tab.
24. Add the Monster object towards the top part of the screen, add the Dino object and the door object towards the
lower part of the screen.
25. Run your program.
NPCs in GameMaker Studio 2.0
1. Start a new GameMaker 2.0 project.
2. Add the following sprites located in the indicated folders in the braidgraphics folder
animation-set/greeter-walk-50-w.gif – name this sprite dinoleft
animation-set/monstar-walk-w.gif – name this sprite monsterright
icons_128/door1.png – name this sprite door
icons_128/flower.png – name this sprite flower
bg_4_02.jpg – name this sprite background
3. Open the room object and set the size as 1280 by 720.
4. Click the Background link and add the background sprite Background.
5. Create objects for each sprite and name the objects dinoobj, monsterobj, doorobj, and flowerobj.
6. Create a new Path object.
7. Name this path Monsterpath and click to set the following points:
8. Open the Monster object and click Add Event.
9. Choose the Create Event.
10. In the Create Event window type the word Path.
11. Drag the first red path icon into the area. You see options for the path.
12. Choose the MonsterPath for the path name, set the path speed to 4 and change the On End action to Reverse.
13. Open the Dino objects.
14. In the Add Event choose the Key Down left option.
15. Type the word Move in the search window.
16. Chose the first blue arrow icon with four arrows and drag it into the edit window.
17. Set the direction arrow to left.
18. Type the search term Speed.
19. Drag the blue icon under the Movement section into the edit window
20. Set the speed to 5. The direction taken from the Direction Fixed will be used.
21. Add an event Key Down right and use these same steps to set a move to the right and set the speed to 5.
22. Add a Key Up Left event and set a speed of zero.
23. Add a Key Up Right event and set the speed of zero.
24. Go back to your Room window and click the Instances option.
25. Drag your objects into the room. Place the monster at the top of the screen and the dino and door along the
bottom.
26. Run your game.
Using Scripts
While simple games can be created by using the GameMaker drag and drop tools, real games require that scripts be
used in place of the graphical tools. Scripts, like any other actions, must be attached to game events. Most of these
events are the create event and the step event. The Create Event is executed when the object is create d or when the
game starts. When used in the Create Event a script typically is used to create and initialize variables which will be used
in the other scripts. The Step event is executed each time through the game loop. When scripts are attached to the Ste p
Event they will be executed for each step event, each time through the game loop.
In the case of our simple game program, we want to do the following:
Dino Object
Create Event – define and initialize variables/flags
Step Event – check the keyboard for left/right keys, move the character, flip the sprite
Monster Object
Create Event – set the object onto a path
Step Event – check the position on the path and flip the sprite
There are some fundamental concepts that are useful to understand with scripting.
Variables are named by the designer and used to hold information. Variables can be assigned values by placing the
variable on the left side of an equal sign and the data to be assigned on the right side of the equal.
Myspeed = 10
Variables can also be placed on the right side of an equal sign and, if used this way, will take the value of the variable
and assign it to the variable on the left side of the equal sign.
Myspeed = 10
Xspeed = Myspeed
Xspeed now has the value 10
When choosing a name for a variable the game programmer cannot use a name already used and the variable name is
case sensitive. For example, the variables named Myspeed and myspeed are two different variables.
Another important concept is data types. This refers to what kind of data the scripts use. The basic data types are:
Boolean values = true or false
numbers = 12 or 34.44
text = “abcd”
Along with assigning variables the programmer must also understand expressions. An expression includes an assignment
operation (using the = sign) and calculations. A calculation uses plus, minus, multiplication, and division. These
expressions are:
v1 = 10 + 12 (v1 is 22)
v2 = 10 – 4 (v2 is 6)
v3 = 2 * 10 (v3 is 20)
v4 = 10 / 5 (v4 is 5)
Variables that contain values can be used on the right side of the = sign.
v1 = 10
v2 = v1 + 5 (v2 is 15)
Another data type are Boolean variables. These variables can only contain two values, true or false. The values of true
and false are written as the words true or false without quotes.
movingflag = true
Text variables can contain text values. These values are assigned by putting double quotes around the variables.
myname = “Fred”
When writing scripts there are some basic commands that are often used. These include:
If command
The if command is an operation that checks an expression as being either true or false. If the expression is true then a
set of commands is executed. If the expression is false then the commands are skipped (not executed). The boundary
characters enclose the commands and these are { }.
if( expression is true )
{
statements to execute if expression is true
}
In this case a set of statements will be executed when an expression is true. If not, then the statements are skipped.
A variation on this command is the if/else statement. This allows for two different sets of commands to be executed
based on the if condition being true or not true (false).
if ( expression is true )
{
statements to execute if expression is true
}
else
{
statements to execute if expression is false
}
for example:
if( expression is true )
{
speed = 5
}
else
{
speed = 2
}
So far, the if command evaluates an expression as either true or false. What is this expression and how is it evaluated?
The IF command uses a set of comparison operators to return a true or false value. These are:
A == B (A is equal to B)
A > B (A is greater than B)
A < B (A is less than B)
A >= B (A is greater than or equal to B)
A <= B (A is less than or equal to B)
A != B (A is not equal to B)
For example:
if ( x > room_width )
{
speed = 0
}
else
{
speed = 5
}
Where room_width is the x value of the room. This statement checks the x position of an object and, if it is outside the
room to the right then it sets the object’s speed to zero, otherwise it sets the speed to 5.
Since room_width goes from 0 to the size of the room, we should also check to see if the value of x is 0 or less. This can
be done in a second if statement, but an easier way is to use the AND and OR commands in the if command. These are
used to make two Boolean comparisons in the same IF command. The AND operator is && and the OR operator is ||.
if( x > room_width || x < 0 )
{
speed = 0
}
else
{
speed = 5
}
GameMaker 8.1 Scripting
GameMaker Studio 2.0 Scripting
GameMaker 8.1 Scripting
1. Download and unzip the BraidGraphics file.
2. Start a new GameMaker document.
3. Add the following sprites located in the indicated folders in the braidgraphics folder
animation-set/greeter-walk-50-w.gif – name this sprite dinoleft
animation-set/monstar-walk-w.gif – name this sprite monsterright
icons_128/door1.png – name this sprite door
icons_128/flower.png – name this sprite flower
4. For each sprite, make sure you click the Center button to set the sprite origin.
5. Create a background by loading the background image named bg_4_02.jpg – name this level1back
6. Create a room.
7. Click the settings tab and name the room level1 and set the size as 1280 by 720.
8. Click the background tab and add the level1back background.
9. Create objects for each sprite and name the objects dinoobj, monsterobj, door, and flower.
You will next add a path and behaviors for some of the objects.
10. Right click the Path folder and choose Create path.
11. In the path window click the show room button and choose the room show the room.
12. Set the Snap X and Snap Y values as 32 if they are not set to this value.
13. Uncheck the closed check box and click Click points on the screen to set positions. Lines will automatically be
drawn between position.
14. Change the path name from Path0 to MonsterPath and click the green check mark to save the path. The
monster will now travel from right to left and, at the end of the path, reverse. However, the image will not
reverse.
15. Drag your objects into the room. Place the monster at the top of the screen and the dino and door along the
bottom.
You will next add a Create Event and a Step Event and use scripts behind these events .
16. Open the Monster object.
17. Add a Create Event.
18. Click the Control tab and drag the Script icon into the action area.
19. You see the script window. Enter the following in the Script Window:
The first line is an if statement that calls a GameMaker function path_exists(). This function, when given a path
name, returns either true or false if the path exists. Before assigning a path you always want to check to make
sure the path exists else your program will error out. We could have written this statement as:
if ( path_exists(Monsterpath) == true )
but since the if statement is evaluating a true or false value adding the == true statement is redundant.
Inside the if block of code is the statement:
path_start(Monsterpath, 4, 3, false)
This is another GameMaker function that assigns the object within which the function is called to a path. There
are four parameters to this function, separated by a comma “,” character. The first parameter is the name of the
path (without quotes). The second is the speed of the object as it follows the path. The third is what happens
when the object reaches the end of the path, and the fourth is whether the path is absolute to the starting point
of the object. That is, the x/y values of the path will map directly to the x/y values in the room.
20. Click the checkbox to save the script. You will next add scripts to the Dino object.
21. Open the dino object.
22. Add a Create Event and add a script to the create event. In this case we will add a variable to the object through
the create event
23. In the script editor write the following:
This defines a variable named “facingleft” and sets the variable to true. This will be used to flip the image when
a change in direction happens. We also set the built-in variable direction to 180 so the dino direction is to the
left.
24. Click the green check box to save the script.
25. Add a Step event to the dino object and add a script to this event.
Since the step event happens once per game loop we want to check the keyboard and move the object. We will
be relying on two built-in object variables. These are direction and speed. The direction variable is set to a
direction by setting a degree value. To the right side of the screen is degree 0 and to the left is degree 180. Up is
90 and down is 270. The second built-in variable is speed. This sets the speed of the object in pixels per second.
26. In the script enter the following:
There are three if statements in this script. The first two check the left and right arrow keys. Notice that each calls a
GameMaker function in the if() command. Each of these functions evaluates whether a key particular is being pressed.
The key checked is passed to the function as a parameter. Notice the names “vk_left” and “vk_right”. These refer to the
arrow keys. To map a letter key you would replace the vk_left with ord(‘A’) which will map the keyboard letter A (upper
or lower case).
In each if() statement the object direction and speed is set.
The third if() statement contains a compound statement. It checks to see if either the vk_right or the vk_left keys have
been released. If either has been released then the speed is set to 0.
27. Run your game. You should be able to control your dino character with left/right. The image is not flipping but
this can be done by adding some scripting commands to the step event.
28. Add the following code to the end of your script in the step event.
The last two if() commands perform a compound expression check. In this case it looks at two conditions and since the
connector is AND (&&) both expressions have to be true in order for the if() command to be true.
In this case, the facingleft flag is check (which was set in the create event). If this flag is set to true and if the direction
has been changed to zero (facing right) then two things have to be done. First, the flag has to be set to false since the
direction of the dino is to the right.
facingleft = false
Next, an object variable named image_xscale is assigned a value. In this case, the value of -1 will cause the default sprite
image of this object to be reversed on the X scale (flipped horizontally). Both of these steps will happen if the flag
indicates that the dino is facing left and the direction has been set to right.
The second new if() command does exactly the same but checks to see if the direction is 180 (facing left). It then resets
the facingleft flag to true and sets the image_xscale back to it’s de fault value of 1.
29. Save the script and run your program. You should now have the image flip based on which direction it is moving.
The last thing we want to do is reverse the image for the Monster sprite when it reaches the end of its path.
30. Open the monster object.
31. Add a Step Event and create a script for the step event.
When an object is set on a path GameMaker keeps track of where it is on the path as a percent complete. At the
start of the path the position is 0 and at the end of the path it is 1. A path position of .5 means that the object is
halfway through the path. This information is stored in a variable named “path_position”.
32. In the script for the Monster Step Event enter the following code.
The monster sprite is initially facing right. As the monster moves through the path the path position changes.
When the path position has reached a value greater than .99 we reverse the image with image_xscale = -1.
Likewise, when the monster has returned to the starting point on the path (less than .01) we set the image scale
back to 1.
We don’t use 0 or 1 to check path_position because the object never reaches these values.
33. Save your script and run your program. The monster now reverses.
GameMaker Studio 2.0
1. Start GameMaker Studio 2.0 and create a new project.
2. On the Create New Project page choose GameMaker Language and name the project pathscripts
3. Add the following sprites located in the indicated folders in the braidgraphics folder
animation-set/greeter-walk-50-w.gif – name this sprite dinoleft
animation-set/monstar-walk-w.gif – name this sprite monsterright
icons_128/door1.png – name this sprite door
icons_128/flower.png – name this sprite flower
bg_4_02.jpg – name this sprite background
4. Open the room object and set the size as 1280 by 720.
5. Click the Background link and add the background sprite Background.
6. Create objects for each sprite and name the objects dinoobj, monsterobj, doorobj, and flowerobj.
7. Create a new Path object.
8. Name this path Monsterpath and click to set the following points:
9. Open the Monster object and click Add Event.
10. Choose the Create Event. You see a Script window open to the right. The first step is to have the script attach
the path. Enter:
This starts the object (the monster) on the path. The first parameter is the path name, the second is the speed,
the third is what happens when the monster reaches the end of the path, and the fourth is setting the path to
non-absolute.
11. Close the script window to save the script.
12. Add a Step event to the Monster.
13. In the script window enter the following:
This will check the position of monster on the path and, if the position is at the start of the path (path_position <
.01) the image will be set to the original direction (facing right). If the path_position is at the end ( > .99) the
image xscale is flipped.
You will now set the dino movement controls.
14. Open the Dino object.
15. Add a Create Event.
16. In the script window enter:
This will be used to check which way the dino is facing in order to flip.
17. Close the script window and add a Step event to the dino.
18. Add the script to the Step Event
This script will move the dino left and right and mirror the sprite as it changes direction.
19. Open the room and click the Instances layer.
20. Add the Monster object at the top of the room and the door and dino objects at the bottom.
21. Run your game.