Download

How to set default pawn and player class without blueprints?

Hello L&G…

I´m trying to set my default pawn/character (ACharacter) to make it playable at game start…
I can create a Blueprint from GameModeBase class and setting it on Default Pawn Class, but I don’t want to use BP, I´m sure there is a way to do it via C++

Any clues?
Thanks…

The way you would do it resembles the way you would do in BP.

You need to create in C++ your GameMode class derived from the GameMode of your choice, lets say GameMode or GameModeBase, and inside the constructor you would write:



    DefaultPawnClass = AYourCharacter::StaticClass();    // in blueprint it would be GetClass()


or in a more fancy way these days:



    static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter"));
    if (PlayerPawnBPClass.Class != NULL)
    {
        DefaultPawnClass = PlayerPawnBPClass.Class;
    }



Example with comment explaining where to set default GameMode in the Map and entire Project. Your game mode is a subclass of Unreal’s “AGameModeBase”.



/**
* Primary game mode.
* Set default mode for the map - World Settings >> GameMode >> GameModeOverride
* Set default mode for the project - Edit >> Project Settings >> Maps & Modes >> Default GameMode
*/
APrimaryGameMode::APrimaryGameMode(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
 LOG("APrimaryGameMode: APrimaryGameMode()");

 // set default pawn, player controller, and HUD
 // these can be read-only viewed in the editor:
 // Edit >> Project Settings >> Maps & Modes >> Default Modes
 DefaultPawnClass = AFirstPersonCharacter::StaticClass();
 PlayerControllerClass = APrimaryPlayerController::StaticClass();
 HUDClass = APrimaryHUD::StaticClass();
}