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

# UE5 C++ UGameInstance 功能及作用


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

# UE5 C++ UGameInstance 应用


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


UWebSocketGameInstance.h里的定义

cpp 复制代码
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里

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的方法


cpp 复制代码
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 里一些方法罗列

cpp 复制代码
/** 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 其他

待补充

相关推荐
m0_748233647 分钟前
【C++篇】C++11入门:踏入C++新世界的大门
java·c++·算法
m0_7482336423 分钟前
jank实现C++无缝互操作的技术探索
开发语言·c++
zhangzhangkeji24 分钟前
UE5 蓝图-21:主 mainUI 界面蓝图,颜色按钮蓝图 Ul_colorsUl 的内容,尺寸,事件分发器还有赋予按钮形状的环状材质
ue5
沢田纲吉24 分钟前
《LLVM IR 学习手记(七):逻辑运算与位运算的实现与解析》
前端·c++·编译器
BAGAE26 分钟前
HTTPS 加密原理介绍
java·c++·websocket·http·均值算法·启发式算法·最小二乘法
污斑兔36 分钟前
技术随笔:Node.js ESM 中巧用 `-r dotenv/config` 解决环境变量异步加载问题
开发语言·r语言·node.js
半桔1 小时前
【IO多路转接】IO 多路复用之 select:从接口解析到服务器实战
linux·服务器·c++·github·php
无聊的小坏坏1 小时前
从零开始:C++ 线程池 TCP 服务器实战(续篇)
服务器·c++·tcp/ip
ALex_zry1 小时前
C++中使用gRPC over Unix Domain Sockets的高性能进程间通信技术解析
开发语言·c++·unix
Q741_1471 小时前
C++ 分治 快速排序优化 三指针快排 力扣 面试题 17.14. 最小K个数 题解 每日一题
c++·算法·leetcode·快排·topk问题