Unable to move a character from C++ with MoveToLocation()...
I've been working on a turn based game in which characters can perform actions just like X-Com. I've been implementing my game in C++ for easier code maintanance, refactoring and debugging...
And for the task I have at hand I am restricted from using the AI system (Behaviour Trees) of the Unreal.
For character actions (like move to a destination, attack some target, use some skill etc.) I've implemented a generic action management system which is based on a custom base class called UAction.
Whenever a character takes an action, an instance of a class derived from UAction (Say UMoveAction) takes control of the character. This action instance has access to the character's ai controller instance, the pawn/character class instance and also its animation instance. And it makes the character perform the action at hand respecting the action type (Move,Attack etc) for which the derived UAction is implemented for.
Now my problem is I am trying to make the character move using Unreal's path planning. For this I've figured out there is the MoveToLocation(...) method inside the ai controller class of the character (derives from AAIController).
I have generated NavMesh which I can display with "P" button. And Navmesh works without a problem for other parts of my code.
My problem is no matter what I do my characters always fail to move. Once MoveToLocation() is called, it returns success but it seems it immediately fails in 1-2 frames following the tick it is called. The characters briefly starts playing the move animation but then they immediately stop.
Here is the section of the code in UMoveAction which is responsible for moving the character
owner is a reference to the character to which this action is applied to. ownerController is a reference to this character's ai controller and ownerPathFollowingComponent is the UPathFollowingComponent reference which is set in character's ai controller.
I've tried MoveToLocation(...) with many different parameter combinations (commented out in code) but it seems none of them make the character move (more then 1-2 frames).
What might be the problem in this case? Could you suggest where I should look at to solve this?
I finally solved my problem.
I used to instantiate both the character controller and the character classes inside C++ code. I then call the possess function of the controllers to possess their corresponding characters within C++ again.
It appears the the character movement component needs additional initialization to set up correct. And instantiating the characters from the C++ side does not do that.
To do it I first set Auto Possess (or whatever its name was) to true in my character blueprint on editor side so that whenever the character is instantiated its corresponding controller is also instantiated, set up and auto-possesses the character. I also set the controller class to my own controller class in the editor.
Then I created the characters again from the C++ calling the SpawnAIFromClass(...) function. The class I pass in the function is my blueprint class.
This fixes the issue.
I hope the Epic team adds some detailed documentation for the C++ API. Not only the static documentation (which is missing a lot of details by itself as well) but perhaps a handful guide of "dos" and "do nots" or "how to dos" would be awesome for C++ coders.
answered Feb 22 '17 at 08:26 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here