虚幻 Unreal Engine 4 Gameplay Framework(2)—— GameInstance

参考阅读:https://www.tomlooman.com/ue4-gameplay-framework/
文件路径:\Engine\Source\Runtime\Engine\Classes\Engine\GameInstance.h

描述


by Tom

GameInstance has one instance that persists throughout the lifetime of the game. Traveling between maps and menus will maintain the same instance of this class. This class can be used to provide event hooks of handling network errors, loading of user data like game settings and generally features that are not relevant to only a specific level of your game.

header file declaration

/**
 * GameInstance: high-level manager object for an instance of the running game.
 * Spawned at game creation and not destroyed until game instance is shut down.
 * Running as a standalone game, there will be one of these.
 * Running in PIE (play-in-editor) will generate one of these per PIE instance.
 */

GameInstance类:高层级的管理运行游戏实例的类,游戏创建时生成,直到游戏关闭才销毁。每个运行着的游戏对应唯一的GameInstance实例。与游戏场景、关卡不相关的数据和逻辑可以放在这里。从头文件中可以看出,GameInstance相关功能如下:游戏录制、回放,网络会话,用户管理,游戏初始化、关闭等。

访问


  • GetWorld()->GetGameInstance()
  • Actor->GetGameInstance()

Remark from Tom


Generally not used a whole lot early in your game project. Doesn’t do anything critical unless you’re diving deeper into the development (things like game sessions, demo playback or persisting some data between levels)
通常不会在游戏项目的早期使用。除非你深入研究开发(比如游戏会话、演示播放或在关卡间保持数据),否则不要在这个类中做任何重要的事情。

官方案例


Unreal Match 3

UMatch3GameInstance 类 + GlobalGameInstance蓝图
主要功能为:存储、加载用户数据,修改对应的UI,管理用户登录GameStore,更改相机视窗大小,管理游戏前后台切换,检查相机视窗大小是否改变

你可能感兴趣的:(虚幻 Unreal Engine 4 Gameplay Framework(2)—— GameInstance)