Basic 2D Pathfinder Tutorial

Learn how to make certain types of games and use gameEditor.

Basic 2D Pathfinder Tutorial

Postby bamby1983 » Wed Feb 13, 2013 1:24 am

EDIT: After revisiting the attached .ged file, it appears that Game Editor automatically increases the Z-Index every time it clones an object, so cloning an object so many times may not be a good approach. Instead, we can create a separate image for every section of the wall, thereby reducing the number of clones for which you need to manually reset the Z-Index. I used this method originally in the game I'm making and although tedious, it works.


Here's a very basic 2D pathfinder tutorial for top-down games. The objective of this tutorial is to learn how to make an actor find its way through non-linear paths without walking through walls or other objects.

Example: A person walking through a maze or someone walking between rooms in a house along a non-linear path.

Command to be used:
MoveTo(); // Used to move the object to a particular destination

Additional Concepts Involved:
Clones: Cloned objects share the same properties.


In this tutorial, we will move a soldier through a house, making him move through doors and empty spaces while avoiding furniture and walls. There is a zip file attached along with this message that contains the completed tutorial (including the images and the .ged file).

Step 1:
Create a new GE game and add a new actor. In this example, we'll name the actor "Soldier" but you can name it whatever you want to. Assign an animation to this actor. Leave the Z-Index at the default value of 0.5.This is the actor that we will be moving.

Step 2:
Add another actor for the image of the house and assign the appropriate animation to it. You do not need to add the furniture separately unless you're going to interact with it (which is beyond the scope of this tutorial). Set the Z-Index of this actor to 0. The following image shows what you should have so far if you have used the images in the attached zip file.

Image

____________________________________________________________________________________________________________________________
Now for a little bit of theory.

The "MoveTo" function is a standard Game Editor function that moves an actor along a straight line to the defined destination. The following is its syntax:

MoveTo(x, y, speed, avoid)

where:
x = the x co-ordinates of the destination point to which the unit should be moved
y = the y co-ordinates of the destination point to which the unit should be moved
speed = the speed at which the object moves across the screen
avoid = the name of the actor that the object should avoid

It should be noted that we can only specify the name of any ONE actor in the "avoid" field. Game Editor automatically draws an imaginary rectangle (also known as a bounding box) around the extremities of this object and the actor to be moved avoids this bounding box, not the visible portion of the actor itself. So if we were to add an O-shaped animation, GE would avoid the entire rectangle surrounding this O and not just the O-shape itself.

This means that we cannot simply use a single image consisting of multiple walls if we want the actor to walk between them and avoid them at the same time. This is because the entire rectangle containing these walls (including the space between them) would be avoided by default. Fortunately, there is a simple solution we can use to work around this! :)

If we clone an object, GE will treat all clones as the same actor while avoiding them. Therefore, we can use rectangular images for each of these clones and arrange them to that they coincide with the walls in the room. This will cause GE to avoid each of these clones while allowing the actor to move between them. This is actually a workaround for avoiding multiple objects.
___________________________________________________________________________________________________________________________

Let's get back to the hands-on tutorial now.

Step 3:
Create an actor that we will use to make the soldier avoid walls and furniture. Let's name this actor "Avoid_This" so we remember what it was created for. Set the Z-Index of this actor to about 0.01 (note the number of decimal places). Do NOT set it to 0. This is very important. Assign a picture of a square that has the exact same width as the walls as an animation for this actor. In this example, we have used a black square (since it has the smallest file size) with a length and width or 11 pixels (the thickness of most of the walls).

Step 4:
Position this image on the top left corner of the walls so that it coincides with the walls perfectly and LOCK the actor. It is important that you lock the actor before you cloning it, otherwise there will be too many of them to lock manually later. The image below shows the top-left portion of the house with the "Avoid_This" actor in the top-left corner over the wall.

The walls in the image are represented by the light yellow region surrounding most of the house and separating the rooms and passages.

Image

Step 5:
Now that we have the "Avoid_This" actor in position, we need to clone it so that it covers the entire area corresponding to the walls. Right-click on this actor and select "Actor Control," then select "Clone" and then click "Array." You will receive a pop-up window asking you for certain values. Enter the following values in this window and leave the rest at their default value.
Horizontal Count: 0
Vertical Count: 32
Horizontal Distance: default
Vertical Distance: 11

You can calculate the vertical count using the following equation:
Vertical Count = Total length of wall / Length of the "Avoid_This" actor animation
I have rounded down the result as the actor will be unable to reach the bottom left corner without colliding with other clones in any case. Fewer actors mean less memory required for the game to run. You can also just enter any number in the above window without clicking the "OK" button and see the estimated positions of the clones in real-time if you don't want to manually calculate this.

If you had already locked the initial actor as suggested earlier, you will realize that all the resulting clones are also automatically locked. This means that you do not have to worry about them moving when you drag your screen and you also don't need to lock each one individually.

The following image displays the results so far.

Image


Step 6:
Now repeat this process until all walls are covered with these cloned actors. You can select the original actor (which will now be named "Avoid_This.0" - where 0 indicates the clone number) and clone these objects horizontally using the same method outlined above. In case you need to leave space for the doorway, you can clone actors up to the door, then create a single, extra clone and re-position it and start cloning from there again. In case your clones don't exactly match with the desired length of wall, you can always make the last clone in the line overlap with the one before it.

Always remember, though, that cone arrays only replicate objects from left to right and from top to bottom. Even if you add a negative value for the distance between cones, GE won't consider it.

You can also add more images as animations to any of the clones in case you require a different thickness. This newly added animation will automatically be available to all the other clones as well. I haven't does this in this example in order to keep things simple, but you're welcome to experiment with different sizes. Just always remember that the animations need to be perfect rectangles as the bounding box is always rectangular.

The following image shows us what the screen looks like after all the clones (seen as black areas over the walls and balcony boundaries) have been added to this scene.

Image


Step 7:
Now we're ready to implement the final (and easiest) step of the process. We're going to make our soldier move from the bottom left balcony to the top right balcony while avoiding all the walls in the house. We'll use the MoveTo function to do this.

This function will need to be invoked through an event, so we'll use the Mouse Button Down event to call it. This means that our soldier will move when we left-click on an actor. In our case, we'll left click on the house actor to make the soldier move.

Right-click the "House" actor and select "Actor Control." Under the "Events" section (it's the last section at the bottom), click "Add," then select "Mouse Button Down." Left-click once so that this event registers as a left-mouse click. Then click "Add Action" and select "Script Editor." We could also directly select the MoveTo function, although in a typical game, you would most likely have accompanying code as well, which is why I recommend getting comfortable with script editor.

Enter the following within the screen that pops up:

MoveTo("Soldier", 170, -160, 3, "Game Center", "Avoid_This");

In the above code;
"Soldier" is the name of the actor we are going to move. If you named your movable actor different, you would need to enter this name instead.
170 corresponds to the X co-ordinates of the destination to which we will be moving the soldier, while -160 corresponds to the Y co-ordinates of the same. If your game involves a different set of co-ordinates, you should enter those values in place of the ones mentioned above.
"Avoid_This" is the name of the actor that the movable actor (in this case, the soldier) will avoid. This includes ALL the clones we created. If you named the actor to be avoided different in your game, you would need to use it instead of the name used here.

Click "Ok," then click "Immediate Action" and close all pop-up windows.


Step 8:
There's just one more step remaining before we can run the game. Right-click the "House" actor, select "Actor Control" and increase the Z-Index to 0.1 (note the decimal places - the "Avoid_This" actor had a Z-Index of around 0.01 - or one-tenth of the value of the "House" actor's value). This will cause the clones to be hidden below the house actor and the player won't see them on the screen.


Step 9:
Save the game and click "Game Mode" to run it. Once the game is running, left-click the house actor (but not on the soldier) to move the soldier to the designated co-ordinates. If you did everything as explained above, the soldier should walk through the house using doorways and avoiding the walls. He may spin around in circles if you used the animation in my attached file, but that's because of the animation I used was originally intended for a different purpose. Sorry if it makes you giddy - you can use the Change Animation Direction function to set the animation direction to STOPPED so that he doesn't spin so much.


Step 10:
We can clone the "Avoid_This" actor again and place it at the same position as the furniture in the house to make the soldier avoid it as well. Just make sure that there is enough room for the actor to move through or he won't be able to get out of that area. I haven't added clones for the furniture in the room for this tutorial as it follows the same principles as the walls, but you're free to give it a try if you want to.


This concludes the basic 2D pathfinder tutorial. You can use the same concepts to enable actors to find their way through 2D maps and mazes. The following zip file contains the animations and the .ged file for this tutorial.

Pathfinder Basics.zip
(465 KiB) Downloaded 276 times



EDIT (repeated from top): After revisiting the attached .ged file, it appears that Game Editor automatically increases the Z-Index every time it clones an object, so cloning an object so many times may not be a good approach. Instead, we can create a separate image for every section of the wall, thereby reducing the number of clones for which you need to manually reset the Z-Index. I used this method originally in the game I'm making and although tedious, it works.
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score

Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest