Skip to content

Commit b5209b3

Browse files
committed
打印调式信息
1 parent 99db991 commit b5209b3

File tree

4 files changed

+118
-3
lines changed

4 files changed

+118
-3
lines changed

docs/img/log_screen.jpg

597 KB
Loading

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ title: 主页
2525

2626
[__3.添加静态网格文件__](./tutorial/add_mesh_from_file.md)
2727

28-
# [__4.添加一个广告牌组件__](./tutorial/add_billboard.md)
28+
[__4.添加一个广告牌组件__](./tutorial/add_billboard.md)
2929

3030
[__5. 打印调式信息__](tutorial/print_debug.md) — 向命令行、屏幕打印开发过程中的调式信息
3131

docs/tutorial/print_debug.md

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
新创建一个名为 ConsoleLog 的新 Actor 子类(不需要在头文件中执行任何操作)。
44

5-
**ConsoleLog.h**
5+
<details>
6+
<summary><b>AddBillboardComp.h</b> </summary>
7+
68
```C++
79
#pragma once
810
#include "CoreMinimal.h"
@@ -23,9 +25,86 @@ class UNREALCPP_API AConsoleLog : public AActor
2325
virtual void Tick(float DeltaTime) override;
2426
};
2527
```
28+
</details>
29+
<br>
30+
31+
32+
接着我们将在 .cpp 文件中打印日志消息。对于这个例子,我们将在 BeginPlay 方法中打印该消息。所以,当游戏开始时,消息将打印出来。
33+
34+
下面是打印消息的 3 种方法。
35+
36+
* 打印到控制台
37+
* 打印到屏幕:[AddOnScreenDebugMessage()](https://github.com/OpenHUTB/engine/blob/1413799f28af699ad0e6139ea25853b66c719c7d/Engine/Source/Runtime/Engine/Private/UnrealEngine.cpp#L9458)
38+
* 打印的数据格式为向量
39+
40+
41+
分别如下所示:
42+
```C++
43+
UE_LOG(LogTemp, Warning, TEXT("I just started running"));
44+
```
45+
46+
```
47+
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Screen Message"));
48+
```
49+
50+
```
51+
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Orange, FString::Printf(TEXT("My Location is: %s"), *GetActorLocation().ToString()));
52+
```
53+
54+
下面是完整的 C++ 文件(使用宏定义可以简化打印调试信息的步骤)
55+
56+
<details>
57+
<summary><b>AddBillboardComp.cpp</b> </summary>
58+
59+
```cpp
60+
// Copyright (c) 2025 OpenHUTB at the Human University of Technology and Business (HUTB). This work is licensed under the terms of the MIT license. For a copy, see <https://opensource.org/licenses/MIT>.
61+
62+
#define print(text) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, text)
63+
#define printFString(text, fstring) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Magenta, FString::Printf(TEXT(text), fstring))
64+
65+
#include "Tutorial/ConsoleLog.h"
66+
67+
// Sets default values
68+
AConsoleLog::AConsoleLog()
69+
{
70+
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
71+
PrimaryActorTick.bCanEverTick = true;
72+
73+
}
74+
75+
// Called when the game starts or when spawned
76+
void AConsoleLog::BeginPlay()
77+
{
78+
Super::BeginPlay();
79+
// 标注的方式:输出到控制台
80+
UE_LOG(LogTemp, Warning, TEXT("I just started running"));
81+
// 记录到屏幕上
82+
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Screen Message"));
83+
FVector MyVector = FVector(200, 100, 900);
84+
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Orange, FString::Printf(TEXT("My Location is: %s"), *GetActorLocation().ToString()));
85+
print("Hello Engine");
86+
printFString("My Variable Vector is: %s", *MyVector.ToString());
87+
}
88+
89+
// Called every frame
90+
void AConsoleLog::Tick(float DeltaTime)
91+
{
92+
Super::Tick(DeltaTime);
93+
94+
}
95+
```
96+
</details>
97+
<br>
98+
99+
执行的结果如下所示(先打印的在下,后打印的在上):
100+
101+
![](../img/log_temp.jpg)
26102

27103

104+
![](../img/log_screen.jpg)
28105

29106

30107

31-
为了避免每次需要重启引擎编辑器使修改的代码生效,可以参考[热重载](./hot_reload.md)
108+
!!! 笔记
109+
1.为了避免每次需要重启引擎编辑器使修改的代码生效,可以参考[热重载](./hot_reload.md)
110+
2.引擎编辑器界面输出日志默认是没打开,打开方法:窗口->开发者工具->输出日志。

src/tutorial/ConsoleLog.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2025 OpenHUTB at the Human University of Technology and Business (HUTB). This work is licensed under the terms of the MIT license. For a copy, see <https://opensource.org/licenses/MIT>.
2+
3+
#define print(text) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, text)
4+
#define printFString(text, fstring) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Magenta, FString::Printf(TEXT(text), fstring))
5+
6+
#include "Tutorial/ConsoleLog.h"
7+
8+
// Sets default values
9+
AConsoleLog::AConsoleLog()
10+
{
11+
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
12+
PrimaryActorTick.bCanEverTick = true;
13+
14+
}
15+
16+
// Called when the game starts or when spawned
17+
void AConsoleLog::BeginPlay()
18+
{
19+
Super::BeginPlay();
20+
// 标注的方式:输出到控制台
21+
UE_LOG(LogTemp, Warning, TEXT("I just started running"));
22+
// 记录到屏幕上
23+
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Screen Message"));
24+
FVector MyVector = FVector(200, 100, 900);
25+
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Orange, FString::Printf(TEXT("My Location is: %s"), *GetActorLocation().ToString()));
26+
print("Hello Engine");
27+
printFString("My Variable Vector is: %s", *MyVector.ToString());
28+
}
29+
30+
// Called every frame
31+
void AConsoleLog::Tick(float DeltaTime)
32+
{
33+
Super::Tick(DeltaTime);
34+
35+
}
36+

0 commit comments

Comments
 (0)