Hi, I’m playing around with ue4 to gain experience with it, and my first code with c++ failed and crashed it…
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPlayer.h"
// Sets default values
AMyPlayer::AMyPlayer()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AMyPlayer::BeginPlay()
{
Super::BeginPlay();
RootComp = CreateDefaultSubobject<USceneComponent>(TEXT("RootComp"));
PlayerCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("PlayerCamera"));
PlayerCamera->SetupAttachment(RootComp);
PlayerCamera->SetRelativeLocationAndRotation(FVector(0.f, 0.f, 50.f), FRotator(-60.f, 0.f, 0.f));
}
// Called every frame
void AMyPlayer::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AMyPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &AMyPlayer::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AMyPlayer::MoveRight);
}
void AMyPlayer::MoveForward(float value)
{
if (Controller && value)
{
AddMovementInput(GetActorForwardVector(), value);
}
}
void AMyPlayer::MoveRight(float value)
{
if (Controller && value)
{
AddMovementInput(GetActorRightVector(), value);
}
}
And my header:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/Pawn.h"
#include "MyPlayer.generated.h"
UCLASS()
class MYTEST_API AMyPlayer : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPlayer();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
void MoveForward(float value);
void MoveRight(float value);
UCameraComponent* PlayerCamera;
USceneComponent* RootComp;
};
I did c++ in the past, with advanced things, but I here I dont know how I can do this