GameLift 將 Amazon 集成到虛幻引擎項目中 - Amazon GameLift

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

GameLift 將 Amazon 集成到虛幻引擎項目中

本主題說明如何為虛幻引擎設定 Amazon GameLift C++ 伺服器 SDK 外掛程式,並將其整合到您的遊戲專案中。

其他資源:

必要條件

在處理之前,請確定您已檢閱下列先決條件:

必要條件
  • 能夠運行虛幻引擎的計算機。如需虛幻引擎需求的詳細資訊,請參閱虛幻引擎的硬體與軟體規格說明文件。

  • Microsoft 視覺工作室 2019 或更新版本。

  • C 製作 3.1 版或更高版本。

  • Python 版本 3.6 或更高版本。

  • 在路徑上可用的 Git 客戶端。

  • 一個史詩遊戲帳號。在虛幻引擎官方網站上註冊一個帳戶。

  • 與虛幻引擎 GitHub 帳戶相關聯的帳戶。如需詳細資訊,請參閱虛幻引擎網站 GitHub上的存取虛幻引擎原始程式碼

注意

Amazon GameLift 目前支持以下版本的虛幻引擎:

  • 4.22

  • 4.23

  • 4.24

  • 4.25

  • 4.26

  • 4.27

  • 5.1.0

  • 5.1.1

  • 5.2

  • 5.3

從源代碼構建虛幻引擎

通過 Epic 啟動器下載的虛幻引擎編輯器的標準版本僅允許虛幻客戶端應用程序構建。為了構建虛幻服務器應用程序,您需要使用虛幻引擎 Github 存儲庫從源下載和構建虛幻引擎。如需詳細資訊,請參閱虛幻引擎文件網站上的從原始碼建構虛幻引擎教學課程。

注意

如果您尚未這麼做,請依照上的存取虛幻引擎原始碼中的指示,將您的 GitHub 帳號連結 GitHub至您的 Epic Games 帳戶。

將虛幻引擎來源複製到您的開發環境
  1. 將虛幻引擎原始碼複製到您選擇的分支中的開發環境。

    git clone https://github.com/EpicGames/UnrealEngine.git
  2. 查看您用來開發遊戲的版本標籤。例如,下列範例會檢出虛幻引擎版本 5.1.1:

    git checkout tags/5.1.1-release -b 5.1.1-release
  3. 導覽至本機存放庫的根資料夾。當您位於根資料夾時,請執行下列檔案:Setup.bat

  4. 在根文件夾中,同時運行文件:GenerateProjectFiles.bat

  5. 執行上述步驟中的檔案之後,會建立虛幻引擎解決方案檔案。UE5.sln打開視覺工作室,並在視覺工作室編輯器中打開該UE5.sln文件。

  6. 在 Visual Studio 中,開啟 [視] 功能表,然後選擇 [方案總管] 選項。這會開啟虛幻專案節點的右鍵功能表。在 [方案總管] 視窗中,以滑鼠右鍵按一下UE5.sln檔案 (可以只列出UE5),然後選擇 [建置] 以使用開發編輯器 Win64 目標建置虛幻專案。

    注意

    組建可能需要一個多小時才能完成。

構建完成後,您就可以打開虛幻開發編輯器並創建或導入項目。

為外掛程式設定虛幻專案

請按照以下步驟操作,為您的遊戲 GameLift 服務器項目準備好虛幻引擎的 Amazon 服務器 SDK 插件。

若要設定外掛程式的專案
  1. 開啟 Visual Studio 時,瀏覽至 [方案總管] 窗格,然後選擇要開啟虛幻專UE5案的內容功能表的檔案。在內容功能表中,選擇「設定為啟動專案」選項。

  2. 在您的 Visual Studio 視窗的頂端,選擇開始偵錯 (綠色箭頭)。

    此動作會啟動虛幻編輯器的新原始碼建置執行個體。如需使用虛幻編輯器的詳細資訊,請參閱虛幻引擎文件網站上的虛幻編輯器介面

  3. 關閉您開啟的 Visual Studio 視窗,因為虛幻編輯器會開啟另一個 Visual Studio 視窗,其中包含虛幻專案和您的遊戲專案。

  4. 在「虛幻」編輯器中,執行下列任一項作業:

    • 選擇您想要與 Amazon GameLift 整合的現有虛幻專案。

    • 建立新專案。要嘗試使用虛幻的 Amazon GameLift 插件,請嘗試使用虛幻引擎的第三人稱模板。如需此範本的詳細資訊,請參閱虛幻引擎文件網站上的第三人稱視角範本

      或者,使用下列設定來設定新專案:

      • C++

      • 隨著入門內容

      • 桌面

      • 專案名稱。在本主題的範例中,我們將專案命名為GameLiftUnrealApp

  5. 在 Visual Studio 的解決方案總管中,瀏覽至虛幻專案的位置。在「虛幻」Source 資料夾中,找到名為Your-application-name.Target.cs的檔案。

    例如:GameLiftUnrealApp.Target.cs

  6. 複製此檔案並命名副本:Your-application-nameServer.Target.cs

  7. 開啟新檔案並進行下列變更:

    • 變更classconstructor以符合檔案名稱。

    • TargetType.GameType從變更為TargetType.Server

    • 最後一個檔案看起來像下列範例:

      public class GameLiftUnrealAppServerTarget : TargetRules { public GameLiftUnrealAppServerTarget(TargetInfo Target) : base(Target) { Type = TargetType.Server; DefaultBuildSettings = BuildSettingsVersion.V2; IncludeOrderVersion = EngineIncludeOrderVersion.Unreal5_1; ExtraModuleNames.Add("GameLiftUnrealApp"); } }

您的專案現在已設定為接受 Amazon GameLift 伺服器 SDK 外掛程式。

下一個任務是為虛幻構建 C ++ 服務器 SDK 庫,以便您可以將它們導入到項目中。

為虛幻構建 C ++ 服務器 SDK 庫
  1. 下載虛幻的 Amazon GameLift C ++ 服務器 SDK 插件

    注意

    將 SDK 置於預設下載目錄中,可能會因為路徑超過 260 個字元限制而導致建置失敗。例如:C:\Users\Administrator\Downloads\GameLift-SDK-Release-06_15_2023\GameLift-Cpp-ServerSDK-5.0.4

    例如,我們建議您將 SDK 移至其他目錄C:\GameLift-Cpp-ServerSDK-5.0.4

  2. 下載 OpenSSL 裝 如需下載 OpenSSL 的詳細資訊,請參閱建置與安裝說明文件。

    如需詳細資訊,請參閱 Windows 平台適用的 OpenSSL 注意事項說明文件。

    注意

    您用來建立 Amazon GameLift 伺服器開發套件的 OpenSSL 版本應與虛幻用來封裝遊戲伺服器的 OpenSSL 版本相符。您可以在虛幻安裝目錄...Engine\Source\ThirdParty\OpenSSL中找到版本資訊。

  3. 下載庫後,為虛幻引擎構建 C ++ 服務器 SDK 庫。

    在下載 SDK 的GameLift-Cpp-ServerSDK-<version>目錄中,使用-DBUILD_FOR_UNREAL=1參數進行編譯並構建服務器 SDK。下面的例子演示了如何使用編譯cmake.

    在終端機中執行下列命令:

    mkdir cmake-build cmake.exe -G "Visual Studio 17 2022" -DCMAKE_BUILD_TYPE=Release -S . -B ./cmake-build -DBUILD_FOR_UNREAL=1 -A x64 cmake.exe --build ./cmake-build --target ALL_BUILD --config Release

    Windows 組建會在out\gamelift-server-sdk\Release資料夾中建立下列二進位檔案:

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

    • cmake-build\prefix\bin\aws-cpp-sdk-gamelift-server.lib

    將這兩個程式庫檔案複製到 Amazon GameLift 虛幻引擎外掛程式套件中的ThirdParty\GameLiftServerSDK\Win64資料夾。

使用下列程序將 Amazon GameLift 外掛程式匯入範例專案。

導入 Amazon GameLift 插件
  1. 在先前的程序中,找出您從外掛程式解壓縮的GameLiftServerSDK資料夾。

  2. Plugins在您的遊戲項目根文件夾中找到。(如果該文件夾不存在,則在那裡創建它。)

  3. GameLiftServerSDK資料夾複製到Plugins.

    這將允許虛幻項目看到插件。

  4. 將 Amazon GameLift 服務器 SDK 插件添加到遊戲的.uproject文件中。

    在這個例子中,應用程序被調用GameLiftUnrealApp,所以該文件將是GameLiftUnrealApp.uproject

  5. 編輯.uproject檔案以將外掛程式新增至您的遊戲專案。

    "Plugins": [ { "Name": "GameLiftServerSDK", "Enabled": true } ]
  6. 確保遊戲依賴於插件。 ModuleRules 開啟.Build.cs檔案並新增 Amazon GameLiftServer SDK 相依性。此檔案位於下Your-application-name/Source//Your-application-name/

    例如,教學課程檔案路徑為../GameLiftUnrealApp/Source/GameLiftUnrealApp/GameLiftUnrealApp.Build.cs

  7. 添加"GameLiftServerSDK"到列表的末尾PublicDependencyModuleNames

    using UnrealBuildTool; using System.Collections.Generic; public class GameLiftUnrealApp : ModuleRules { public GameLiftUnrealApp(TargetInfo Target) { PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "GameLiftServerSDK" }); bEnableExceptions = true; } }

該插件現在應該可以為您的應用程序工作。繼續下一節,將 Amazon GameLift 功能整合到您的遊戲中。

將 Amazon GameLift 服務器代碼添加到您的虛幻項目

您已經配置和設置了虛幻引擎環境,現在可以將遊戲服務器與 Amazon GameLift 集成。本主題中顯示的程式碼會對 Amazon GameLift 服務進行必要的呼叫。它還實現了一組回調函數,用於響應來自 Amazon GameLift 服務的請求。有關每個函數以及代碼功能的詳細信息,請參閱初始化服務器進程。有關此代碼中的 SDK 操作和數據類型的更多信息,請閱讀。虛幻引擎的亞馬遜GameLift服務器 SDK 參考

若要使用 Amazon 初始化遊戲伺服器 GameLift,請使用下列程序。

注意

下一節提供的 Amazon GameLift 特定程式碼取決於WITH_GAMELIFT預處理器旗標的使用情況。只有在符合下列兩個條件時,此旗標才為真:

  • Target.Type == TargetRules.TargetType.Server

  • 這些插件找到了 Amazon GameLift 服務器 SDK 二進製文件。

這確保了只有虛幻服務器構建調用 Amazon GameLift 的後端 API。它還允許您編寫代碼,以便為您的遊戲可能產生的所有不同虛幻目標正確執行。

將遊戲伺服器與 Amazon 整合 GameLift
  1. 在您的應用程式中開啟.sln檔案。在我們的例子中,該文件GameLiftUnrealApp.sln位於根文件夾中。

  2. 開啟解決方案後,找出應用程式的Your-application-nameGameMode.h檔案。範例:GameLiftUnrealAppGameMode.h

  3. 將標頭檔案變更為與下列範例程式碼對齊。請務必將 "GameLiftUnrealApp" 取代為您自己的應用程式名稱。

    #pragma once #include "CoreMinimal.h" #include "GameFramework/GameModeBase.h" #include "GameLiftServerSDK.h" #include "GameLiftUnrealAppGameMode.generated.h" DECLARE_LOG_CATEGORY_EXTERN(GameServerLog, Log, All); UCLASS(minimalapi) class AGameLiftUnrealAppGameMode : public AGameModeBase { GENERATED_BODY() public: AGameLiftUnrealAppGameMode(); protected: virtual void BeginPlay() override; private: // Process Parameters needs to remain in scope for the lifetime of the app FProcessParameters m_params; void InitGameLift(); };
  4. 開啟相關的來源Your-application-nameGameMode.cpp檔案。在我們的 Example:GameLiftUnrealAppGameMode.cpp. 中,並將代碼更改為與以下示例代碼對齊。請務必將 "GameLiftUnrealApp" 取代為您自己的應用程式名稱。

    此範例說明如何新增與 Amazon 整合的所有必要元素 GameLift,如將 Amazon 新增 GameLift至遊戲伺服器中所述。其中包含:

    • 初始化 Amazon GameLift API 客戶端。

    • 實施回調函數以響應來自 Amazon GameLift 服務的請求OnStartGameSession,包括OnProcessTerminate、和onHealthCheck

    • 使用指定的連接埠呼叫 ProcessReady (),在準備好主持遊戲工作階段 GameLiftservice 時通知 Amazon。

    #include "GameLiftUnrealAppGameMode.h" #include "GameLiftUnrealAppCharacter.h" #include "UObject/ConstructorHelpers.h" DEFINE_LOG_CATEGORY(GameServerLog); AGameLiftUnrealAppGameMode::AGameLiftUnrealAppGameMode() { // set default pawn class to our Blueprinted character static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPerson/Blueprints/BP_ThirdPersonCharacter")); if (PlayerPawnBPClass.Class != NULL) { DefaultPawnClass = PlayerPawnBPClass.Class; } } void AGameLiftUnrealAppGameMode::BeginPlay() { #if WITH_GAMELIFT InitGameLift(); #endif } void AGameLiftUnrealAppGameMode::InitGameLift() { UE_LOG(GameServerLog, Log, TEXT("Initializing the GameLift Server")); //Getting the module first. FGameLiftServerSDKModule* gameLiftSdkModule = &FModuleManager::LoadModuleChecked<FGameLiftServerSDKModule>(FName("GameLiftServerSDK")); //Define the server parameters for a GameLift Anywhere fleet. These are not needed for a GameLift managed EC2 fleet. FServerParameters serverParameters; //AuthToken returned from the "aws gamelift get-compute-auth-token" API. Note this will expire and require a new call to the API after 15 minutes. if (FParse::Value(FCommandLine::Get(), TEXT("-authtoken="), serverParameters.m_authToken)) { UE_LOG(GameServerLog, Log, TEXT("AUTH_TOKEN: %s"), *serverParameters.m_authToken) } //The Host/compute-name of the GameLift Anywhere instance. if (FParse::Value(FCommandLine::Get(), TEXT("-hostid="), serverParameters.m_hostId)) { UE_LOG(GameServerLog, Log, TEXT("HOST_ID: %s"), *serverParameters.m_hostId) } //The Anywhere Fleet ID. if (FParse::Value(FCommandLine::Get(), TEXT("-fleetid="), serverParameters.m_fleetId)) { UE_LOG(GameServerLog, Log, TEXT("FLEET_ID: %s"), *serverParameters.m_fleetId) } //The WebSocket URL (GameLiftServiceSdkEndpoint). if (FParse::Value(FCommandLine::Get(), TEXT("-websocketurl="), serverParameters.m_webSocketUrl)) { UE_LOG(GameServerLog, Log, TEXT("WEBSOCKET_URL: %s"), *serverParameters.m_webSocketUrl) } //The PID of the running process serverParameters.m_processId = FString::Printf(TEXT("%d"), GetCurrentProcessId()); UE_LOG(GameServerLog, Log, TEXT("PID: %s"), *serverParameters.m_processId); //InitSDK establishes a local connection with GameLift's agent to enable further communication. //Use InitSDK(serverParameters) for a GameLift Anywhere fleet. //Use InitSDK() for a GameLift managed EC2 fleet. gameLiftSdkModule->InitSDK(serverParameters); //Implement callback function onStartGameSession //GameLift sends a game session activation request to the game server //and passes a game session object with game properties and other settings. //Here is where a game server takes action based on the game session object. //When the game server is ready to receive incoming player connections, //it invokes the server SDK call ActivateGameSession(). auto onGameSession = [=](Aws::GameLift::Server::Model::GameSession gameSession) { FString gameSessionId = FString(gameSession.GetGameSessionId()); UE_LOG(GameServerLog, Log, TEXT("GameSession Initializing: %s"), *gameSessionId); gameLiftSdkModule->ActivateGameSession(); }; m_params.OnStartGameSession.BindLambda(onGameSession); //Implement callback function OnProcessTerminate //GameLift invokes this callback before shutting down the instance hosting this game server. //It gives the game server a chance to save its state, communicate with services, etc., //and initiate shut down. When the game server is ready to shut down, it invokes the //server SDK call ProcessEnding() to tell GameLift it is shutting down. auto onProcessTerminate = [=]() { UE_LOG(GameServerLog, Log, TEXT("Game Server Process is terminating")); gameLiftSdkModule->ProcessEnding(); }; m_params.OnTerminate.BindLambda(onProcessTerminate); //Implement callback function OnHealthCheck //GameLift invokes this callback approximately every 60 seconds. //A game server might want to check the health of dependencies, etc. //Then it returns health status true if healthy, false otherwise. //The game server must respond within 60 seconds, or GameLift records 'false'. //In this example, the game server always reports healthy. auto onHealthCheck = []() { UE_LOG(GameServerLog, Log, TEXT("Performing Health Check")); return true; }; m_params.OnHealthCheck.BindLambda(onHealthCheck); //The game server gets ready to report that it is ready to host game sessions //and that it will listen on port 7777 for incoming player connections. m_params.port = 7777; //Here, the game server tells GameLift where to find game session log files. //At the end of a game session, GameLift uploads everything in the specified //location and stores it in the cloud for access later. TArray<FString> logfiles; logfiles.Add(TEXT("GameLift426Test/Saved/Logs/GameLift426Test.log")); m_params.logParameters = logfiles; //The game server calls ProcessReady() to tell GameLift it's ready to host game sessions. UE_LOG(GameServerLog, Log, TEXT("Calling Process Ready")); gameLiftSdkModule->ProcessReady(m_params); }
  5. 為下列兩種目標類型建置遊戲專案:開發編輯器和開發伺服器

    注意

    您不需要重建解決方案。相反,只在與您的應用程序名稱匹配的Games文件夾下構建項目。否則,視覺工作室重建整個 UE5 專案,這可能需要長達一個小時。

  6. 兩個組建都完成後,請關閉 Visual Studio 並開啟專.uproject案的檔案,以便在虛幻編輯器中開啟。

  7. 在虛幻編輯器中,封裝遊戲的伺服器組建。若要選擇目標,請移至「平台」、「視窗」,然後選取「Y our-application-name 伺服器」。

  8. 要開始構建服務器應用程序的過程,請轉到平台Windows,然後選擇 P ackage 項目。當構建完成時,您應該有一個可執行文件。在我們的例子中,文件名是GameLiftUnrealAppServer.exe

  9. 在虛幻編輯器中構建服務器應用程序會產生兩個可執行文件。其中一個位於遊戲組建資料夾的根目錄中,並做為實際伺服器可執行檔的包裝函式。

    使用伺服器組建建建立 Amazon GameLift 叢集時,建議您傳入實際的伺服器可執行檔做為執行階段組態啟動路徑。例如,在您的遊戲構建文件夾中,您可能在根目錄中有一個GameLiftFPS.exe文件,另一個文件位於\GameLiftFPS\Binaries\Win64\GameLiftFPSServer.exe。建立叢集時,建議您使用C:\GameLiftFPS\Binaries\Win64\GameLiftFPSServer.exe做為執行階段設定的啟動路徑。

  10. 請務必在 Amazon GameLift 叢集上開啟必要的 UDP 連接埠,以便遊戲伺服器可以與遊戲用戶端進行通訊。默認情況下,虛幻引擎使用端口7777。如需詳細資訊,請參閱 Amazon GameLift 服務 API 參考指南UpdateFleetPortSettings中的。

  11. 為您的遊戲構建創建一個install.bat文件。只要將遊戲組建部署到 Amazon GameLift 叢集,就會執行此安裝指令碼。這是一個示例install.bat文件:

    VC_redist.x64.exe /q UE5PrereqSetup_x64.exe /q

    對於某些版本的虛幻引擎,install.bat應該是

    VC_redist.x64.exe /q UEPrereqSetup_x64.exe /q
    注意

    檔案的檔<>PrereqSetup_x64.exe案路徑為Engine\Extras\Redist\en-us

  12. 現在,您可以打包並上傳您的遊戲構建到 Amazon GameLift。

    您在遊戲組建中封裝的 OpenSSL 版本必須符合遊戲引擎在建置遊戲伺服器時所使用的版本。請務必將正確的 OpenSSL 版本與您的遊戲伺服器組建一起封裝。對於視窗作業系統,OpenSSL 格式為.dll.

    注意

    在您的遊戲伺服器組建中 Package OpenSSL DLL 檔案。請務必封裝您在建置遊戲伺服器時所使用的 OpenSSL 版本相同。

    • libssl-1_1-x64.dll

      libcrypto-1_1-x64.dll

    將您的依賴項與遊戲服務器可執行文件一起打 Package 到 zip 文件的根目錄中。例如,openssl-libdll 應該位於與.exe文件相同的目錄中。

後續步驟

您已經配置並設置了虛幻引擎環境,現在可以開始 GameLift 將 Amazon 集成到您的遊戲中。

如需將 Amazon 新增 GameLift 至遊戲的詳細資訊,請參閱下列內容:

如需測試遊戲的指示,請參閱使用 Amazon 設置本地測試 GameLift Anywhere