# How to handle waypoint based pathfinding in a topdown game?

## How do I…

Make guards go from waypoint to waypoint (1 - 2 - 3) with the option of looping (1 - 2 - 3 - 2 - 1) or just going straight to the first point (1 - 2 - 3 - 1)

## What is the expected result

One guard is assigned to one patrol (group of waypoints), and follows it, the guard can be distracted, which will take the guard off the patrol, but after the distraction is cleared the guard goes back to their usual patrol.

I’m sorry in advance for that loaded of a question, i’m just very inexperienced in topdown games & i couldn’t find a good answer in the forums.

You could make WpX1 WpY1 variables holding destination of waypoint 1 location
And add few more for 2 3 and how ever more you need
And make another variable called Cwp (like Current WayPoint)
Go like
This is meant if there would be 3 of them

Condition
If one of these conditions is true (add condition search for OR)
Guard X position is less than WpX1 - 3
Guard X position greater than WpX1 + 3
If one of these conditions is true (add condition search for OR)
Guard Y position is less than WpY1 - 3
Guard Y position greater than WpY1 + 3

Action
mod(Cwp,3)
Trigger once

Now mod needs to be mod not Mod
3 after Cwp mean cycle it trough 0 1 2 so 3 different numbers
If it would be mod(Cwp,5) then it would cycle trough 0 1 2 3 4 since that would be 5 numbers including 0

And now you make event like

Condition
Value of variable Cwp is equal to 0

Action
Set pathfinding path to and here you put Guard variable WpX1 WpY1

If Cwp equal to 1
Set path finding to WpX2 WpY2

And so go on

Technically you could do it with single event
By storing all your WpX# WpY# in array
And now you would call them by index

For example you make array called Path
Make child called Wp and give each child 2 other childs called WpX and WpY
And now it would be called as variable
Path.Wp[Cwp].WpX
Path.Wp[Cwp].WpY

Where here normally instead Cwp you would put index of variable like
Path.Wp[0].WpX or Path.Wp[2].WpX

But just so you can call it dynamically you use Cwp var to indicate which child it should refer to by index

I am not 100% sure should it be Path.Wp[Cwp].WpY or Path.Wp[Cwp]WpY
Or did i wrote expression correctly for calling array
Someone should correct me on this i do not remember how to do this
Maybe even Path.(Wp[Cwp])WpY or Path.(Wp[Cwp]).WpY

Hmm… this seems complicated. How would it look in code?

I literally wrote
Condition

Action

(Wait i missed one condition gonna edit post above)

With names of which you need to choose

Where rest is just explanation what each part do
And there should be either only 2 events if you go with array
Or as many events as waypoints you have +1 where that 1 would be event to move your guard