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

Yeah, thats pretty much what you would expect to use. Although it does depend a bit on how many you will have around. You might want to spawn them dynamically around the player instead of keeping them in the world.

Ofc im goin to spawn everything dynamically - its a client server game, im makin server from scratch not using ue :slight_smile:

Hi, im also trying to spawn cubes (to make a tetris game) from an AIController class, and the issue is that everytime a cube is spawned, the AiController Blueprint is duplicated at run time. The issue is that in BeginPlay() method, spawn is called so everytime AiController Blueprint is duplicated for the new pawn another SpawnActor() loop is run. it leads to endless spawning thus UE4 Crashes.
I’d like one single AiController_BP (at run time) for my whole cubes array. How to do so please ?

Spawning A pawn

Since it was related to the topic so I am posting it here.
First I wanted to ask if we can spawn a pawn using SpawnActor. When I tried doing it it was returning a null value and the engine crashed.

I am using a TSubClassOf<class MyPawn> to spawn the blueprint instance at run time.

I too have run into this. I have a Character that I am trying to spawn using GetWorld()->SpawnActor. I am also using TSubClassOf<> to reference what to spawn and I can assign that via Blueprints.

My CapsuleComponent is the root component of the Character I want to spawn. I need my CapsuleComponent to be assigned as Pawn… I admit I may be doing this incorrectly, but this is what I have that works so far.

So in my constructor for my Character class that is to spawn I have:
GetCapsuleComponent()->SetCollisionProfileName(FName(“NoCollision”));

That allows the Character to spawn.
Then in BeginPlay function, I have:

GetCapsuleComponent()->SetCollisionProfileName(FName(“Pawn”));

This works. Of course, if there are collision issues where the Character has spawned, then this will result in collisions that you may not like the results of.
I don’t know if there is a better way, but other options, including playing around with the Spawn Collision Handling Method, did not work so far. If I didn’t need the Capsule Component to be a “Pawn” profile, then the Character would spawn, and the Spawn Collision Handling Method would then apply naturally, but this technically breaks that.

It doesn’t feel right, but this is what I have worked out so far. Perhaps as I learn more about Unreal Engine the solution will present itself in respect to Spawning Actors that are setup with Pawn object type collision.

One more thought that I haven’t tried is to move the Mesh as the Root Component, and make sure it’s Collision Presets is set to CharacterMesh… I haven’t tried that yet. Perhaps that would fix it?