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

UE5 C++ UGameInstance 功能、作用及应用

# UE5 C++ UGameInstance  功能及作用
 


网上有很多文章介绍,例如在游戏中只有一个实例,换关卡不会丢失等。暂时省略。

#  UE5 C++ UGameInstance  应用


## 应用一,UE5 C++ UGameInstance  里监听player创建事件


UWebSocketGameInstance.h里的定义

UCLASS()
class MYUE521_API UUWebSocketGameInstance : public UGameInstance
{GENERATED_BODY()public:virtual void Init() override;virtual void Shutdown() override;void OnPlayerCreated(ULocalPlayer* player);
}

说明:

  1.  Init()方法是在UGameInstance.h里定义的。源码里的注解是允许在这里自定义一些实现
  2. Shutdown()方法是在UGameInstance.h里定义的。源码里的注解是允许在这里自定义一些清理工作
  3. void OnPlayerCreated(ULocalPlayer* player) 方法是我自己定义的方法,用来监听创建player以后执行的方法

下面来看一下实现UWebSocketGameInstance.cpp里

void UUWebSocketGameInstance::Init() {Super::Init();OnLocalPlayerAddedEvent.AddUObject(this,&UUWebSocketGameInstance::OnPlayerCreated);}void UUWebSocketGameInstance::Shutdown() {//先写自定义的清理工作Super::Shutdown();
}void UUWebSocketGameInstance::OnPlayerCreated(ULocalPlayer* player) {UE_LOG(LogTemp, Warning, TEXT("%s plaer is created"), *FString(__FUNCTION__));
}

说明:

  1.  UE5里的事件绑定,这里绑定到自定义的OnPlayerCreated 方法里
  2. 触发事件OnLocalPlayerAddedEvent的位置在GameInstance.cpp里有AddLocalPlayer(ULocalPlayer* NewLocalPlayer, FPlatformUserId UserId)方法里有OnLocalPlayerAddedEvent.Broadcast(NewLocalPlayer); 通过广播通知


## 应用二,UE5 C++ UGameInstance  里player转成目标Characher的方法


ACharacter* character = UGameplayStatics::GetPlayerCharacter(this->GetWorld(), 0);
Amyue521Character* character2 = Cast<Amyue521Character>(character);
if (character2 == nullptr) {UE_LOG(LogTemp, Warning, TEXT("%s  1强制类型转换成Amyue521Character失败"), *FString(__FUNCTION__));
}
else {UE_LOG(LogTemp, Warning, TEXT("%s  1强制类型转换成Amyue521Character成功"), *FString(__FUNCTION__));
}

## 应用三,UE5 C++ UGameInstance  里一些方法罗列

/** Returns number of fully registered local players */
int32					GetNumLocalPlayers() const;/** Returns the local player at a certain index, or null if not found */
ULocalPlayer*			GetLocalPlayerByIndex(const int32 Index) const;/** Returns the first local player, will not be null during normal gameplay */
ULocalPlayer*			GetFirstGamePlayer() const;/** Returns the player controller assigned to the first local player. If World is specified it will search within that specific world */
APlayerController*		GetFirstLocalPlayerController(const UWorld* World = nullptr) const;/** Returns the local player assigned to a physical Controller Id, or null if not found */
ULocalPlayer*			FindLocalPlayerFromControllerId(const int32 ControllerId) const;/** Returns the local player assigned to this platform user id, or null if not found */
ULocalPlayer* FindLocalPlayerFromPlatformUserId(const FPlatformUserId UserId) const;/** Returns the local player that has been assigned the specific unique net id */
ULocalPlayer*			FindLocalPlayerFromUniqueNetId(FUniqueNetIdPtr UniqueNetId) const;
ULocalPlayer*			FindLocalPlayerFromUniqueNetId(const FUniqueNetId& UniqueNetId) const;
ULocalPlayer*			FindLocalPlayerFromUniqueNetId(const FUniqueNetIdRepl& UniqueNetId) const;/** Returns const iterator for searching list of local players */
TArray<ULocalPlayer*>::TConstIterator	GetLocalPlayerIterator() const;/** Returns reference to entire local player list */
const TArray<ULocalPlayer*> &			GetLocalPlayers() const;

#  UE5 C++ UGameInstance  其他

待补充

http://www.lryc.cn/news/152115.html

相关文章:

  • NodeJs-http模块
  • 翻译句子 前面的路是非常狭窄的 不能翻译成 the ahead of road is narrow 的原因
  • NTT功能与实现
  • Flutter(九)Flutter动画和自定义组件
  • 【python】可视化
  • C++继承多接口,调用虚函数跳转到错误接口的虚函数的奇怪问题
  • C++:日期类
  • c++ 学习之 构造函数的使用
  • 算法通关村15关 | 超大规模数据场景常见问题
  • qemu编译与使用
  • bazel远程构建(Remote Execution)
  • uniapp 微信小程序仿抖音评论区功能,支持展开收起
  • js:创建一个基于vite 的React项目
  • 论文阅读_医疗知识图谱_GraphCare
  • Android 蓝牙开发( 四 )
  • 涂鸦智能携手亚马逊云科技 共建“联合安全实验室” 为IoT发展护航
  • Oracle21C--Windows卸载与安装
  • 关于 MySQL、PostgresSQL、Mariadb 数据库2038千年虫问题
  • Linux - Docker 安装使用 常用命令 教程
  • AtCoder Beginner Contest 318 G - Typical Path Problem 题解
  • 21.4 CSS 盒子模型
  • MybatisPlus入门
  • 飞腾平台芯片测试固件(SFW)和开机启动log
  • 【大数据实训】基于Hive的北京市天气系统分析报告(二)
  • WPF列表样式
  • Android逆向学习(二)vscode进行双开与图标修改
  • 一个基于YAPI接口生产代码的开源工具
  • Redis 缓存穿透击穿和雪崩
  • 在windows上配置ninja环境
  • ③matlab向量和矩阵