Ai Following Waypoints

Hello iam working a base for a tower defense game . I want the ai to follow a array of invisble objects (Billboards). I think i got the basic logic down but after days of testing it just doesnt work. The Ai would move from Wavepoint0 to Wavepoint1 to Wavepoint3… and after i save the level or compile blueprints it would switch up the order. It was driving me nuts.

As you can see at first the blueprint gets all the actors of the class "Wavepoint " first which is just an actor with a “set actor location”
Then the branch checks if the pawn is in motion. if it isnt it checks if the waveindex ist still lower then the lenght of the array of wavepoints.

Whats really important here is the order of the array of the Wavepoints, but the array should be automaticely in order which it is. But what i found out that unreal sometimes i still dont quite know when changes not the name that is displayed but the ID of the object.

So there is like Wavepoint01 Wavepoint02 Wavepoint03 and then suddenly Wavepoint_353 although its displayed as Wavepoint Wavepoint04 . And that of course messes up the order of the array.

So i think the logic of the blueprint works its just the order of the array that gets messed up by the naming system of unreal. And i dont want to use expensive sorting algorithm.

Iam sorry for the block of text but iam trying to figure it out since days.

Hi,

As you’ve already experienced it yourself, the GetActorsOfClass is not guaranteed to return your actors in order! You have to sort them yourself.

First of all, it’s not a good idea to use GetActorsOfClass withing a function or event that ticks every frame (I believe your Wander event ticks every frame).

If you don’t have too many points, then a simple yet dirty workaround would be to go to your level blueprint, drag your waypoints one by one and add them to an array in order. Do this once using the Begin Play event. Next, using this same Begin Play event, get your player/ai pawn, pass this array to them and store it into a variable somewhere there. There you go! You have a sorted array that you can use in your AI.

Another but much better way would be to have a separate class for your Waypoints. Keep a static count reference there which increments and gives waypoints a new ID whenever you add a new waypoint to your scene. You can then use GetActorsOfClass in conjunction with that static count reference to create an array and then sort it based on that ID. However, this cannot be easily done in blueprints and you would be better off implementing it in C++.

Hope this helps.

Thanks really much for your fast awnser . My c++ expüerience is reallllllllly basic i just know about the basic stuff like loops and if statements , and dont even have a clue on how to to use c++ with ue4 although iam interested in learning it. I feel like when iam trying to implement something into unereal 4 i have to think more about how to set it up in ue4 then how the logic actually works like in actual programming.

I think that workaround would work. So i would have an array in my levelblueprint in which i add every waypoint manually after i placed them in the level to that array? and then call that array from my ai controller class?

The weird thing is that getactorofclass actually has the actors in the right order until ue4 decides to give them different id names at some point.

Thanks for saving my mind tonight.

i dont quite know hot i get the array from the level blueprint to the ai controller.
The ai also spawns later and isnt there from the beginning.

This is how my level blueprint looks now.

Yes. You would use that array in your AI Controller blueprint.

As I already mentioned above, the GetActorsOfClass is not guaranteed to return your items in the order in which you added them to the scene. It might happen that they end up in the order you want, but you shouldn’t really count on it :slight_smile:

Good luck on your UE4 adventure!

You’re very welcome :wink:

Hey,

Did you figure out how to do it?

No not yet i dont know how to get the array from the level blueprint to my ai class.
Because the ai class + the pawn isnt in the level already but spawns later, so i cant reference them in the level blueprint.

While you cannot access the Level blueprint from any other blueprints, you can access GameMode, GameInstance, and GameStates blueprints from pretty much any other blueprint including from Level blueprint itself. What this means is that instead of saving your Waypoints array in the Level blueprint, you should store them in any of the other mentioned blueprints (i.e. GameMode, GameInstance, or GameState). If your game is not going to be multiplayer, then it might not really matter in which of these blueprints you store this array. Let’s see how it can be done with GameMode:

Create a blueprint based on GameModeBase (Let’s name it BP_MyGameModeBase). Open it and create an array variable of your waypoints type. Compile and close it.

Go to your Level blueprint and store the waypoints’ array to your GameMode as such:

Remember that you don’t need to store these in Level blueprint anymore.
Now in your AI blueprints, whenever you need a reference to these waypoints, simply get the game mode, cast it to your BP_MyGameMode, and then get the waypoint array stored inside the GameMode.

Thank you really much!!! i think i nearly got it now but for some reason my cast always fails in the level bluepritn as well as in the ai class.

== level Blueprint

== Ai Class

The first picture is the level blueprint and the scecond is the ai controller

And in the TowerDefense Blueprint i just created a variable of the type wavepoint array

Did you set your GameMode in Project Settings and World Settings?

Oh that was it !!! Its finally working!!! THANK YOU SO MUCH!!!