虚幻文档中教程项目中的错误

Errors in tutorial project from Unreal's documentation

本文关键字:错误 项目 教程 文档      更新时间:2023-10-16

我是第一次尝试学习C++/UE4,教程中提供的代码(在他们自己的文档中(抛出了错误。我怎样才能解决这个问题和/或找到一个有效的教程?

我正在尝试在 https://docs.unrealengine.com/en-US/Programming/Tutorials/PlayerInput/index.html 完成本教程,但步骤 1 中提供的代码抛出错误。

我已经尝试了"潜在修复"并在网上四处寻找,但没有找到任何可以修复错误的东西。

AMyPawn::AMyPawn(( { 设置此棋子以每帧调用 Tick((。 如果不需要,可以将其关闭以提高性能。 PrimaryActorTick.bCanEverTick = true;

// Set this pawn to be controlled by the lowest-numbered player
AutoPossessPlayer = EAutoReceiveInput::Player0;
// Create a dummy root component we can attach things to.
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
// Create a camera and a visible object
UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent"));
// Attach our camera and visible object to our root component. Offset and rotate the camera.
OurCamera->SetupAttachment(RootComponent);
OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
OurVisibleComponent->SetupAttachment(RootComponent);

}

错误 C2065"UCameraComponent":未声明的标识符

错误 C2065"我们的相机":未声明的标识符

错误 C2672"UObject::创建默认子对象":找不到匹配的重载函数

错误 C2974 "UObject::创建默认子对象":"TReturnType"的模板参数无效,类型应为

错误 MSB3075 命令"C:\Program Files\Epic Games\UE_4.22\Engine\Build\BatchFiles\Build.bat" SecondUnrealProjectEditor Win64 Development -Project="C:\Users...\SecondUnrealProject\SecondUnrealProject.uproject" -WaitMutex -FromMsBuild" 退出,代码为 5。请验证您是否有足够的权限来运行此命令。

我希望代码能够正常运行而不会出错,因为我完全按照教程进行操作(并且实际上已经复制粘贴了"工作"代码以检查我没有意外更改任何内容(

第一个错误意味着编译器找不到UCameraComponent的声明,其他错误只是后续错误。

您需要将以下内容添加到您的MyPawn.cpp

#include "Camera/CameraComponent.h"

修复此问题后,您将收到另一个错误,这次是因为缺少UStaticMeshComponent的声明。为此,您需要以下内容包括:

#include "Components/StaticMeshComponent.h"

错误消息是不同的,因为在第一种情况下,编译器想要实例化未知类型的对象,这是不可能的,而在后一种情况下,编译器想要将未知类型的指针UStaticMeshComponent分配给另一个已知类型的指针USceneComponent,并且它不知道UStaticMeshComponent可以强制转换为USceneComponent

我同意教程错过了这一点很糟糕,但是使用正确的工具查找缺少的包含相当容易。在Visual Studio中,您只需单击任何类型(例如UCameraComponent(,同时按住Ctrl,它将直接转到声明,或者它将显示具有潜在声明的文件列表。

在虚幻引擎中,通常每个组件的包含文件都具有该组件的名称,因此对于UCameraComponentCameraComponent.h,它驻留在子文件夹Camera中。

我也遇到了这个问题。 我已经完成了教程并进行了一些修改。 视觉工作室 2019 14.24.28315 UE4 4.24

我的典当

#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/Character.h"
#include "MyPawn.generated.h"
UCLASS()
class HOWTO_PLAYERINPUT_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyPawn();
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;
UPROPERTY(EditAnywhere)
USceneComponent* OurVisibleComponent;
//Input functions
void Move_XAxis(float AxisValue);
void Move_YAxis(float AxisValue);
void StartGrowing();
void StopGrowing();
//Input variables
FVector CurrentVelocity;
bool bGrowing;
};

我的典当.cpp

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

#include "MyPawn.h"
#include "Camera/CameraComponent.h"
#include "Components/StaticMeshComponent.h"
// Sets default values
AMyPawn::AMyPawn()
{
// 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;
// Set this pawn to be controlled by the lowest-numbered player
AutoPossessPlayer = EAutoReceiveInput::Player0;
// Create a dummy root component we can attach things to.
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
// Create a camera and a visible object
UPROPERTY(EditAnywhere)
UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent"));
// Attach our camera and visible object to our root component. Offset and rotate the camera.
OurCamera->SetupAttachment(RootComponent);
OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
OurVisibleComponent->SetupAttachment(OurCamera);
OurVisibleComponent->SetRelativeLocation(FVector(268.0f, 0.0f, -14.14f));
OurVisibleComponent->SetRelativeRotation(FRotator(45.0f, 0.0f, 0.0f));

}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// Handle growing and shrinking based on our "Grow" action
float CurrentScale = OurVisibleComponent->GetComponentScale().X;
if (bGrowing)
{
// Grow to double size over the course of one second
CurrentScale += DeltaTime;
}
else
{
// Shrink half as fast as we grow
CurrentScale -= (DeltaTime * 0.5f);
}
// Make sure we never drop below our starting size, or increase past double size.
CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));

// Handle movement based on our "MoveX" and "MoveY" axes
if (!CurrentVelocity.IsZero())
{
FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
SetActorLocation(NewLocation);
}

}
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// Respond when our "Grow" key is pressed or released.
PlayerInputComponent->BindAction("Grow", IE_Pressed, this, &AMyPawn::StartGrowing);
PlayerInputComponent->BindAction("Grow", IE_Released, this, &AMyPawn::StopGrowing);
// Respond every frame to the values of our two movement axes, "MoveX" and "MoveY".
PlayerInputComponent->BindAxis("MoveX", this, &AMyPawn::Move_XAxis);
PlayerInputComponent->BindAxis("MoveY", this, &AMyPawn::Move_YAxis);
}
void AMyPawn::Move_XAxis(float AxisValue)
{
// Move at 100 units per second forward or backward
CurrentVelocity.X = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}
void AMyPawn::Move_YAxis(float AxisValue)
{
// Move at 100 units per second right or left
CurrentVelocity.Y = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}
void AMyPawn::StartGrowing()
{
bGrowing = true;
}
void AMyPawn::StopGrowing()
{
bGrowing = false;
}

我只是觉得每次UE4更新时,基本上每个教程都会下地狱,这很疯狂。 *注意:我在完成本教程时摸索着。我是UE4的meh,没有花时间"正确"编写此代码,所以如果风格/惯例方面有任何错误,我很抱歉;)

相关文章: