星鸿阁

 找回密码
 立即注册
搜索
热搜: 活动 交友 动画
查看: 1367|回复: 1

C++打印函数

[复制链接]

2254

主题

2764

帖子

9644

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
9644
发表于 2021-3-22 18:22:22 | 显示全部楼层 |阅读模式


创建一个名为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);}

回复

使用道具 举报

2254

主题

2764

帖子

9644

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
9644
 楼主| 发表于 2021-3-22 18:22:47 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|starfluidga

GMT+8, 2025-3-10 04:10 , Processed in 0.018636 second(s), 20 queries .

Made by Liga 星鸿阁

Copyright © 2020-2048, LigaStudio.

快速回复 返回顶部 返回列表