• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

1. end current path, 2. move_towards_point(another path_position(0) ) and 3. once there start another path

MartinK12

Member
I’m building solution for crossroads in my tower defense game. Enemies move on path, once they hit object obj_choose_path they should end this path, choose another path, move to its start and once there start another path.
1. Can’t figure out how to check if they are already on another path_position(0), I also don't want to change enemies speed.
2. Can I use path_speed of the path I just ended as enemy speed to move_towards_point?

GML:
if place_meeting(x, y, obj_choose_path) and (path_choosen == false) {
    path_choosen = true;
    new_path_to_go = choose(path_mm_2); //for testing one path only
    new_path_x = path_get_x(new_path_to_go, 0);
    new_path_y = path_get_y(new_path_to_go, 0);
    move_to_new_path = true;
    path_end();
}

if (move_to_new_path) {
    if point_distance(x, y, new_path_x, new_path_y) >= path_speed { //can I use path_speed here?
        move_towards_point(new_path_x, new_path_y, path_speed);
    } else { //we are on new path so just start it? but enemies will jump to path_position(0) depending on position of path? - not sure about this part?
        path_start(new_path_to_go, path_speed, path_action_stop, true);
        move_to_new_path = false;
    }
}
EDIT: I figured out better code but still not sure about it
 
Last edited:

huenix

Member
what did you end up doing? ... is the code above what you used?
i am having an issue trying to stop/change the end goal while the AI is moving towards the original goal
 

Neptune

Member
Path finding, and mid-pathway changes are complex. The general answer is you need to have the utmost clean "flow control" to do it well, and even more so depending on how complex your AI is.

I'm not sure how to do this with built-in functions. Call me crazy, but I wouldnt recommend built-in functions for most anything that is complex.
 

MartinK12

Member
@huenix I'm using this code and looks like enemies r changing paths correctly.

But to avoid strange situations I put obj crossroad on straight line (vertical or horizontal) before actual crossroad
and put 2 paths on each other for some length before crossroad.
So once enemies reach crossroad, they are already on separate tracks
 

vdweller

Member
Use a state machine, that is a switch statement. Typing from phone but the general idea is to create an instance variable called eg phase and then in the step code do:

switch phase {
case 0:
start a path
++phase;
break;

case 1:
if path has ended {
++phase;
}
break;

case 2:
move towards point
if point reached {
start another path
++phase;
}

etc etc
}

90% of my games nowadays are structured this way. You can do cutscenes, puzzle games, enemy ai... it's simple to write and compartmentalizing your code makes it easy to go back and change stuff instead of doing a huge if spaghetti everywhere and not being sure what is being executed when. Learn to get used to it and you'll jump miles ahead in programming p. much anything that has to be executed sequentially.
 

huenix

Member
oh... i should close this.... but idk how ..... i figured out something .... and it might help others too.... i made it easy ...

i mixed my melee/ranged collision boxes (from the ai) and the "path_speed).... once it is in an "Attack State" it changes its path speed to "0" and after AI/Player is dead it goes back to normal path speed
 
Top