URGENT: Get AI Character Location

Hi,
I think what I am trying to do is quite simple, but I can’t get around a solution for it.

Basically I am trying to get the location of an AI character, which is obviously not the player character. I want to use the AI character’s position to process something in my widget.

In other words how do I get the position of an AI character in my Widget event graph?

I know there is GetActorLocation, but this would get me the players location not the AI character one.

Would really appreciate a prompt reply.

Thanks in advance.

The GetActorLocation Blueprint node will get you the AI character’s location if you plug in the AI Character actor as the target parameter.

So, actually the problem is, more specifically: how do you access the AI Character actor from within a Widget Blueprint owned by the player? That depends, but there are multiple ways.

One way would be to use a TActorIterator to loop over all of the AI Characters in the world until you find the specific one you want. You might want to perform this loop lookup just once when play begins, and store a reference to that target AI Character in your Player Controller or even the Widget Blueprint object — whichever makes more sense in your case.

Alternately, you could make the AI Character send a reference of itself to a more easily accessible object, perhaps during its BeginPlay event. For example, on BeginPlay, you could have the AI Character grab the first PlayerController and tell the PlayerController to store a reference to the AI Character using a custom Blueprint function.

If your game needs to support networked multiplayer, it may be preferable to have the AI Character register itself onto human-controlled Pawn actors instead of just a single PlayerController, since there will be more than one player, and PlayerControllers for other players are not accessible by connected clients anyway.

Again, the best approach depends on your game and your design. But, one way or another, as long as you can stick a reference to the AI Character you want in your Widget Blueprint object, then getting that character’s location is the easy part.

Hope that helps!

Hi,
Thanks a lot for your help. Yes that is the problem.
I have spent so much time trying to look for ways with which I can reference the AI character with and I just couldn’t.

I can’t manage to find the “TActorIterator” block and I am not sure how I can store the reference to the AI once the TActorIterator finds it. I would be really grateful if you can post on here a picture of a quick example of how you would do this via the event graph.

Thanks in advance.

Sorry, -G. I must have had a brain spasm the moment I started writing about the TActorIterator. That’s how you do what I was trying to say using C++. We were talking about Blueprint scripting, obviously. Let me try again.

What I meant to suggest is the Get All Actors of Class node.

Using that node, you can find all the AI Character actors in the world and loop over them until you discover the exact one you want. Alternatively, the Get All Actors with Tag node might offer you a bit more utility, if you’d rather go that route. It works very similarly to its ‘of Class’ cousin node.

Hopefully one of those ways will get you where you want to go. And again, sorry for the earlier confusion!

Hi,
I now need to do the same thing but with C++, so can you please tell me how I can go about doing that. Note that my knowledge of C++ is very limited.

Thanks

Figured it out use, simply use GetActorLocation();