参考阅读:https://www.tomlooman.com/ue4-gameplay-framework/
文件路径:
\Engine\Source\Runtime\Engine\Classes\GameFramework\GameModeBase.h
\Engine\Source\Runtime\Engine\Classes\GameFramework\GameMode.h
描述
by Tom
The primary class that specifies which classes to use (PlayerController, Pawn, HUD, GameState, PlayerState) and commonly used to specify game rules for modes such as ‘Capture the Flag’ where it could handle the flags, or to handle ‘wave spawns’ in a wave based shooter. Handles other key features like spawning the player as well.
GameMode is a sub-class of GameModeBase and contains a few more features originally used by Unreal Tournament such as MatchState and other shooter type features.
In multiplayer the GameMode class only exists on the server! This means no client ever has an instance. For singleplayer games this has no effect. To replicate functions and store data you need for your GameMode you should consider using GameState which does exist on all clients and is made for specifically this purpose.
header file declaration
- GameModeBase.h
/**
* The GameModeBase defines the game being played. It governs the game rules, scoring, what actors
* are allowed to exist in this game type, and who may enter the game.
*
* It is only instanced on the server and will never exist on the client.
*
* A GameModeBase actor is instantiated when the level is initialized for gameplay in
* C++ UGameEngine::LoadMap().
*
* The class of this GameMode actor is determined by (in order) either the URL ?game=xxx,
* the GameMode Override value set in the World Settings, or the DefaultGameMode entry set
* in the game's Project Settings.
*
* @see https://docs.unrealengine.com/latest/INT/Gameplay/Framework/GameMode/index.html
*/
- GameMode.h
/**
* GameMode is a subclass of GameModeBase that behaves like a multiplayer match-based game.
* It has default behavior for picking spawn points and match state.
* If you want a simpler base, inherit from GameModeBase instead.
*/
GameModeBase类:每个Level有对应的GameModeBase类,只存在于服务端上,它负责管理游戏规则,得分,什么Actor可以在这个场景中存在,谁可以加入这场游戏等等。在GameMode中指定这个场景中PlayerController,Pawn,HUD,GameState,PlayerState使用的类型。通常,我们都会继承这个类或GameMode类定义自己的GameMode。
GameMode类:是GameModeBase类的子类,它定义的模式为多人对局型游戏,如CS,虚幻竞技场
访问
GetWorld()->GetAuthGameMode() // GetWorld is available from any Actor instance.
GameModeBase类中的功能
大致列举头文件中的说明:
- Initializing the game :初始化游戏
- Accessors for classes spawned by game: 游戏生成的类的类型名,即TSubclassOf<T>类型的变量
- Accessors for current state:游戏当前转态的访问器
- Starting/pausing/resetting the game:开始/暂停/重置游戏
- Player joining and leaving:玩家加入、离开游戏
- Spawning the player's pawn:生成玩家的pawn
- Engine hooks:引擎钩子
- 各种事件
官方案例
Unreal Match 3
UMatch3GameMode 类
主要功能为:定义游戏奖励,棋子移动速度,管理游戏开始、暂停等状态,定义炸弹能量相关参数、显示和隐藏UI等