Cast APawn with ConstructorHelpers

Hi everybody

I’m trying to get a pawn from my blueprint and what i’ve done is getting a my blueprint from c++ with this code
static ConstructorHelpers::FClassFinder PlayerPawnBPClass(TEXT(“Blueprint’/Game/Blueprints/AnesthesistCharacter’”));
but when i cast there’s an error telling me that i cannot convert ConstructHelpers:: to const FSubobjectPtr &

here’s the full code

APawn* AQuietOpsGameMode::SpawnDefaultPawnFor(AController* NewPlayer, class AActor* StartSpot)
 {
		 APawn* pawnPlayer{};
	
		 static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("Blueprint'/Game/Blueprints/ThiefCharacter'"));
		 pawnPlayer = Cast<APawn>(PlayerPawnBPClass);
		 return	pawnPlayer;
	
 }

Hwow to cast properly my blueprint to a pawn ?
Thanks

Hi, you don’t need to cast it like that, remember that only one GameMode class exists at a time , but there can be several characters and casting wouldn’t really help you would it?, so how can you get a reference to your character?
Well in general when a game is playing you can simply “GetGameMode” from any other c++ class (or blueprint class)…
So for example you would go to your characer.cpp class and add :

void AMyCharacter::BeginPlay()
{
	Super::BeginPlay();
		GameModeReference = (AMyGameMode*)GetWorld()->GetAuthGameMode();
}

Of course you also need to add a variable to character.h class:

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Gameplay)
	class AMyGameMode* GameModeReference;

If your character is a blueprint you would do the same thing in your blueprint,
you can simply GetGameMode and store reference to it into a GameModeVariable (object type).
Or you can Cast it like this (Read my Answer - by “Jurif” on the bottom):

Ok, now that you have a reference to your GameMode, you could simply do this, in your character BeginPlay function you would add a line to store reference of self(this) to a GameMode variable :

 void AMyCharacter::BeginPlay()
 {
     Super::BeginPlay();
         GameModeReference = (AMyGameMode*)GetWorld()->GetAuthGameMode();
         GameModeReference->ReferenceToMyCharacter->this;
 }

In order for that to work we first need an appropriate variable in our GameMode.h class:

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Gameplay)
	class AMyCharacter* ReferenceToMyCharacter;

So there you go , this way at the Begin Play your character will get a reference to gameMode and store it into a variable, and then also send reference to self back and store it to GameMode->ReferenceToMyCharacter variable.
Now you have references on both ends, and you can use this method on all classes.

Now to wrap this up , in GameMode.cpp you are spawning a blueprint, it should look like this:

static ConstructorHelpers::FObjectFinder <UBlueprint> MyCharObject(TEXT("/Game/MyChar/Blueprints/MyCharacterBP"));
	if ( MyCharObject.Object != NULL)
	{
		MyCharacterBP = (UClass*) MyCharObject.Object->GeneratedClass;
	}

And of course you need a variable in the .h class :

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category =MyAwesomeVariables)
		UClass* MyCharacterBP;

There you go, good luck :wink:

Thanks for your answer, i’ve already tried to so something similar to that but i got “.Object is not a member of ConstructorHelpers::FClassFinder” knowing that i’m with the 4.6
and when you tell that there is only one game mode at time, i’m trying to run it on network, the client and server will run only one game game (who will run on server) ?, or each of the client and the server will run their own game mode distinctly ?

Thank you for your time

Yes in multiplayer game it’s different,
The GameMode exists only on the server and controls the core gameplay stuff, so on the clients you need to use GameState which can hold all info about the game , exists for client and server. You can read about in the documentation.

I’ve found some documentation about the difference between Game State and Game Mode but none said if the game mode is shared between server and client or if it’s distincly, and about this syntax i found that it has changed from this

 if ( MyCharObject.Object != NULL)
{
MyCharacterBP = (UClass*) MyCharObject.Object->GeneratedClass;
}

to this

 if (PlayerPawnBPClass.Class != NULL)
	 {
			 MyCharacterBP = (UClass*)PlayerPawnBPClass.Class->GeneratedClass;
	 }

Maybe i’m doing it wrong, but i’ve got no error using .Class instead of .Object

Yes both methods should work, in both cases we are getting the Class.
It’s interesting thou, I never tried to do Class->GeneratedClass,
another example is the way I have my HUD setup:

static ConstructorHelpers::FClassFinder<AJF_HUD> HudBP(TEXT("/Game/UI/Blueprints/JFHudBP"));
	if (HudBP.Class != NULL)
	{
		HUDClass = HudBP.Class;
	}

All that really matters is that it works :).

The difference between being able to use MyCharObject.Class or MyCharObject.Object comes down to the ConstructorHelper Finder you use. There is FClassFinder (.Class) and FObjectFinder (.Object)

You are trying to assign an object that represents the pawn class to a pawn object. No typecast will help that. If you are trying to create a new pawn using that class then what you want to do is pass that class object as a parameter to SpawnActor().

If you are only trying to get the current (existing) player pawn then this is definitely not the way to go. You should just call GetPlayerController() with the index set to zero (in networked multiplayer, it’s always zero). Once you get the controller then you can call GetControlledPawn() to get the player pawn.