# I am making a TD without paths.

#### WimpyLlama

##### Member
I am trying to make a Tower Defence game without using paths because I want the map to be randomly generated. I have not started work on the random generation yet, but I am trying to get enemy's to walk across the map. In order to do this, I created 4 path objects, each one made to move the enemy object in different directions. The problem is that it is set up so that if the enemy touches the path it moves in the corresponding direction, but when it goes from up to right or down to left, vice versa, the enemy travels on the edge. I know why this is happening, I just don't know how to fix it. Please help. Here is my code.
Code:
``````Step event for enemy.

//Set Movement
x = x + hsp;
y = y + vsp;

//Move On Path
if (place_meeting(x,y,obj_path_right)) {

hsp = walksp;
vsp = 0;

} else if (place_meeting(x,y,obj_path_left)) {

hsp = -walksp;
vsp = 0;

} else if (place_meeting(x,y,obj_path_up)) {

vsp = -walksp;
hsp = 0;

} else if (place_meeting(x,y,obj_path_down)) {

vsp = walksp;
hsp = 0;

}``````

#### Gamebot

##### Member
An array or ds_grid?

You can spawn the path first. Then set direction by number. Something like:

0 = keep going/notta here.
1 = up;
2 = down;
3 = right;
4 = left;

5 = tower 1;
6 = tower 2;
.
.
.

You can also use the above code for enemies too.

#### WimpyLlama

##### Member
I did not think of that. That will help with compacting it down, but it still won't solve my main problem.

#### Gamebot

##### Member
When you create a grid or array you can slice your room up the same and simply draw your enemies to the middle of the path. Should work. You might have to do a change variable to point_direction them the correct way...

#### WimpyLlama

##### Member
How exactly would I do that? I am new to the coding part of game maker. I have been doing drag and drop for a while so I wanted to learn some gml. I don't know anything about arrays. Please don't think I am some noob who knows nothing. I know something's, just not everything.

##### Member
If I understand correctly, the issue is that your "enemies" are touching the "path" objects and then changing direction without being in the center of the path, correct?

If so, you could do an array/grid system like @Gamebot mentioned. Or, if you want to stick to your current setup of using objects, here is a simple solution:

Check to see if you are within a specified distance of the Path object's ( x ) or ( y ) before changing direction.
To do that, we can use instance_place instead of place_meeting. They are nearly the same, but instance_place returns the id of the object found, or noone if there are no collisions.
It might look something like this:
Code:
``````i = instance_place( x , y , obj_path_right );
if( i != noone )
{
if( abs( y - i.y ) < speed )
{
// Change speed/direction here
}
}
i = instance_place( x , y , obj_path_up );
if( i != noone )
{
if( abs( x - i.x ) < speed )
{
// Change speed/direction here
}
}
i = instance_place( x , y , obj_path_left );
if( i != noone )
{
if( abs( y - i.y ) < speed )
{
// Change speed/direction here
}
}
i = instance_place( x , y , obj_path_down );
if( i != noone )
{
if( abs( x - i.x ) < speed )
{
// Change speed/direction here
}
}``````
This certainly isn't a very efficient solution, but it should work with your current setup.
The code " if( abs( x - i.x ) < speed ) " basically checks to see if the enemy would pass over the center ( x ) of the path on the next frame.

You might still notice a visual offset of a few pixels, depending on the speed of your enemies. To remedy this, you should snap the enemy's ( x ) or ( y ) to the path object's.

For example, if changing the enemy's direction to [ RIGHT ] or [ LEFT ], we want his ( y ) position to be equal to the ( y ) position of the path object. You could do that when you change the direction.

The basic concept is that we only want to change the enemy's direction once it is in the center of the path.

Forum Staff