我第一次尝试学习C++/UE4,教程中提供的代码(在他们自己的文档中)会抛出错误。我如何解决这个问题和/或找到一个有效的教程?
我试图在https://docs.unrealengine.com/en-US/Programming/Tutorials/PlayerInput/index.html上完成本教程,但是步骤1中提供的代码会引发错误。
我已经尝试过“潜在的修复”,并在网上环顾四周,但没有发现任何可以修复错误的地方。
AMyPawn::AMyPawn() { //将此典当设置为调用Tick()每个帧。如果你不需要的话,你可以关掉它来提高性能。PrimaryActorTick.bCanEverTick =真;
// 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”的无效模板参数,类型预期
错误命令“C:\Program\Epic Games\UE_4.22\Engine\Build\BatchFiles\Build.bat”SecondUnrealProjectEditor Win64 Win64 Development -WaitMutex -FromMsBuild“与代码5一起退出。请验证您是否有足够的权限运行此命令。
我期望代码运行时不会出现错误,因为我完全遵循了本教程(事实上,我已经复制粘贴了“工作”代码,以检查我没有意外地更改任何内容)。
发布于 2019-08-31 12:02:39
第一个错误意味着编译器找不到UCameraComponent的声明,其他错误只是后续错误。
您需要在MyPawn.cpp中添加以下内容
#include "Camera/CameraComponent.h"修复后,您将得到另一个错误,这一次,因为UStaticMeshComponent的声明丢失了。为此,您需要以下内容:
#include "Components/StaticMeshComponent.h"错误消息是不同的,因为在第一种情况下,编译器希望实例化一个未知类型的对象,这是不可能的,而在后一种情况下,编译器希望将未知类型的UStaticMeshComponent指针分配给另一个已知类型USceneComponent的指针,而且它不知道UStaticMeshComponent可以转换为USceneComponent。
我同意教程忽略了这一点是很糟糕的,但是用正确的工具找一个缺失的包含是相当容易的。在Visual中,您只需在保存UCameraComponent时单击任何类型(例如,Ctrl ),它将直接转到声明中,或者它将向您显示具有潜在声明的文件列表。
在“虚幻”中,通常情况是,对于每个组件,包含文件都有该组件的名称,因此对于UCameraComponent,它是CameraComponent.h,它驻留在子文件夹Camera中。
发布于 2020-01-30 16:01:50
我也遇到过这个问题。我已经完成了一些修改的教程。视频工作室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工作,没有花时间“正确”地写这段代码,所以如果有什么错误的风格/惯例,我很抱歉;)
https://stackoverflow.com/questions/57737673
复制相似问题