Toon Tank (v2) Game Help

Never able to get any PlayerController in Beginplay

Like PlayerController = Cast(GetController()) ;

But it is working fine in Tick.

The same goes for game mode too.

ToonTanksPlayerController = Cast(UGameplayStatics::GetPlayerController(this,0));

Can you post your code please, I am confused by the phrasing of your message?

// Fill out your copyright notice in the Description page of Project Settings.


#include "Tank.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Kismet/GameplayStatics.h"

ATank::ATank()
{
	SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring Arm"));
	Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
	SpringArm->SetupAttachment(RootComponent);
	Camera->SetupAttachment(SpringArm);
}

// Called when the game starts or when spawned
void ATank::BeginPlay()
{
	Super::BeginPlay();
}


// Called every frame
void ATank::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	PlayerController = Cast<APlayerController>(GetController()) ;
	if(PlayerController)
	{
		FHitResult HitResult;
		PlayerController->GetHitResultUnderCursor(ECollisionChannel::ECC_Visibility,false,HitResult);
		RotateTurret(HitResult.ImpactPoint);
		DrawDebugSphere(GetWorld(),HitResult.ImpactPoint,25.f,12,FColor::Red,false,-1.0f);
	}
}


void ATank::Move( float amount)
{
	FVector DeltaLocation = FVector::ZeroVector;
	DeltaLocation.X = amount * Speed * UGameplayStatics::GetWorldDeltaSeconds(this);
	AddActorLocalOffset(DeltaLocation,true);
}


void ATank::Rotate(float amount)
{
	FRotator DeltaRotation = FRotator::ZeroRotator;
	DeltaRotation.Yaw = amount *  Rotation * UGameplayStatics::GetWorldDeltaSeconds(this);
	AddActorLocalRotation(DeltaRotation,true);
}


void ATank::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	PlayerInputComponent->BindAxis(TEXT("MoveForward"),this,&ATank::Move);
	PlayerInputComponent->BindAxis(TEXT("Turn"),this,&ATank::Rotate);
	PlayerInputComponent->BindAction(TEXT("Fire"),IE_Pressed,this,&ATank::Fire);
}

void ATank::HandleDestruction()
{

	Super::HandleDestruction();
	SetActorHiddenInGame(true);
	SetActorTickEnabled(false);
}


void ATank::Fire()
{
	Super::Fire();
}

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "BasePawn.h"
#include "Tank.generated.h"

class UCameraComponent;
class USpringArmComponent;
/**
 * 
 */
UCLASS()
class TOONTANKS_API ATank : public ABasePawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	ATank();
	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	void HandleDestruction();

	APlayerController* GetTankPlayerController() const { return PlayerController ; }
	

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	//Call when fire pressed
	virtual void Fire() override;


private:
	UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Components",meta=(AllowPrivateAccess="true"))
	USpringArmComponent * SpringArm;
	
	UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Components",meta=(AllowPrivateAccess="true"))
	UCameraComponent * Camera;

	UPROPERTY(EditAnywhere,Category="Movement")
	float Speed;
	UPROPERTY(EditAnywhere,Category="Movement")
	float Rotation;

	UPROPERTY()
	APlayerController* PlayerController;

	
	void Move(float amount);
	void Rotate(float amount);
	
};

// Called when the game starts or when spawned
void ATank::BeginPlay()
{
	Super::BeginPlay();
       //Never work always null .
       PlayerController = Cast<APlayerController>(GetController()) ;
}

@DanM Could you respond to this student please.

Thanks

Could you show BP_TankPlayerController please?

// Fill out your copyright notice in the Description page of Project Settings.


#include "ToonTanksPlayerController.h"

void AToonTanksPlayerController::SetPlayerEnableState(bool bPlayerEnabled)
{
	if(bPlayerEnabled)
	{
		GetPawn()->EnableInput(this);
		
	}
	else
	{
		GetPawn()->DisableInput(this);
	}
	bShowMouseCursor = bPlayerEnabled;
}
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "ToonTanksPlayerController.generated.h"

/**
 * 
 */
UCLASS()
class TOONTANKS_API AToonTanksPlayerController : public APlayerController
{
	GENERATED_BODY()

public:
	void SetPlayerEnableState(bool bPlayerEnabled);
	
};

@DanM Please download the project from this link

How are you determining that it’s null? Seems to be fine on my end

image

check the log please @DanM

Oh in the game mode, your earlier post said tank

I’ll check it in a bit when I’m back at the PC.

Thanks

Works fine for me

I suggest you perform a rebuild.

No luck same old. Is there any configuration I am missing @DanM ?

I have rebuild it from the editor . Same thing.

Is there any possibility to clear the doubt over a call . Else I just have to scrap this. I can not move forward .

Thanks

Just so we’re on the same page. By “rebuild” I didn’t mean “build again”, I mean use the rebuild task/project i.e.

in VS Code use the task:
ToonTanksEditor Win64 Development REbuild

In VS:
Build > REbuild ToonTanks

As buiding only compiles files that have changed from the last build and reuses previous compiled object files for ones that haven’t whereas rebuild first deletes everything and compiles everything from scratch.


If you did do that have you tried ensuring it regenerated/recompiled the code by deleting the Binaries and Intermediuate folders?

@DanM I have rebuilt in the same way but from the rider, the result is the same.

Even deleting the binaries and intermediate?

If you unzip the file you sent into a different folder, do you have issues with that one?

Privacy & Terms