MoveToLocation with PlayerController

So I’m trying to create a game where right-clicking makes your pawn move to the target point through pathfinding. However, SimpleMoveToLocation isn’t supposed to be use in a network environment (source: Does UE4 have client-side prediction built in? - Multiplayer & Networking - Unreal Engine Forums) and MoveToLocation is only available in AIController.

To solve this problem, I implemented Droneah’s solution found here: http://droneah.com/content/ue4-getting-multiplayer-player-pawn-ai-navigation-work-c which basically has two pawns and two controllers (a custom PlayerController and a default AIController) and then tries to sync the pawns properly. This worked okay I guess, but it’s just becoming very clunky to work with.

I decided to try and refactor the code into something less obnoxious to work with and searched around a bit. I found this: Accurate Alternative for 'Simple Move to Location' - Blueprint - Unreal Engine Forums which seems to suggest that I can cast my PlayerController to an AIController and then use MoveToLocation. I was a bit dubious and as I thought, the cast ends up returning NULL.

It seemed a bit annoying to me that there’s no direct access to pathfinding in the Playercontroller since a lot of games need it (MOBAs, RTSs, etc). But I didn’t give up, so I tried making a wrapper that inherited PlayerController and then copy all the pathfinding logic from AIController over to it. Despite it breaking the DRY principle, it sounded like a good idea at the time. However, I got stuck on a compilation error on “DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FAIMoveCompletedSignature, FAIRequestID, RequestID, EPathFollowingResult::Type, Result);”. Some problem with FAIMoveCompletedSignature.

However, I didn’t give up! I kept searching and found a reply by MieszkoZ saying that the correct way to do this is with a PlayerController without a pawn controlling an AIController with a pawn. This still seems kind of messy to me, but at least I can remove the proxy pawn that I’m currently using. Is that the right way to go about it?

1 Like

I’m making an RTS myself, and here’s how i have it working

My player controller possesses my RTSCamera pawn, which uses WSAD to move around and all that junk. All of my characters have AIControllers attached to them. When i left click on a unit, i get a reference to the unit through the mouse trace, and do something like:

AIController* ai = Cast<AIController>(unit->GetController());
ai->moveToLocation(location);

Its working perfectly for me so far. Give it a try.

Unless you have some heavy pawn related features, I would encapsulate the AI-Controlled-Pawn into your own pawn and control it giving commands (like move to) by using the AI-Controller. Now if you have very pawn dependend features I might think about own implementation of “move to location” for your pawn (but I see you tried that already).

Edit: Essentially what Makotech222 wrote ^ above.