making enemies walk towards player randomly on GRID

T

Tom Barylov

Guest
Hey Folks!

I have enemies that walk randomly towards my player.

Works just fine. Now I would like them to stay on a 64x64 Grid (18x11).
how do I do that?

thanks!

(alarm code making them move around, randomly towards player):
Code:
``````newdir=point_direction(x,y,obj_player1.x, obj_player1.y) + irandom_range(-64,64)
speed=4
direction=newdir
alarm[0]= irandom_range(60,150)``````

TheouAegis

Member
One way is to take your direction and round it off to 90.

direction = newdir div 90 * 90;

Then set your alarm to however long it would take to walk precisely one grid space. But obviously that's not a good idea.

You could use the mp_grid functions to let GM decide where to go. That would also let your AI avoid any obstacles in the way. That would probably be the easiest way.

Alternatively, get the coordinates of where the AI is headed and then every step check if the AI is aligned to the grid, then if it is, check attempt to move along the shortest vector (horizontal or vertical) toward the destination. If an obstacle is encountered, move to the other vector. The issue with this method is your AI could get trapped in corners or dead-ends, which is why the built-in functions would be better.

T

Tom Barylov

Guest
One way is to take your direction and round it off to 90.

direction = newdir div 90 * 90;

Then set your alarm to however long it would take to walk precisely one grid space. But obviously that's not a good idea.

You could use the mp_grid functions to let GM decide where to go. That would also let your AI avoid any obstacles in the way. That would probably be the easiest way.

Alternatively, get the coordinates of where the AI is headed and then every step check if the AI is aligned to the grid, then if it is, check attempt to move along the shortest vector (horizontal or vertical) toward the destination. If an obstacle is encountered, move to the other vector. The issue with this method is your AI could get trapped in corners or dead-ends, which is why the built-in functions would be better.
Hey Aegis

yes, that makes sence.
I would consider the last option, and this is why: The stage is field with random placed obsticals. the only part they cant get throught is the wall around the room. If they encounter a cube, they shall destroy it and walk through.

I got the logic, but I am having trouble coding it :/