Making AI follow actor indirectly?

I’m trying to design a follower who will generally stay within range of the player, but will constantly be on the lookout for areas to investigate around them, with the end goal being that they would randomly find items and bring them to the player, or just do idle activities in different spots. Right now I can make the follower to go random points within the map (which are marked with a specific actor class) but I can’t make them stay within range of the player. When I do confine them near the player, they still try to go to areas that aren’t inside that radius and then start printing error messages.

Basically, I want the follower to make an array of all the objects that are within a sphere around the actor, pick a random spot out of that list and go to it. Is there an easy way to do that that I’m missing?

You can do it (at least) following two ways.

First is add a sphere collider component onto your main character and give it a large enough radius to overlap things around it. Then set both the actors you want to be detected and the main character to generate overlap events. Then used the “GetOverlappingActors” function to populate an array of the actors you want and get a randomIntegerInRange from 0 and the Length of that array - this can be your “random pickup actor for the follower”. Just get its world location, send it to your follower and use there the aiMoveTo (more precisely - send the found actorRef to the follower and in that BP get its location etc…)

Second solution, use the SphereOverlapActors Node. It is easy to setup but it fires only on demand. Meaning if you would like it in every tick, you need to hook it in the Tick function. The Overlaps from solution1 fire every tick automatically (as long as you don’t disable the collider)

What you might need for both solutions: Hold a reference (save it to a variable of actorRef) which was your last found actor, so your follower doesn’t switch on every tick. And in your endOverlapEvent reset that variable (set it to Null), so your follower can pick a new one.

I tried using the first method, and the compiling works fine, but once I get into the game the follower moves into the center of the map and starts spamming me with error messages. The error points me to the AI Move To and says “Accessed None trying to read property CallFunc_Array_Get_Item” so i assume something is going wrong with the array and the follower isn’t sure where to go?

This is the blueprint I came up with, which is a BT task and currently the only thing in my follower’s behavior tree. I also made sure every point of interest (POI in this case) had an overlapping event. Pippa is the name of the follower.

I’m not really sure how to save a variable, I’m having a lot of trouble figuring out how to use the same variables between tasks.

I havent worked with BT much, but I guess you need to save it in a Billboard value to Share it between Tasks.

The Error ist quite simple, Imagine your getOverlappedActors Returns None…your random Node get’s one between MIN 0 and MAX -1 (which doesn’t make Sense) and from that you Access an Array with it’s Index <0 which also doesn’t work, Arrays start at 0.

Why are you getting the element from an Array of the PC and Not from the Return Array of your getOverlappedActors Node?

Why are you getting the element from an Array of the PC and Not from the Return Array of your getOverlappedActors Node?

It won’t like me connect the Return Array directly to the Get node… it just says it isn’t compatible. Is there a better way to do that?

Sorry if these questions are dumb, I’m very new to programming.

No Problem, everybody started once :wink: try pull Out from the Return of the overlap and Type in get. Basically make a new get Node. UE Sometimes does such Things when working with Arrays. The Return of the get goes again Into your getWorldLocation node

Okay, I did manage to connect the Get node correctly. I also removed the -integer, so now the max is just the length of the array. It still is giving me the same error, though, so I guess it’s having trouble finding the overlapping actors?