UE4 C++初探

UE4 C++ 初探


声明:笔者引擎版本为 Unreal Engine 4.16.1

从打印 Hello World 和打印基本变量开始:

工程的建立和基本代码的编写就不再赘述了,网上有很多,我看的是这个:
http://blog.csdn.net/u011326794/article/details/47706959

可以在新建的AActor中的Tick或者BeginPlay中使用 GEngine 的 AddOnScreenDebugMessage 进行输出,GEngine 是 UEngine 类型的一个全局指针,被声明在 Engine.h 中,源代码如下:

Engine.h

/** Global engine pointer. Can be 0 so don't use without checking. */
extern ENGINE_API class UEngine*			GEngine;

在 Engine.cpp 中对 GEngine 进行了初始化:

Engine.cpp

/**
 * Global engine pointer. Can be 0 so don't use without checking.
 */
ENGINE_API UEngine*	GEngine = NULL;
  • UE4 在屏幕上打印文字的函数原型如下:
void UEngine::AddOnScreenDebugMessage(
		uint64 Key,
		float TimeToDisplay,    // 消息在屏幕上显示的时间
		FColor DisplayColor,    // 文字的颜色
		const FString & DebugMessage,    // 显示内容
		bool bNewrOnTop,
		const FVector2D & TextScale
)

void UEngine::AddOnScreenDebugMessage(
		int32 Key,
		float TimeToDisplay,    // 消息在屏幕上显示的时间
		FColor DisplayColor,    // 文字的颜色
		const FString & DebugMessage,    // 显示内容
		bool bNewrOnTop,
		const FVector2D & TextScale
)

官方文档的例子在这里:

void AHelloWorldPrinter::Tick(float DeltaTime)  
{  
    Super::Tick(DeltaTime); //Call parent class Tick  

    static const FString ScrollingMessage(TEXT("Hello World: "));  

    if (GEngine)  
    {  
        const int32 AlwaysAddKey = -1; // Passing -1 means that we will not try and overwrite an   
                                       // existing message, just add a new one  
        GEngine->AddOnScreenDebugMessage(AlwaysAddKey, 0.5f, FColor::Yellow, ScrollingMessage + FString::FromInt(MyNumber));  

        const int32 MyNumberKey = 0; // Not passing -1 so each time through we will update the existing message instead  
                                     // of making a new one  
        GEngine->AddOnScreenDebugMessage(MyNumberKey, 5.f, FColor::Yellow, FString::FromInt(MyNumber));  

        ++MyNumber; // Increase MyNumber so we can see it change on screen  
    }  
}
  • 贴出一个Demo:
// Called every frame
void AFloatingActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	
	FVector NewLocation = GetActorLocation();
	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 8.f, FColor::Blue, TEXT("Hello World !"));
		GEngine->AddOnScreenDebugMessage(0, 3.f, FColor::Red, *NewLocation.ToString());
	}
}
  • 代码说明:

    • Tick 在类创建的时候由编辑器自动创建
    • GetActorLocation() 用来获取 Actor 当前的 Location,返回的是一个 FVector 结构体类型
    • 使用FVector::ToString()将 NewLocation 由 FVector 转为 FString
  • 笔者的代码执行结果如下:
    笔者的代码执行结果

  • 补充:

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值