创建一个名为ConsoleLog的新角色。 (修正)#include "Engine/Engine.h" 我们不必在头文件中做任何事情,但是下面是创建新角色时应显示的默认代码。我给演员打电话ConsoleLog,如果您选择其他名称,请确保在必要时进行更改。 ConsoleLog.h#pragma once#include "CoreMinimal.h"#include "GameFramework/Actor.h"#include "ConsoleLog.generated.h"UCLASS()class UNREALCPP_API AConsoleLog : public AActor{ GENERATED_BODY() public: // Sets default values for this actor's properties AConsoleLog();protected: // Called when the game starts or when spawned virtual void BeginPlay() override;public: // Called every frame virtual void Tick(float DeltaTime) override; };
.cpp文件是我们将注销消息的位置。对于此示例,我们将注销消息中的BeginPlay消息。因此,当游戏开始时,消息将打印出来。下面是三种记录消息的方法。 登录控制台UE_LOG(LogTemp, Warning, TEXT("I just started running"));
打印到屏幕GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Screen Message"));
打印矢量GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Orange, FString::Printf(TEXT("My Location is: %s"), *GetActorLocation().ToString()));
下面是完整的.cpp文件。前两个define调用是使记录消息更容易的快捷方式。 ConsoleLog.cpp// define a print message function to print to screen#define print(text) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Green,text)#define printFString(text, fstring) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Magenta, FString: rintf(TEXT(text), fstring))#include "ConsoleLog.h"// Sets default valuesAConsoleLog::AConsoleLog(){ // Set this actor 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 spawnedvoid AConsoleLog::BeginPlay(){ Super::BeginPlay(); // Standard way to log to console. UE_LOG(LogTemp, Warning, TEXT("I just started running")); // Log to Screen GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Screen Message")); FVector MyVector = FVector(200,100,900); // log vector GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Orange, FString::Printf(TEXT("My Location is: %s"), *GetActorLocation().ToString())); // Use the shortcut defined above print("Hello Unreal"); printFString("My Variable Vector is: %s", *MyVector.ToString()); }// Called every framevoid AConsoleLog::Tick(float DeltaTime){ Super::Tick(DeltaTime);}
|