BlueMoonProductions
NL EN


Paths
Paths - making A* and Bézier Curves(and a bunch of other things) a whole lot easier since 5.3.
Required software: GameMaker 5.3 or higher
Difficulty: Average

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

1. 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.

2. 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 Symbol

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

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!

3. 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:

4. 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.

5. 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.

Two 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, that is) is saved in path_positionprevious.

path_scale represents the scale of your path, it's relative size. The entered value 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 position as it's origin). In degrees, counter-clockwise.

6. Path-related event


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



That's it! I hope you found this tutorial helpful.

This work is licensed.
License


NL EN