Amazon GameLift
开发人员指南 (版本 )
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

将 Amazon GameLift 添加到 Unreal Engine 游戏服务器项目

本主题可帮助您在游戏服务器项目中设置和使用适用于 Unreal Engine 的 Amazon GameLift 服务器开发工具包插件。如果您不确定 Amazon GameLift 是否支持您所使用的操作系统的信息,请参阅对于游戏服务器

设置 Unreal Engine 服务器软件开发工具包插件

按照以下步骤为您的游戏服务器项目准备好适用于 Unreal Engine 的 Amazon GameLift 服务器软件开发工具包插件。

设置适用于 Unreal Engine 的 Amazon GameLift 软件开发工具包插件

  1. 下载 Amazon GameLift 服务器开发工具包 要验证是否支持您的游戏系统要求,请参阅 Amazon GameLift 软件开发工具包

  2. 构建适用于 Unreal 的 C++ 服务器软件开发工具包库。 开发工具包下载中包含 C++ 的源代码(请参阅 GameLift_<release date>\GameLift-SDK-Release-<version>\GameLift-cpp-ServerSDK-<version>)。生成开发工具包前检查此目录中 README 文件了解最低要求。

    要构建开发工具包库,请转到目录 GameLift-cpp-ServerSDK-<version> 并使用设置为 true 的标志 -DBUILD_FOR_UNREAL。以下说明介绍了如何使用 cmake 进行编译。

    对于 Linux 用户:

    mkdir out cd out cmake -DBUILD_FOR_UNREAL=1 .. make

    生成以下二进制文件:

    • out/prefix/lib/libaws-cpp-sdk-gamelift-server.so

    对于 Windows 用户:

    mkdir out cd out cmake -G "Visual Studio 14 2015 Win64" -DBUILD_FOR_UNREAL=1 .. msbuild ALL_BUILD.vcxproj /p:Configuration=Release

    生成以下二进制文件:

    • out\prefix\bin\aws-cpp-sdk-gamelift-server.dll

    • out\prefix\lib\aws-cpp-sdk-gamelift-server.lib

    有关构建 C++ 软件开发工具包的更多详细信息,包括最低要求和构建选项,请参阅下载中包含的 README.md 文件。

  3. 将二进制文件添加到 Amazon GameLift 插件文件。 将新二进制文件复制到 Unreal 插件的 ThirdParty 目录中,路径如下:

    在 Linux 中的路径:

    • ThirdParty/GameLiftServerSDK/Linux/x86_64-unknown-linux-gnu/aws-cpp-sdk-gamelift-server.so

    在 Windows 中的路径:

    • ThirdParty\GameLiftServerSDK\Win64\aws-cpp-sdk-gamelift-server.dll

    • ThirdParty\GameLiftServerSDK\Win64\aws-cpp-sdk-gamelift-server.lib

  4. 将 Amazon GameLift 插件导入到项目中。 将插件导入 Unreal Engine 的方法有许多种,但以下方法不需要 Unreal 编辑器。

    1. 将插件添加到游戏项目。插件的 GameLiftServerSDK 目录下的所有内容必须包含在插件文件中,包括已生成的二进制文件。

    2. 将插件添加到游戏的 .uproject 文件:

      "Plugins": [ { "Name": "GameLiftServerSDK", "Enabled": true } ]
    3. 添加插件与游戏的 ModuleRules 的依赖关系:

      using UnrealBuildTool; public class MyAwesomeGame : ModuleRules { public MyAwesomeGame(TargetInfo Target) { PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "GameLiftServerSDK" }); } }

添加 Amazon GameLift 代码

有关添加 Amazon GameLift 功能的更多信息,请参阅以下主题:

在将特定于 Amazon GameLift 的代码添加到 Unreal Engine 游戏项目时,请使用预处理器标记 WITH_GAMELIFT=1 将代码括起来。此标记确保只有服务器生成包可调用 Amazon GameLift 背板 API,这样,无论您的生成目标是什么类型,写入的代码都可以正确执行。

只有在满足以下条件时,才执行用 WITH_GAMELIFT=1 标记括起来的代码:

  • 插件找到 Amazon GameLift 服务器软件开发工具包二进制文件。

  • 生成包是游戏服务器:Target.Type == TargetRules.TargetType.Server

以下代码段介绍了如何使用 Amazon GameLift 初始化 Unreal Engine 游戏服务器。

// "GameLiftFPS" is a sample game name, replace with file names from your own game project #include "GameLiftFPS.h" #include "Engine.h" #include "EngineGlobals.h" #include "GameLiftFPSGameMode.h" #include "GameLiftFPSHUD.h" #include "GameLiftFPSCharacter.h" #include "GameLiftServerSDK.h" AGameLiftFPSGameMode::AGameLiftFPSGameMode() : Super() { //Let's run this code only if GAMELIFT is enabled. Only with Server targets! #if WITH_GAMELIFT //Getting the module first. FGameLiftServerSDKModule* gameLiftSdkModule = &FModuleManager::LoadModuleChecked<FGameLiftServerSDKModule>(FName("GameLiftServerSDK")); //InitSDK establishes a local connection with GameLift's agent to enable further communication. gameLiftSdkModule->InitSDK(); //When a game session is created, GameLift sends an activation request to the game server and passes along the game session object containing game properties and other settings. //Here is where a game server should take action based on the game session object. //Once the game server is ready to receive incoming player connections, it should invoke GameLiftServerAPI.ActivateGameSession() auto onGameSession = [=](Aws::GameLift::Server::Model::GameSession gameSession) { gameLiftSdkModule->ActivateGameSession(); }; FProcessParameters* params = new FProcessParameters(); params->OnStartGameSession.BindLambda(onGameSession); //OnProcessTerminate callback. GameLift invokes this callback before shutting down an instance hosting this game server. //It gives this game server a chance to save its state, communicate with services, etc., before being shut down. //In this case, we simply tell GameLift we are indeed going to shut down. params->OnTerminate.BindLambda([=](){gameLiftSdkModule->ProcessEnding();}); //This is the HealthCheck callback. //GameLift invokes this callback every 60 seconds or so. //Here, a game server might want to check the health of dependencies and such. //Simply return true if healthy, false otherwise. //The game server has 60 seconds to respond with its health status. GameLift defaults to 'false' if the game server doesn't respond in time. //In this case, we're always healthy! params->OnHealthCheck.BindLambda([](){return true; }); //This game server tells GameLift that it listens on port 7777 for incoming player connections. params->port = 7777; //Here, the game server tells GameLift what set of files to upload when the game session ends. //GameLift uploads everything specified here for the developers to fetch later. TArray<FString> logfiles; logfiles.Add(TEXT("aLogFile.txt")); params->logParameters = logfiles; //Calling ProcessReady tells GameLift this game server is ready to receive incoming game sessions! gameLiftSdkModule->ProcessReady(*params); #endif }