pathfinding canghe sprite

S

space fable

Guest
hi guys, i have a object. this object must go in a specific point. to go in this point it needs avoid other object. i do it with pathfinding everything works.

I have a problem with change sprite.
The object have a different sprite for every direction.3 change sprite work good but when object go to left, give me the sprite to go to right.

Someone can help me?

That's the code. It is all in step event



Code:
grid=mp_grid_create(0,0,room_width/72,room_height/92,72,92);
path=path_add();
mp_grid_add_instances(path,wall,true);
mp_grid_path(grid,path,x,y,1024,720,true);
path_start(path,3,"",1);


if direction=90 {sprite_index=spriteUP}
if direction=0 {sprite_index=spriteRIGHT}
if direction=270 {sprite_index=spriteDOWN}
if direction=180 {sprite_index=spriteLEFT}
 

jo-thijs

Member
Hi and welcome to the GMC!

The problem here is that the object moves by a path.
However, paths don't change the variable direction,
so direction will remain 0 for the entire path.

What you can do is use this code in the end step event:
Code:
switch point_direction(xprevious, yprevious, x , y) {
case   0: sprite_index = spriteRIGHT; break;
case  90: sprite_index = spriteUP; break;
case 180: sprite_index = spriteLEFT; break;
case 270: sprite_index = spriteDOWN; break;
}
 
S

space fable

Guest
thanks, but code don't works. with this code not change anyo sprite
 

jo-thijs

Member
My code works just fine when I test it.

However, I've taken a look at the code you posted and it contains quite some issues.
Code:
STEP EVENT:
grid=mp_grid_create(0,0,room_width/72,room_height/92,72,92);
path=path_add();
mp_grid_add_instances(path,wall,true);
mp_grid_path(grid,path,x,y,1024,720,true);
path_start(path,3,"",1);
First of all, path_add creates a new path, which takes up resources.
However, you never delete this path, creating a memory leak.
Since you do this every step, you create 30 or 60 paths per second that are never deleted and barely used.

The code you've given should rather be used in the create event and you should take care that the path gets deleted when it's no longer used.

The same goes for creating grids every step, you should use mp_grid_destroy to clean those up as well.

Secondly, mp_grid_adsd_instances requires you to pass the id of the grid, whereas you pass the id of the path.
Coincidence might have caused this to be no issue in your project so far,
but it will sooner or later and on top of that, it is just plain confusing.

Thirdly, you pass the empty string "" as third argument to path_start,
whereas the third argument needs to be one of the path_action_* values.
GameMaker silently ignores you give it wrong datatypes here and will treat a string as the number 0,
which corresponds with one of those path_action_* values,
however, this is again plain confusing.

Fourthly, if I understand correctly what you're doing here, room_width and room_height would be 1024 and 720 respectively.
In that case, the numbers 72 and 92 don't make a nice grid and will leave awkward space at the borders that is not concidered by the mp_grid.
At those borders, you've put the destination, but since the destination is not in the grid, the mp_grid functions should not even return a path to the destination.
I'm actually quite curious if this code actually brought your object to its destination for you.

What you should do, is use ceil(room_width / 72), ceil(room_height / 92) instead.

And finally, you give sprites for 4 directions, but you make your object move in 8 directions.
This is something I did not make my code for, but it should still act pretty fine.

Please, concider these advices and verify whatever my code is actually not working.
Remember, my code is supposed to go in the end step event.
 
S

space fable

Guest
i'm sorry i open again this discussion because now i have another litle bit problem with pathfinding.

With this code i my object will go in position x=720 y=920

Code:
mp_grid_path(grid,path,x,y,720,920,true);


if poistion x=720 y=920 is not empty i want object go in position x= 720 y=838.

if poistion x=720 y=838 is not empty i want object go in position x= 720 y=746.

if poistion x=720 y=746 is not empty i want object go in position x= 720 y=654.

if poistion x=720 y=654 is not empty i want object go in position x= 720 y=552.

if poistion x=720 y=552 is not empty i want object go in position x= 720 y=460.

i want if place is not empty y will be y-92

i do that:
(i did not test the code but i will be it or something similar with a lot of "if")
Code:
if place_empty(720,920)
{
mp_grid_path(grid,path,x,y,720,920,true);
}

if !place_empty(720,920)
{
mp_grid_path(grid,path,x,y,720,838,true);
}

if !place_empty(720,838)
{
mp_grid_path(grid,path,x,y,720,746,true);
}
if !place_empty(720,746)
{
mp_grid_path(grid,path,x,y,720,654,true);
}
if !place_empty(720,654)
{
mp_grid_path(grid,path,x,y,720,552,true);
}
if !place_empty(720,552)
{
mp_grid_path(grid,path,x,y,720,460,true);
}
My question is :

is there a more simple code to do it?
 

Yal

🐧 *penguin noises*
GMC Elder
Code:
starty = 920;
while(!place_empty(720,starty)){
  starty -= 92;
}
mp_grid_path(grid,path,x,y,720,starty,true);
 
S

space fable

Guest
it is so simple. i'm real stupid :confused:. Thanks for you help
 

Yal

🐧 *penguin noises*
GMC Elder
By the way, I guess I should explain a thing from my code snippet.
starty -= 92 is short for starty = starty - 92. The syntax can look a little weird before you're used to it. Apart from -=, there's also +=, *=, /= operators.

! means 'not', but in the boolean sense - AKA it reverses true/false values. I prefer using !, ||, and && over 'not', 'or' and 'and' so that I won't mix up their english meanings with their maths/programming meanings that are slightly different, and I recommend that you'd do the same.
 
R

Robert

Guest
Just thought I would throw this out there, Im not sure if its a bug with GM or what, but if you create a new path over and over, like every step, and even delete it, it will still cause lag. When you use path_add even if you remove it right away it will still cause the game to lag after a while. Same thing with sprite_add, there are certain functions that even when you release their memory it doesn't seem to prevent the game from lagging. It's strange because watching the debug window doesn't show that the game is using more and more memory, it just... lags? So with that said, it's better to only create the path once and then just change it over and over than it is to create a new path over and over.
 
S

space fable

Guest
well at moment everythings works good. But i'm just at begin of my game.i will try to run my game for a lto of time to see if i will have problem with lag
 
B

bojack29

Guest
Hi and welcome to the GMC!

The problem here is that the object moves by a path.
However, paths don't change the variable direction,
so direction will remain 0 for the entire path.

What you can do is use this code in the end step event:
Code:
switch point_direction(xprevious, yprevious, x , y) {
case   0: sprite_index = spriteRIGHT; break;
case  90: sprite_index = spriteUP; break;
case 180: sprite_index = spriteLEFT; break;
case 270: sprite_index = spriteDOWN; break;
}
Actually this is incorrect. The direction IS influenced by the path. If you dont't believe me try starting a path and setting the image angle to the direction every step. Youll notice the sprite will change his image angle as the path moves along.

The likely culprit is the fact that your direction may not be 90 or 0 degrees. It may be a precision issue like 91.222334 degrees and so on.

A trick to fix this is to simply round the operation.
Code:
//While path_index is greater than -1
image_index = round(direction / 90);
This assumes one sprite carries all 4 directions.
 

jo-thijs

Member
You're right, it does change direction.
I did some tests way back and I'm pretty sure it wasn't the case then.

Anyway, it's good I know it happens like this now.

EDIT:
Hm, I might just be remembering this wrong.
GameMaker 8.1 is doing the same.
 
Last edited:
Top