前言
初学UE4和C++,在尝试将蓝图函数转为C++代码时候,发现了一个很坑人的玩意,就是蓝图中的构造脚本(Consttruction Script)和C++中类的构造函数不是一个东西!因此想记录下来。
准备工作
先创建一个继承Actor的类,现在我有个现成的继承Pawn的类(下文就直接说Actor),可以凑合着用。
CameraOnPlane.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "CameraOnPlane.generated.h"
DECLARE_LOG_CATEGORY_EXTERN(TestLog, Log, All);
UCLASS()
class TRAJECTORY_API ACameraOnPlane : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
ACameraOnPlane();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
virtual void OnConstruction(const FTransform& Transform) override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
CameraOnPlane.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "CameraOnPlane.h"
DEFINE_LOG_CATEGORY(TestLog);
// Sets default values
ACameraOnPlane::ACameraOnPlane()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance i