Paths

Were you looking for Reverting GM:Studio back to the old GM8 layout?

This tutorial is an introduction to paths for GameMaker. Requires version 5.3 or higher, which I’m guessing doesn’t shrink my demographics at all.

Table of contents

  1. What is a path, and what can I use it for?
  2. Path editor
  3. Assigning a path to an instance
  4. Ending a path
  5. Path related variables
  6. path related event

What is a path, and what can I use it for?

Literally, a path is a bunch of two-dimensional points, with lines inbetween them. You can make an object move over these lines.

Paths can be used for a lot of things. You can use them for NPC race cars, pathfinding, etc. You can make the as a resource in the IDE, but also during the game itself, on the fly.

Path editor

I think it’s a good idea to start with the editor, since this will help me to explain all the basics of a path. Click the following symbol in GameMaker to open the path editor:

path icon

Path

The next image shows the path editor, followed by a list explaining the numbered items.

Path editor

Path editor

  1. This is the resource name of your path. When programming, you can use this name to refer to the path. So obviously the entered name has to be unique. Also, it is advised to use the pth_ tag, fore example: pth_mypath. This will help you to avoid name collisions, and makes it easier to recognise the resource.
  2. This is the part of the interface you’ll be using most of the time. This is where you’re path is being displayed. You can click in this area to add new points to your path. When you do this, you will notice that some nodes have different colors. There are three colors: Blue, red and green. The Blue circles are regular nodes. These don’t mean anything special. The Red circle is the node that is currently selected. This is usually the last-made node, but you can select a different one by choosing one from the list on the left(I will talk about that one later). The Green square is the start of the path. If an object starts moving along a path, it will start here. The dots are connected with yellow lines, this is the path itself. If you add a new node, it will connect to the node made before that one, and the first node.
  3. This is a list containing all the nodes from your path. You can use this to select nodes. Behind the (x, y) position of every node, it also shows the procentual speed. I will talk about that later.
  4. Instead of clicking in the main interface, you can add a point as well using this form, for example if you want it at a very specific position. You can also use it to edit nodes. Something new is “sp”, this means “speed”. Here you enter the relative speed of that node, procentual. If the speed of an object that travels over the path is 8, and the procentual speed of that node is 50, the speed of the object will be 4 when it passes that node. The speed of the object changes gradually. Inbetween a node of sp:100 and a node of sp:50, the procentual speed will be 75.
  5. Here you can choose between two different configurations: Straight lines or Smooth curves. Straight lines is the default option: this will draw straight lines between the nodes. Smooth curves .. well, it smooths the curves. It makes the curves .. smooth. Oh, whatever. Try it yourself.
  6. The option “closed” determines wether the path loops or not, that is, if there should be a line between the first node and the last-placed node.
  7. The precision is only important if you use smooth curves. This determines how smooth smooth curves actually are. A number from 1-8.
  8. This bar contains several options for modifying the path as a whole, as well as options to modify the grid, and last but not least: an option to display a room in the background!

Assigning a path to an instance

The next step is getting our instance to follow the path. To do this we use the function path_start(path, speed, endaction, absolute). The arguments are:

  • path: This is the resource name of your path, that you entered in the box described in the previous chapter.
  • speed: The speed of the object while traveling over the path. Measured in pixels per step.
  • endaction: “End Action”, what is supposed to happen at the end of the path. The options are:
    0 Stop the path, stop moving.
    1 Start the path again. If the path isn’t closed, the instance will be moved to the start position of the path.
    2 Start the path again, but do not move to the start position. If the path is closed, this is the same as the above, but if it’s open, it will start from where it is at the end of the path.
    3 Move back over the path. Basically multiplies the speed with -1.
  • absolute: Indicates wether the path should be started at the exact coordinates you used in the path editor, or at the object’s coordinates.
    So if you enter true, the instance will be moved to the paths coordinates, but if you enter false, the path will be moved to the instance’s coordinates.

Ending a path

Ending a path is easy, use the function path_end(). This function doesn’t have any arguments. You don’t have to specify the path, since an instance can only follow one path at a time.

Path related variables

There are a few variables related to paths. These are useful if you want to use or change specific data of the path.

Our first variable is path_index. It contains the resource index of the current path. This variable is read-only, which means you can request its value, but not change it. If you want to change the path, use the earlier mentioned path_start.

wo other variables you can change are the two included in path_start: path_speed and path_endaction.

path_position is the position of the instance on the path. This value ranges from 0 to 1, begin to end. The previous position (the position the instance was in during the previous step) is saved in path_positionprevious.

path_scale represents the scale of your path, relatively. The entered number should be a factor, so the standard value is 1.

Last but not least there’s path_orientation, which you can use to turn the path (using the starting point as the origin). In degrees, counter-clockwise.

Path related event

GameMaker also has a path related event. You can find it under the green diamond: Otherend of path. This event is executed once when the instance reaches the end of the path.

Leave a Reply

Name and website fields are completely optional.