Download

Spawning Player

Hi,
I’m new to the unreal, and I’m working on a game. I want to spawn player at the beginning of the game at a specific start location. From what I read, I should spawn an actor of the player class, and then use “Possess” on player controller. After some tries, I wrote a code that spawns an actor in gamemodebase class, but it’s not working. I would appreciated any help.

// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "TPCharacter.h"
#include "GameFramework/GameModeBase.h"
#include "TopDownFPS1GameModeBase.generated.h"

/**
 * 
 */
UCLASS()
class TOPDOWNFPS1_API ATopDownFPS1GameModeBase : public AGameModeBase
{
	GENERATED_BODY()

		virtual void StartPlay() override;

public:
	//ATPCharacter is a player character
	UPROPERTY()
		TSubclassOf<class ATPCharacter> CharacterClass;
};

// Copyright Epic Games, Inc. All Rights Reserved.


#include "TopDownFPS1GameModeBase.h"

void ATopDownFPS1GameModeBase::StartPlay()
{
	Super::StartPlay();
	check(GEngine != nullptr);
	GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Everything's alright"));
	FVector Location{ -900.0f, -400.0f, 5.149992f };
	FRotator Rotation{ 0.0f, 0.0f, 0.0f };
	FActorSpawnParameters PlayerSpawnParameters{};
	PlayerSpawnParameters.Owner = this;
	APawn* Player_Character = GetWorld()->SpawnActor<APawn>(CharacterClass, Location, Rotation, PlayerSpawnParameters);
	GetWorld()->GetFirstPlayerController()->Possess(Player_Character);
}

You need to spawn a player controller in login 1st, then after that login stuff has run and you have joined as a spectator then you will want to make the character and have the controller posses that pawn or your character. Then in restart game spawn him, as you spawn him you will posses the pawn setup your weapons or whatever in hand, so when you appear in game you have all your stuff and whatever it is he needs to be ready to go and start playing.

I know this is vague but that it the process you should follow.

Read the functions in BaseGameMode CALLED Login() Prelogin() RestartGame() those will require your attention and you will end up doing a lot of stuff there. Really read the whole file it is all important, but those 3 functions you will for sure need to be in.

1 Like

FYI you dont need to do any manual spawning (unless you really want to). In the GameMode you can define your default Pawn and PlayerController classes and the player will automatically spawn on a PlayerStart. These GameMode settings will also appear in the editor.

/**
* 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)
{
	// 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();
}

Then during the game you can Possess() a different Character if you have such mechanics.

1 Like

I set my default player class as a BP class derived from C++ class, and it’s working, but when I press play, my character is spawned on the location of the camera. so if I do something with map and “move my view”, my character will spawn somewhere else. I tried to place an actor BP derived from c++ class inside my game, and then using him as player character, but while I did something similar when I was making a making pong game (I had a pawn, which I set to be controlled by player), I did it in blueprints, and I don’t really know, how to do it in c++.

Actually nevermind. I resolved that by adding Player Start to the level. But thank you for help.

1 Like

nice, that it was that easy.

You can make one big setup for loading your character in game. Guess it depends on what you are after.

1 Like