当前位置: 首页 > article >正文

ue学习part2

此为Unreal Engine 5 C++ The Ultimate Game Developer Course第5节 The Actor Class的学习记录

第5节 The Actor class

construction script是在游戏运行前就运行的脚本。而event graph是游戏运行开始时运行的

在开发世界中,如果主控在远离蓝图物体的地方,那么该蓝图物体不会运行直到主控靠近蓝图物体

ctrl+ shift + space展示tooltips ;_VA_ARGS_ 代表可接受可变参数



c++编译除了在visual studio上进行编译,在ue上编译会比较快.。需要注意的是live code能够快速hot reload and recompile,但是当ue关闭重启之后,需要再次点击该按钮进行hot reload否则可能看不到改变结果。注意:在头文件有进行改变时或者有重实例化时(reinstance),不要使用live coding(因为可能会导致一些结构损坏)

 课程原文:

So why should we not use live coding if it's a feature built into the engine? 那么,如果实时编码是引擎中内置的功能,为什么我们不应该使用它呢?

Well, what it does is it patches running native code which is never reliable. 嗯,它的作用是修补运行本机代码,这些代码从来都不可靠。

It really only works reliably for function body changes for anything else like header changes. 它实际上只对函数体更改(如 Headers 更改)可靠工作。

For example, you should close the editor and compile from the IDE, attempting to modify a header file 例如,您应该关闭编辑器并从 IDE 进行编译,尝试修改头文件

with a reflected type. 替换为反射类型。

For instance, something with a U property will result in Unreal Engine trying to re instance any 例如,具有 u 属性的内容将导致 Unreal Engine 尝试重新实例化任何

of the objects of that type, which is a process that happens to corrupt your blueprints regularly. 中,这是一个经常损坏 Blueprint 的过程。

Re instancing is what it sounds like, creating a new instance of objects of that type, and this is Re instancing 顾名思义,创建该类型对象的新实例,这是

a setting that can actually be disabled in the engine. 实际上可以在 Engine 中禁用的设置。

We can turn off re instancing altogether, which is in general a good measure. 我们可以完全关闭重新实例化,这通常是一个很好的措施。

建议工作流程:使用visualstudio就只需要修改或者新建文件后build module(或者build solutin)之后 按ctrl + f5重启ue(没有进入ue之后需要再次live code的要求)

建议关闭enable reinstancing

在visual studio2022以上版本,可以在ue关闭的情况下,直接在visual studio新建c++类(2022及以下版本需要在ue中新建c++类)

课程原文:

One common misconception is that some students think that you need to regenerate your project files 一个常见的误解是,一些学生认为您需要重新生成项目文件

every time you compile or add a new class, and this is not true. 每次编译或添加新类时,这不是真的。

Notice that I just waited a few moments and my red squiggles went away, so I did not need to regenerate 请注意,我只是等待了片刻,我的红色波浪线就消失了,所以我不需要重新生成

my project files. 我的项目文件。

Regenerating your project files is only something you have to do when you have cached or corrupted files 重新生成工程文件只是在缓存或损坏文件时才需要执行的作

in your project. 在您的项目中。
-----------------------------------
And remember, you don't have to regenerate your project files. 请记住,您不必重新生成项目文件。

And what I mean by that is deleting intermediate and binaries and right clicking on your new project 我的意思是删除中间项和二进制文件,然后右键单击您的新项目

and generating Visual Studio Project files. 以及生成 Visual Studio 项目文件。

So generating your project files is not necessary when creating a new Cplusplus class or compiling your 因此,在创建新的 Cplusplus 类或编译

code changes. 代码更改。

So that's my preferred workflow. 所以这是我首选的工作流程。


 修改virtual texture支持

 log string函数输出日志在output log;而print string输出字符在screen and output log

print string的key值可以让新出现的消息顶替原本的消息,防止每帧调用tick函数时出现spamming(日志刷屏)的状况

void AItem::BeginPlay()
{
	Super::BeginPlay();

	UE_LOG(LogTemp, Warning, TEXT("begin play called"));

	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(1, 60.f, FColor::Cyan, FString("Item onscreen"));
	}
}


AItem::AItem()
{
 	// 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 spawned
void AItem::BeginPlay()
{
	Super::BeginPlay();

	UE_LOG(LogTemp, Warning, TEXT("begin play called"));

	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(1, 60.f, FColor::Cyan, FString("Item onscreen"));
	}
}

// Called every frame
void AItem::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	UE_LOG(LogTemp, Warning, TEXT("DeltaTime: %f"), DeltaTime);
	if (GEngine)
	{
		FString name = GetName();
		// FString message = FString::Printf(TEXT("DeltaTime1: %f"), DeltaTime);
		//*被重载了
		FString message = FString::Printf(TEXT("Item Name: %s"), *name);
		GEngine->AddOnScreenDebugMessage(1, 60.f, FColor::Cyan, message);
	}
}


draw debug sphere有助于调试看不见的物体位置

#include "Items/Item.h"
#include "DrawDebugHelpers.h"

#define DRAW_SPHERE(Location) if (GetWorld()) DrawDebugSphere(GetWorld(), Location,  25.f, 24, FColor::Red, false, 30);

// Sets default values
AItem::AItem()
{
 	// 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 spawned
void AItem::BeginPlay()
{
	Super::BeginPlay();

	UE_LOG(LogTemp, Warning, TEXT("begin play called"));

	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(1, 60.f, FColor::Cyan, FString("Item onscreen"));
	}

	//UWorld* World = GetWorld();
	//if (World)
	//{
	//	FVector Location = GetActorLocation();
	//	DrawDebugSphere(World, Location, 25.f, 24, FColor::Red, false, 30);
	//}
	FVector Location = GetActorLocation();
	DRAW_SPHERE(Location)
	
}

如果想要宏定义能在其他类也被使用,就将它放在工程文件的头文件里面,在要使用该宏的类中include工程文件的头文件(在此例中是放在MyProject.h中)

draw debug line

void AItem::BeginPlay()
{
	Super::BeginPlay();

	UWorld* World = GetWorld();
	FVector Location = GetActorLocation();
	if (World)
	{
		FVector Forward = GetActorForwardVector();
		DrawDebugLine(World, Location,Location + Forward * 100.f, FColor::Red, true, -1.f, 0, 2.f);
	}
	
	DRAW_SPHERE(Location)
	
}
------------------------------
使用宏定义之后
#define DRAW_LINE(StartLocation, EndLocation) if (GetWorld()) DrawDebugLine(World, StartLocation,EndLocation, FColor::Red, true, -1.f, 0, 2.f); 

// Called when the game starts or when spawned
void AItem::BeginPlay()
{
	Super::BeginPlay();

	UWorld* World = GetWorld();
	FVector Location = GetActorLocation();
	FVector Forward = GetActorForwardVector();
	
	DRAW_SPHERE(Location)
	DRAW_LINE(Location, Location + Forward * 100.f)
	
}

 draw debug point 

void AItem::BeginPlay()
{
	Super::BeginPlay();

	UWorld* World = GetWorld();
	FVector Location = GetActorLocation();
	FVector Forward = GetActorForwardVector();
	
	DRAW_SPHERE(Location)
	DRAW_LINE(Location, Location + Forward * 100.f)
	if(World) 
	{
		DrawDebugPoint(World, Location + Forward * 100.f, 15.f, FColor::Red, true);
	}
	
}
-----------------
使用宏定义
#define DRAW_POINT(Location) if (GetWorld()) DrawDebugPoint(GetWorld(), Location, 15.f, FColor::Red, true);

void AItem::BeginPlay()
{
	Super::BeginPlay();

	UWorld* World = GetWorld();
	FVector Location = GetActorLocation();
	FVector Forward = GetActorForwardVector();
	
	DRAW_SPHERE(Location)
	DRAW_LINE(Location, Location + Forward * 100.f)
	DRAW_POINT(Location + Forward * 100.f)
	
}

 多行宏定义

#define DRAW_VECTOR(StartLocation, EndLocation) if(GetWorld()) \
	{\
		DrawDebugLine(GetWorld(), StartLocation, EndLocation, FColor::Red, true, -1.f, 0, 2.f);\
		DrawDebugPoint(GetWorld(), EndLocation, 15.f, FColor::Red, true);\
	}


http://www.kler.cn/a/561892.html

相关文章:

  • LeetCode每日精进:622.设计循环队列
  • C#快速调用DeepSeek接口,winform接入DeepSeek查询资料 C#零门槛接入DeepSeek C#接入DeepSeek源代码下载
  • K8S能部署大数据集群吗?为什么?K8S的HPA功能可以作为大数据处理消息积压的问题(Kafka的分区)
  • R 语言科研绘图 --- 柱状图-汇总
  • react18自定义hook实现
  • NLP工程师逐步切入机器人和具身智能方向
  • Go语言--语法基础2--下载安装
  • PHP入门基础学习四(PHP基本语法)
  • Linux 之 Centos 安装Consul
  • redis小记
  • 从单片机的启动说起一个单片机到点灯发生了什么下——使用GPIO点一个灯
  • 低延迟,高互动:EasyRTC的全场景实时通信解决方案
  • Imagination通过最新的D系列GPU IP将效率提升至新高度
  • 分享些常用的工具类
  • 面试基础----ReentrantLock vs Synchronized
  • 大语言模型学习路径与开源模型推荐
  • 基于SSM的《计算机网络》题库管理系统(源码+lw+部署文档+讲解),源码可白嫖!
  • mysql逻辑备份 mysqldump和mydumper实践
  • java项目之图书管理系统设计与实现(源码+文档)
  • 为AI聊天工具添加一个知识系统 之122 详细设计之63 实体范畴论和神经元元模型:命名法函子