UE4 C++初探
2017-08-08 11:47
603 查看
UE4 C++ 初探
声明:笔者引擎版本为 Unreal Engine 4.16.1从打印 Hello World 和打印基本变量开始:
工程的建立和基本代码的编写就不再赘述了,网
4000
上有很多,我看的是这个:
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
笔者的代码执行结果如下:
补充:
FString 的
FString::SanitizeFloat(double Infloat)将 double 类型转为 FString 类型,FString::SanitizeFloat()
相关文章推荐
- C++ UE4属性
- C++初探:c和c++的区别简述,以及编译器对代码做了什么?
- Introduction to C++ Programming in UE4——UE4官方文档翻译与理解(二)
- UE4新手编程之创建C++项目
- UE4 C++ 定时器
- 测测试代码高亮:Windiff 原理初探(C++源码)
- C++ 多态性机制初探
- UE4 C++调用蓝图方法
- Learning C++ by Creating Games With UE4(15.05.08)-3(Chapter 1)
- ue4-C++中加载一个蓝图类(二)-C++中绑定Blueprint武器
- Learning C++ by Creating Games With UE4(书籍)
- UE4新手编程之创建C++项目
- C++面向对象模型初探
- UE4材质初探(转载)
- ue4教程文档翻译 C++ 射击游戏
- UE4材质初探
- Andriod C++游戏开发初探
- UE4 C++控制在UMG控件中播放视频
- 【UE4】【C++】生成子弹、炮弹 (左键射出、抛物线)
- C++学习笔记——类的初探