How to programatically spawn some pawns and attach controllers to them?

Hello. I’ve been searching through the documentation but i didnt find good example. So i got pawn class which consists of cube mesh. I got also MyAIController class. SpawnActor spawns the pawn, but how should i create the controller for it? And is it possible to control the pawn controller using my main character controller?

What i mean is to send command to pawn controller using the playercontroller, and the pawn controller would do things i want with the pawn

Call of the APawn::SpawnDefaultController() will create AIController instance for pawn. You may send commands for pawns from one place such as APlayerController or HUD.

But what if i have my own AIController? How can i select which class is the controller?

What i mean is i got MyPlayerController which is being set up in GameMode, and i got also MyAIController. Wouldnt the function be confused which one to choose? it takes no parameters so.

For a selection of the controller’s class you need to create blueprint of the pawn and to select the class in the field AIControllerClass.



/**
 * Default class to use when pawn is controlled by AI.
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(DisplayName="AI Controller Class"), Category=Pawn)
TSubclassOf<AController> AIControllerClass;


Or you may assign the class in initialization’s code of the pawn without blueprint.

Im doin all in cpp. Thanks for explanation. Btw is it only me, or UE really lacks c++ examples?

1 Like

Check out ShooterGame!

Where are those examples? In Editor? Launcher? Website? Im lookin for c++ not blueprint if possible

At the top of the c++ forum page, wiki tutorials. Shooters one of the example tutorials

ShooterGame is available from the Learn tab in the Launcher, there’s quite a few C++ examples of games there :slight_smile:

There are no enemies spawning in that tutorial. And thats what im lookin for. How do i create other players and attach controllers to them

Setting AIControllerClass in Pawn constructor works and the aicontroller moves him to random location as specified in Tick function. But this works only when i drag and drop the pawn in editor. When i spawn the Pawn with SpawnActor it doesnt move.

Thats the MyAIController

Thats the pawn

When i drag and drop i see debug output that controller’s consutructor is called and then it ticks. But when i use SpawnActor i see only message from Pawn’s BeginPlay() method, when its clearly stated in constructor that AIControllerClass = AMyAIController::StaticClass();

What im doin wrong?

Okay edit2:
I made it work. I removed the AIControllerClass from pawn constructor. Instead i use SpawnActor for Pawn, SpawnActor for controller and then Possess the new pawn with new controller. Is it the fully proper way to do this?

And other question, how to properly use MoveToLocation? It seems to not move my pawn (SetActorLocation works fine tho). Do i need to create a movement component first for the pawn to use MoveToLocation function??

Do you call APawn::SpawnDefaultController() for actor after SpawnActor?

No. I use SpawnActor to create instance of controller and then Posses. it works. The reason movetolocation isnt working is that i dont have movement component which im goin to implement now. Wish me luck!

Okay so what i got so far and what doesnt work:

The “main” character controller spawns a pawn when E is pressed, then MyPawn->SpawnDefaultController() is called. I see debug output from controller’s constructor and tick. I created CharacterMovementComponent ( its empty, what should i put inside?? ) and added it to pawn class and initialized in constructor. But i see that UMovementComponentForMyPawn::TickComponent is not being ticked. There is no documentation at that at all i digged the forum, wiki, answers and i still didnt find that basic info how to succesfully control those **** pawns.

There are pieces of my code.

I changed pawn to character and it worked. But thats so weird… oh well

Not really weird. CharacterMovementComponent requires a character based class. Hence the “Character” part :slight_smile:

Its kind of daft though, because there’s really no reason for movement components to require a special base class, its just a holdover from the inheritance-based nature of old unreal engine code I think. There’s even a trello card on the development trello to make the movement component work on anything. So clearly they know its dumb :slight_smile:

It’s because Character and CMC talk to each other so much and talk to lot’s of different properties of one another, so they have to be used together or not at all.

Sure, but actually that suggests a refactor is required, because movement is a pretty basic usage case and should not really be tied to the type of thing needing movement. Its kind of my point really, using real composition you could have simply added the movement component to the pawn and have it work. Equally you could have developed a special movement component and had it work across character and pawn and have it work. That’s the real advantage of components (the compartmentalization of specific features to composable classes).

Is it okay that i will use character class for every monster/player in the world instead of pawn?