我目前正在尝试遵循此tutorial,以开始制作我的第一个游戏。除了使用我自己的属性名称外,我完全按照上述教程中的内容进行操作。我的问题是我的角色类中的这段代码没有被调用:
void AGetOutCharacter::BeginPlay()
{
Super::BeginPlay();
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Using GetOutCharacter"));
}
}
就像在本教程中一样,我有一个游戏模式类和一个从该类扩展的蓝图。该蓝图类在项目设置中被设置为默认游戏模式。该游戏模式使用从角色类扩展的角色类蓝图作为默认的典当类。同样,这是在项目设置中设置的,就像在教程中一样。
我可能只是迷失了头脑,但我无法终生弄清楚为什么在PIE中运行游戏时未显示调试日志。在我的游戏模式类中有一个与上面相同的日志,它在游戏运行时显示,但在角色类上却没有。我尝试将在游戏模式StartPlay()
和角色BeginPlay()
中都输出到输出日志控制台的日志语句放到游戏模式中,同样,游戏模式中的一个语句就像一个超级按钮,而角色类中的一个则不起作用。工作。我还尝试过在字符类中调用AddOnScreenDebugMessage(...)
函数的行上放置一个断点,并且从未达到该断点。我在同一位置的游戏模式类中放置了一个断点,但该断点被击中。原因告诉我,这意味着游戏使用我的课程的方式有些问题,但编辑器显示一切正常。
一个特别有趣且令人困惑的交互是,如下所示,没有设置用于控制玩家移动的映射。因此,当我将游戏模式和角色类别设置为默认值时(如游戏运行时所预期的那样),角色将无法移动。但是,当我将默认的pawn设置为stock pawn类时,这种无法移动的现象仍然存在。仅当我删除游戏模式类作为默认游戏模式时,我才能重新获得在运行游戏时在关卡中移动的能力。这表明正在使用字符类,但未调用其BeginPlay()
函数。
由于我已经解决了几个小时的问题,所以任何帮助将不胜感激。谢谢!
供参考,这里是游戏模式和角色类:
GameMode.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GetOutGameModeBase.h"
#include "GetOutGameMode.generated.h"
/**
*
*/
UCLASS()
class GETOUT_API AGetOutGameMode : public AGetOutGameModeBase
{
GENERATED_BODY()
public:
virtual void StartPlay() override;
};
GameMode.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "GetOutGameMode.h"
void AGetOutGameMode::StartPlay()
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Using GetOutGameMode"));
}
}
Character.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "GetOutCharacter.generated.h"
UCLASS()
class GETOUT_API AGetOutCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AGetOutCharacter();
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;
};
Character.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "GetOutCharacter.h"
// Sets default values
AGetOutCharacter::AGetOutCharacter()
{
// Set this character 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 AGetOutCharacter::BeginPlay()
{
Super::BeginPlay();
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Using GetOutCharacter"));
}
}
// Called every frame
void AGetOutCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AGetOutCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
另外,如果我在我的项目设置/蓝图中犯了一个错误,这是我的项目的项目设置和参考查看器图:
与朋友交谈后,我发现了问题的原因!
在游戏模式类中,我正在重写的StartPlay()
函数没有调用Super::StartPlay()
此外,解决了问题。