Stupid pathfinder

hi. long time no see.

I have a little problem with the pathfinder.or may be he has problem with me… I donn’t know. no matter.

this is my scene now:

an empty field with two objects. the yellow rectangle is a car (belive me, that’s a car). and the NPC worker.
the NPC is the pathfinding object, he needs to move to his job, the car. but because the rectangle is a car, it needs to be an obstacle to the NPC. so, it is an obstacle and a destination in the same time! but of course, the NPC doesn’t need to go IN the car, just somewhere around it (preferably below, because the NPC has a higher Z order), so it is not a big problem as I think.
but GDevelop sad: no dude, I won’t do that :stuck_out_tongue:

now the main problem is the NPC don’t start moveing when it gets his order. until I move the car a little bit up (it is a draggable object).
I tried few things:

  • the NPC got the coordinates from variables. I give + pixels on Y. (it’s start working above 100~120, but thats too much. the rectangle itself is 100*50). the NPC goes too far from the car.
  • I set a collision mask for the NPC myself. now it is only 5 pixel tall on Y. and set all his points to his ankle.
  • I tried to play the obstacle’s parameters. I set off impassable, but it needs to be inpassable, becouse it is a big phisical object. I don’t want the NPC goes trough on it! it is weird, when I set the cost low enough (still higher than 0), the NPC goes trough the rectangle in a total EMPTY field, where he can go around wit no cost.
  • and of cource I checked the pathfinder NPC’s settings too. it hasn’t extra border. I tried more virtual cell size, but it’s not help.

Yeah, this is an interesting problem. What I usually do in such situations is disable the obstacle behavior when I want the pathfinding object to move to and maybe even interact, collide with an obstacle and then enable the obstacle behavior when the pathfinder is moving somewhere else and i want it to avoid the obstacle.

One problem you may going to experience however is that if the pathfinder going to collide with the obstacle when the behavior is disabled, it may get stuck in the obstacle when you enable the behavior again. In order to avoid that, you want to make sure the pathfinder is not colliding with the obstacle when you enable the behavior and if it does, you want to move the pathfindiner away from the obstacle before enable the behavior.

So, it is a bit tricky but doable.

Basically this is what you want to do:

Move NPC to destination
If destination is car
disable obstacle for car

If destination is NOT car AND NPC is not colliding with car
enable obstacle for car

if NPC is colliding with car
move NPC away from car

Something like this.
Let me know if my explanation was not clear :+1:

your explanation is clear, but this solution isn’t what I looking for. with tweaking the settings I have an almost usable version. it is enough for now. the new problem is:
now the “reached the destination” condition isn’t work.

I’m not sure how it needs to be work, because it isn’t on the documentation (or it is hidden somewhere) … but it isn’t give “true” when the moving object reaches his destination and stop. why?!?

That condition return true for a single frame only which is a fraction of a second. Your game runs at 30-60 frames / second so it is updating that many times / second and the condition return true for 1 frame only.
After 1 frame, it is no longer relevant really until you move again to a destination.

So whatever is that you want to do, you want to do it in that 1 frame.
Possibly you want to set a variable and then do something based on the value of that variable.

If NPC reached its destination then DO = 1 to variable XYZ
If variable XYZ = 1 then do whatever you want, when you done set XYZ back to 0.
So you get a bit more time (more than 1 frame) to do whatever is that you want to do.


If you have multiple NPC’s moving in to different destinations, it could be also that GD doesn’t pick the right NPC to test the condition at the right time and that’s why it doesn’t return true because we are talking about a single frame only, if you have many NPC in the scene GD might just missed the right instance at the right frame.
Then what you can try is the famous for each event to force it to check this condition for every single instance each frame.

of course I do that. do xx variable if reach destination, but it doesnt do! I checked in the debugger, the NPC is on the desired coordinates, and it is stop moving. but the condition "reached destination’ doesn’t get out the true signal. so that variable isn’t modified. this is the problem.

Do you set the value of that variable back to default at any point?
If you do, disable that event so you can be 100% sure it was never triggered.
Did you try the for each event and object variables maybe?
If that does not help, I have no idea why it doesn’t work for you, It does work for me. Must be something specific about your use case.

Make a screenshot of your events and share it, maybe someone can figure out what it could be.
Could also help if you try to reproduce the problem in a new project and then share the project so people can try.

1 Like

it is simple as I write down, but I can do screenshots.

your intuition was wright, I want multiple NPSs and multiple instances fo multiple car types. but now I have only 1 rectangle and 1 pathfinding object.

I think the problem is that you constantly trigger the action to move the NPC to destination and so it is constantly trying to move and never finish and that’s why the condition to check if destination reached does not work in your case.

So, what you want to do is add a Trigger Once condition to the first event so you order the NPC to move only once. Make sure it is the last condition in the event.

Variable OV_move of NPC_worker = 1
Trigger Once

And then it should stop and the condition if destination reached should fire.

1 Like

oh yes! that was it! thank you!
sadly in the examples aren’t use the “reaches destination” condition, thats why I never saw the “trigger once” condition in any example and the documentation isn’t mention… a real tutorial is needed! or a detailed documentation.

Thank You again!

I was so delighted when you solve the problem, I don’t construe it.
but now I don’t understand why I need to use the trigger once condition.

You told, I constantly trigger the action, but it isn’t quite true. I order it: constantly moving UNTIL reaches destination, but after it arrives turn itself off. and I never order start moving again. even it is just an 1/60 -th of a second, it needs to turn the value of the variable “OV_move” to “0”, and nobody told never to nobody, write it back to “1”. and when this variable is “0”, it is forbidden to move I think.

so thats why I can’t understand, why it wasn’t enough, and why the “trigger only once” condition needed? because nobody triggered it wtice.

In your case, the way you did it, the condition if NPC reached the destination was never true, so you never turned it off, and this is why:

The action to move the NPC to destination was fired every single frame which means 30-60 times in one second. So even if the NPC did reach the destination in one frame, in the next one the action to move to destination was fired again and so the condition to check if NPC reached the destination was never true because the NPC was constantly moving.

Now, of course technically the NPC did reach the destination and stopped and when the action fired again to move to destination, technically the NPC did not have to move because it was already at the destination, but in this case it is not important. What is important is that, the action to move did fire and so the NPC was in “moving” state even if it didn’t really had to move and so when the condition checked if the NPC reached the destination it is returned false because the state of the NPC was “moving” and because the action fired every single frame, the npc was always “moving” and was never “reached the destination” even though it did.

So what the Trigger Once condition does, it is make sure the action to move NPC to destination fire only a single time, and when the NPC reach the destination it does have a chance to change it state to “reached destination” and the condition can return true at the end.

Don’t know if it makes sense to you, but in some cases like this when you don’t get the result you want, you need to look deeper under the hood and think about how the game is being executed frame by frame, millisecond to millisecond which is require to understand how certain conditions and actions really work.

Hope I was able to answer your question.

1 Like