Amazon GameLift
开发人员指南 (版本 )
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 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>)。在构建开发工具包之前,查看此目录中的自述文件以了解最低要求和其他信息。

    要构建开发工具包库,请转到目录 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 15 2017 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 插件文件。 打开您正在使用的 UE4 的插件版本的目录(例如,GameLift-SDK-Release-3.3.0\GameLift-Unreal-plugin-3.3.0\UE4.21.1\GameLiftServerSDK)。将步骤 2 中创建的二进制文件复制到 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 Editor。

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

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

      "Plugins": [ { "Name": "GameLiftServerSDK", "Enabled": true } ]
    3. 将插件名称作为依赖项添加到游戏的 ModuleRules 列表中。以下示例显示了模块名称的列表以及添加到其中的 Amazon GameLift 插件。

      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 游戏服务器。

//This is an example of a simple integration with GameLift server SDK that makes game server //processes go active on Amazon GameLift // Include game project files. "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 communication. gameLiftSdkModule->InitSDK(); //Respond to new game session activation request. GameLift sends activation request //to the game server along with a game session object containing game properties //and other settings. Once the game server is ready to receive player connections, //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 before shutting down the instance //that is hosting this game server to give it time to gracefully shut down on its own. //In this example, we simply tell GameLift we are indeed going to shut down. params->OnTerminate.BindLambda([=](){gameLiftSdkModule->ProcessEnding();}); //HealthCheck callback. GameLift invokes this callback about every 60 seconds. By default, //GameLift API automatically responds 'true'. A game can optionally perform checks on //dependencies and such and report status based on this info. If no response is received //within 60 seconds, health status is recorded as 'false'. //In this example, we're always healthy! params->OnHealthCheck.BindLambda([](){return true; }); //Here, the game server tells GameLift what port it is listening on for incoming player //connections. In this example, the port is hardcoded for simplicity. Since active game //that are on the same instance must have unique ports, you may want to assign port values //from a range, such as: //const int32 port = FURL::UrlConfig.DefaultPort; //params->port; 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; //Call ProcessReady to tell GameLift this game server is ready to receive game sessions! gameLiftSdkModule->ProcessReady(*params); #endif }