【问题标题】:Errors in tutorial project from Unreal's documentation虚幻文档中的教程项目中的错误
【发布时间】:2019-08-31 11:35:07
【问题描述】:

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

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

我已经尝试了“可能的修复”并在网上四处查看,但没有找到任何可以修复错误的方法。

AMyPawn::AMyPawn() { // 将此 pawn 设置为每帧调用 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 'OurCamera':未声明的标识符

错误 C2672 'UObject::CreateDefaultSubobject':找不到匹配的重载函数

错误 C2974 'UObject::CreateDefaultSubobject':'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。请确认您有足够的权限运行此命令。

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

【问题讨论】:

  • "如果您是 Unreal Engine 4 的新手,您可能需要先阅读我们的编程快速入门教程。对于本教程,我们假设您熟悉创建项目、添加 C++代码,然后编译你的代码。”
  • 这看起来是一个相当明显的错误,您需要包含包含UCameraComponent 的文件。我什至可以说,如果没有一些先验的 c++ 知识或更好的经验,虚幻的开发将不会很有趣。看看The Definitive C++ Book Guide and List。还有视频教程和诸如 udemy 课程之类的东西,虽然我听说找到一个好的和完整的课程可能会很棘手(但这只是异端邪说,如果你喜欢视频学习,可能会有很多好处)。
  • 是的,我已经完成了他们的第一个教程。这是第二个。我还在学习基础知识,但是当教程提供的代码不起作用时,这很难。
  • @George 我认为这很明显,但本教程甚至不建议包含我尚未完成的任何内容。不幸的是,我真的没有时间或金钱来订购任何书籍:(
  • 是的,刚刚尝试了该代码并且可以确认它缺少包含。

标签: c++ unreal-engine4


【解决方案1】:

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

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

#include "Camera/CameraComponent.h"

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

#include "Components/StaticMeshComponent.h"

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

我同意本教程错过了这一点很糟糕,但是使用正确的工具很容易找到丢失的包含。在 Visual Studio 中,您可以在按住 Ctrl 的同时简单地单击任何类型(例如 UCameraComponent),它会直接进入声明,或者会向您显示具有潜在声明的文件列表。

在 Unreal 中,通常情况下,对于每个组件,包含文件都有该组件的名称,因此对于 UCameraComponent,即为 CameraComponent.h,它位于子文件夹 Camera 中。

【讨论】:

  • 你是绝对的传奇人物
  • 现在唯一的问题是我仍然得到“无法将“UStaticMeshComponent *”类型的值分配给“USceneComponent *”类型的实体。我是否通过将 USceneComponent(在 MyPawn.h 中)更改为 UStaticMeshComponent 来解决这个问题,反之亦然,还是忽略错误? (在窗口底部显示,代码中出现红线,但不在错误列表中)
  • 糟糕,你是对的。我也将 UStaticMeshComponent 添加到我的答案中。对不起!
【解决方案2】:

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

MyPawn.h

#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;

};

MyPawn.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 的我,没有花时间“正确”编写这段代码,所以如果有任何错误的风格/惯例,我很抱歉;)

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 1970-01-01
    • 2021-03-02
    • 2022-12-21
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    相关资源
    最近更新 更多