Character is null when playing in dedicated server

Hi everyone,

I have created a new third person project to check if this can work and as I thought it does not. I am handling the input in the player controller and sending the value to the character. And it gives a null reference, What I did was this.

Created a new third person project (C++ not Blueprints)

Created a PlayerController and added necessary components for handling only the forward movement

PlayerController .h

	ADedicatedServerTest_PC(const FObjectInitializer& ObjectInitializer);

	// Called to bind functionality to input
	virtual void SetupInputComponent() override;

	void MoveForward(float AxisValue);

PlayerController .cpp

ADedicatedServerTest_PC::ADedicatedServerTest_PC(const FObjectInitializer& ObjectInitializer) :Super(ObjectInitializer)
{

}

void ADedicatedServerTest_PC::SetupInputComponent()
{
	Super::SetupInputComponent();
	
	InputComponent->BindAxis("MoveForward", this, &ADedicatedServerTest_PC::MoveForward);
}

void ADedicatedServerTest_PC::MoveForward(float AxisValue)
{
	Cast<ADedicatedServerTestCharacter>(this->GetControlledPawn())->MoveForward(AxisValue);
}

And removed this functionality from the Character cpp file.

then create a Blueprint extension of the game mode and set the character and the player controller defaults there. And set the blueprint extended game mode as the default.

Everything works when playing normally i.e locally but when I tick the ‘Dedicated Server’ the editor crashes at the ‘if’ statement in the Character in this part of the code. What caught my eye here was the in Visual Studio’s Locals windows said that ‘this’ has a value of 0x0000000000000000 .

void ADedicatedServerTestCharacter::MoveForward(float Value)
{
	if ((Controller != NULL) && (Value != 0.0f))
	{
		// find out which way is forward
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);

		// get forward vector
		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
		AddMovementInput(Direction, Value);
	}
}

I do not understand what I am doing wrong or what I am not doing at all

can someone explain why is this happening

I found out where the problem was. Well actually the problems were two.

First of all, when playing in dedicated server the playercontroller would not instantaneously have the reference to it’s character. So I added a check before that so everything executes only if there is a valid reference

The second thing was that I was using this->GetControlledPawn() which is deprecated I guess so I changed it to this->GetPawn())

The changes made are only in the player controller and the c++ file is changed to the following

ADedicatedServerTest_PC::ADedicatedServerTest_PC(const FObjectInitializer& ObjectInitializer) :Super(ObjectInitializer)
{

}

void ADedicatedServerTest_PC::SetupInputComponent()
{
	Super::SetupInputComponent();
	
	InputComponent->BindAxis("MoveForward", this, &ADedicatedServerTest_PC::MoveForward);
}

void ADedicatedServerTest_PC::MoveForward(float AxisValue)
{
	if (this->GetPawn() != nullptr)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, this->GetPawn()->GetName());
		Cast<ADedicatedServerTestCharacter>(this->GetPawn())->MoveForward(AxisValue);
	}
	else GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("the controlled pawn is null"));
		
}

I haven’t changed anything else and the problem is fixed

the GEngine statements are for debugging only and they are not required