Control a pawn off start?

So I understand that in a gamemode constructor, you should do all of your player spawning. I have gotten this to work by spawning my pawn into the game. The only problem is, I can’t control it until I select it in the World Outliner. I understand that when you set the default pawn class in the gamemode constructor, it spawns a pawn object of that class. How can I get a reference to this object so that I can possess it off start?

My current code is here: machinesGameMode.h -

Thanks in advance.

So you spawning actor or use default pawn?

Using default pawn

Tried to use pointers to reference the objects, but I still have the same issue. To control my default pawn, I have to click on it in the World Outliner.

I believe if you are manually spawning the pawn like this you need to possess it as well.

That said you should not have to do that. Simply setting the pawn as you did in the link posted with the question should be enough. I would go through the scene and make sure there was only one pawn being spawned. It’s also worth making sure that all of the objects you expect to be there are there ie: your game mode, player controller, etc.

machinesGameMode.cpp - Went further and tried to use Possess function in the constructor and in the BeginPlay function. Crashed on start. Then attempted to use the UnPossess function. Crashed on start.

Where am I supposed to use this function if at all? Half of the things I do in cpp just crash the engine!

You doing this complitly wrong, don’t create objects from defult objects it confuses engine, you should use SpawnActor or CreateObject. Also by convetion you should nto use constructor for any gameplay code, only for default varables you should also not create any objects in constructor. Reason is constructor is used for by reflection system to read default properties that are alter used for example in blueprints, use BeginPlay() insted to spawn objects, Also GameMode spawns PlayerController and DefaultPawn for you on PlayerStart and it automaticly posses pawn, you can disable that by not setting DefaultSpawn and you can spawn pawn for yourself and posses.

Sorry, I posted my earlier comment about possessing before refreshing the page. In my world outliner I don’t see anything that shouldn’t be there (except that it’s spawning another camera actor for no reason when my pawn has one, would also be great to know how to disable that).

This is the only class that should be spawning anything other than components though, so I don’t know what could be wrong. It actually was automatically possessing before I made this post, but then it had stopped and I didn’t know why.

Anyway here is my world outliner after starting the game:

I wrote a comment but it disappeared…

Anyway, my default pawn is not automatically possessed, or at least controllable until i click on it in the outliner, and if I don’t set default pawn, then it will just spawn a pawn of type ADefaultPawn anyway, and Possess still crashes my editor.

None of that would matter if my pawn would be possessed off start without me having to click on it in the World Outliner…

It seems that it’s my pawn class that is having issues being possessed on start. If i use a different pawn, I don’t have to select it. I’m not sure why, but it’s here:

Header: RTSCamera.h -
cpp: RTSCamera.cpp -

Okay, so the problem isn’t that the pawn isn’t moving, it’s that the camera doesn’t follow it unless I select it. Maybe because it’s spawning a second camera…

So I found out that my problem was the the camera was not following unless I selected it. I set the AttachParent attribute of CameraComponent to the root component of the pawn.