# How do one NPC follow a succession of target points?

I have a project that contains 1 NPC (AI) and 2 target points. In the project I can make the NPC follow any of the 2 target points, but I can not get it to follow target point 1 and then target point 2, and that is what I would like to do, so that I can make a path.

What I would like to know:

tp1 = target point 1 and tp2 = target point 2

What I can do:

In creating the variable that sets the target, I checked the box: instance editable. As you can see in the image, I can set the target in the level editor.

Blueprint of the NPC:

When I went to do the programming for him to follow the other target point (tp2) after reaching the first one (tp1), I did not get it in any way. No results appear with my search, which for me does not make any sense, because in the level editor I can easily.

Looking at the level editor / Looking at the blueprint:

I also tried using the Get All Actor of Class, but without success.

Link from where I also looked for help:

https://docs.unrealengine.com/en-us/Gameplay/HowTo/FindingActors/Blueprints

EDIT 1 (Attempt based on ColdSteel's answer)

I created a variable of type array of actor called varHelp:

As you can see, I added 2 elements to this array, but I can not edit any of the 2.

I researched a way to change this, until I got to this link:

Unfortunately it caused an error in the code, causing me to give up for now and look for another way to solve my main problem.

Although I created 2 elements in the array, none of the elements appears in the level editor:

But I can create new elements and define them as the desired target points:

However at the time of defining them in the construction script I again error in some detail:

EDIT 2 (Another attempt, based on Stephen's answer)

With the help of Stephen I discovered how to promote a variable (right click button):

New blueprint:

But I still have not got the expected result, see below an explanation of what happens:

Red code (Simple Move) => The NPC moves toward target point 2, without first passing target point 1.

Blue code (AI MoveTo) => Nothing happens.

EDIT 3 (Attempt made thanks to the progress I made with the answers of ColdSteel and Stephen)

This time I managed to get the NPC to do the correct path, but it is not dynamic since the NPC does not go directly from target point 1 to target point 2.

New blueprint:

I know the loop executes all at once, so the value registered in the function to move the NPC was always the last.

With the creation of two Boolean variables, I was able to "circumvent the operation of the loop".

With the delay I assure you that it will allow NPC time to arrive at target point 1, only to go to target point 2.

I know that what I did is far from ideal, to have an idea I would have much more problems if there were more target points.

EDIT 4

Now I can get the NPC to go to target point 2 immediately after reaching target point 1.

I added a collision box to target point 1:

Changes in the blueprint:

Important to inform, that such a loop was not accused as infinite, since I added a collision box only to target point 1.

Now I have to know how to make a code for any amount of target points.

EDIT 5 (Finally I did it!)

For this I used the "Do N" and added an integer variable that will serve as counter.

Blueprint:

Basically, every time the NPC collides with a target point that has collision box, the loop with the "Do N" will be reset, but with the help of the integer variable that serves as a counter, the subsequent target point will be set correctly.

I do not know if this form I found is the most effective and functional. Please feel free to comment and correct me.

So your problem is... that you dont see TargetPoint2 in cast list ? - if so then it behaves as Intended because both TP and TP2 are instances of class TargetPoint. To distinguish between you TP placed in world - you could use tags or anything else just not the Cast.

Cast would work only if you made a Class for each TP like TP1 that inherits TargetPoint, TP2 that inherits TargetPoint and so on.

• I looked at this link (docs.unrealengine.com/en-us/Gameplay/Tags) and this (answers.unrealengine.com/questions/820380/…), but I could not define what I have to do. From what little I understood, I will have to work with parents and children. – Boneco Sinforoso May 11 at 21:57
• I discourage you in doing so... you better define an actor for your NPCs and put an array if targetPoints there... once they reach one target point advance the index of array to the next targetPoint and so on. – ColdSteel May 11 at 22:01
• I'm sorry for my ignorance, but I do not know how to do it. I'm newbie. Could you send me a link from a site that will help me do this? – Boneco Sinforoso May 11 at 22:10
• I am not aware of such tutorials, but this is trivial... – ColdSteel May 11 at 22:15
• Open your NPC BP, add an array, add the targetPoints, or spheres or whatever that has transform... – ColdSteel May 11 at 22:16

If you are trying to get an AI to move to two simple locations then use the AIMoveTo Function.

As you can see, it has an on success execution pin so when you successfully get to the first location, you can execute the movement to the second.

-- EDIT FOR WHAT SOME OF WHAT YOU'VE TRIED --

When you do a Get All Actors of Class, you are doing just that. UE4 is finding everything that matches the Target Point Actor but after this, you're only getting the first entry (0) in the array.

To get all Target Points you'd need to loop through that array and match the result with what is in the array in your AI pawn and if it's a match, move it. Get Display Name is probably the best option here.

• I can not associate the target points to variable. – Boneco Sinforoso May 13 at 22:55
• Have you tried right clicking on the output of the Get and promoting to variable? Or, on the output of the Find Actors of Class and store the array to use at run time? – Stephen May 14 at 11:34
• Thank you. I edited the question again, could you tell me what I did wrong this time? – Boneco Sinforoso May 14 at 22:40
• I understood why the red code in the NPC is going straight to target point 2. I just need to find a way the loop does not execute all at once. – Boneco Sinforoso May 14 at 22:55
• It's missing a little detail to be all right. The previous 2 comments referred to edit number 2. Now I've done edit number 3. – Boneco Sinforoso May 15 at 0:05

First, after the Event BeginPlay, you will use the function Get All Actors Of Class. This function will output an array, just right click on the output variable of the function to Promote to Variable:

After that, you will use a ForEachLoop more a function DO N, and before that you should create an integer variable (default value is 1) to command the function DO N:

The loop will execute until the number of target points is finished, without the function DO N the NPC would always be taken towards the last target point. So if you have 5 target points, and do not have the function DO N, the NPC would go straight to target point 5 without first going through any of the other target points.

Since the value of the integer variable is 1, at the first execution of the loop, what is inside the DO N will only be executed once, which is what matters:

Once time, then the target point to follow is 1.

The way I have made the next target point to be defined is that when it hits a target point, the integer variable is raised by 1 and the loop restarts.

For this to happen correctly, you must add a box collision to all target points except the last one:

This way you can use the Event ActorBeginOverlap.

In EventActorBeginOverlap, you use the CastTo TargetPoint, and with the help of a sequence you reset the function and increase the value of the integer variable, in order to start the ForEachLoop again:

Reset DO N:

ForEachLoop start again:

You do not need to use print.

That way, as soon as the NPC collides with target point 1, the DO N will be reset, the integer variable will increase its value by 1, and the ForEachLoop will start again.

Now, the DO N will execute 2 times, setting target point 2 as goal. And so on...