本文内容
第31章 设置多人游戏
第31章 设置多人游戏
Introduction
注意:本章随附的源代码和资源可以在 GitHub 上找到: https://github.com/AMZN-Olex/O3DEBookCode2111/tree/ch31_enable_multiplayer_gem
我们的项目启用了 Multiplayer gem,但是,在开始编写多人游戏组件之前,还需要进行更多设置。
例 31.1.需要做的还有很多
set(ENABLED_GEMS
...
Multiplayer
)
在 O3DE 中,编写多人游戏组件涉及代码生成,因此我们必须为将生成和编译多人游戏组件的 Gem 和项目添加代码生成支持。
启用 Multiplayer 代码生成
在本章中,我们将为 C:\git\book\Gems\MyGem 中的 MyGem 启用多人游戏组件支持。
注意:对于包含多人游戏组件的任何 Gem,也需要执行相同的步骤。
- 将代码生成文件添加到 MyGem.Static 构建目标。
例 31.2. mygem_autogen_files.cmake
set(MPGEMPATH ${LY_ROOT_FOLDER}/Gems/Multiplayer/Code/Include)
set(AUTOPATH ${MPGEMPATH}/Multiplayer/AutoGen)
set(FILES
${AUTOPATH}/AutoComponent_Common.jinja
${AUTOPATH}/AutoComponent_Header.jinja
${AUTOPATH}/AutoComponent_Source.jinja
${AUTOPATH}/AutoComponentTypes_Header.jinja
${AUTOPATH}/AutoComponentTypes_Source.jinja
)
- 将此列表添加到 MyGem.Static CMake 目标。
ly_add_target(
NAME MyGem.Static STATIC
FILES_CMAKE
mygem_autogen_files.cmake
...
- 向 MyGem.Static 添加代码生成规则:
ly_add_target(
NAME MyGem.Static STATIC
...
AUTOGEN_RULES
*.AutoComponent.xml,AutoComponent_Header.jinja,
$path/$fileprefix.AutoComponent.h
*.AutoComponent.xml,AutoComponent_Source.jinja,
$path/$fileprefix.AutoComponent.cpp
*.AutoComponent.xml,AutoComponentTypes_Header.jinja,
$path/AutoComponentTypes.h
*.AutoComponent.xml,AutoComponentTypes_Source.jinja,
$path/AutoComponentTypes.cpp
)
注意:AUTOGEN_RULES的每一行都不应包含换行符。书籍格式强制换行,但这部分实际上在您的CMakeLists.txt中应如下所示:
AUTOGEN_RULES *.AutoComponent.xml,Auto...ileprefix.AutoComponent.h *.AutoComponent.xml,Auto...ileprefix.AutoComponent.cpp *.AutoComponent.xml,Auto...ComponentTypes.h *.AutoComponent.xml,Auto...ComponentTypes.cpp
否则,您将看到奇怪的 CMake 错误。
- 为 MyGem.Static 添加对 Multiplayer.Static 的依赖项。多人游戏组件需要它。
ly_add_target(
NAME MyGem.Static STATIC
BUILD_DEPENDENCIES
PUBLIC
# For autogen components
Gem::Multiplayer.Static
...
- 此时,MyGem.static 的 CMake 构建目标应如下所示。
例 31.3.在启用多人游戏的情况下完成 MyGem.Static
ly_add_target(
NAME MyGem.Static STATIC
NAMESPACE Gem
FILES_CMAKE
mygem_files.cmake
mygem_autogen_files.cmake
INCLUDE_DIRECTORIES
PUBLIC
Include
PRIVATE
Source
BUILD_DEPENDENCIES
PUBLIC
AZ::AzCore
AZ::AzFramework
Gem::StartingPointInput.Static
Gem::LyShine.Static
Gem::PhysX.Static
Gem::EMotionFXStaticLib
# For autogen components
Gem::Multiplayer.Static
AUTOGEN_RULES
*.AutoComponent.xml,Au...refix.AutoComponent.h
*.AutoComponent.xml,Au...refix.AutoComponent.cpp
*.AutoComponent.xml,Au...ComponentTypes.h
*.AutoComponent.xml,Au...ComponentTypes.cpp
)
- 将多人游戏组件描述符添加到 Gem 中。您需要对 Gem 中的所有多人游戏组件进行一次此类调用。
例 31.4.对 MyGemModuleInterface.h 的更改
#include <Source/AutoGen/AutoComponentTypes.h>
MyGemModuleInterface()
{
m_descriptors.insert(m_descriptors.end(), {
...
});
...
//< Register multiplayer components
CreateComponentDescriptors(m_descriptors);
}
注意:某些 Gem 没有 ModuleInterface,而是在其 Module 构造函数中注册组件描述符。无论哪种方式,您都应该将 CreateComponentDescriptors 添加到将组件描述符添加到 AZ::Module m_descriptors的位置。
- 向多人游戏系统注册组件。
例 31.5.对 MyGemSystemcomponent.cpp 的更改
#include <Source/AutoGen/AutoComponentTypes.h>
void MyGemSystemComponent::Activate()
{
// Register multiplayer components
RegisterMultiplayerComponents();
}
- 在 C:\git\book\Gems\MyGem\Code\Source\AutoGen 中创建多人游戏组件 XML 定义。这是一个不需要 C++ 代码的起始多人游戏组件。
例 31.6.MyFirstNetworkComponent.AutoComponent.xml
<?xml version="1.0"?>
<Component
Name="MyFirstNetworkComponent"
Namespace="MyGem"
OverrideComponent="false"
OverrideController="false"
OverrideInclude=""
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</Component>
- 在构建中包含MyFirstNetworkComponent.AutoComponent.xml。
例 31.7.对 Gems\MyGem\Code\mygem_files.cmake 的更改
set(FILES
...
Source/AutoGen/MyFirstNetworkComponent.AutoComponent.xml # new
)
- 重新编译 Gem 和项目。如果您看到类似于以下代码段的 CMake 日志行,则表示您已成功将 Gem 配置为支持多人游戏组件。
2>Generating C:\git\book\build\External\MyGem-409da4e6\
Code\Azcg\Generated\Source\AutoGen\
MyFirstNetworkComponent.AutoComponent.h using
template C:/git/o3de/install/Gems/Multiplayer/
Code/Include/Multiplayer/AutoGen/AutoComponent_Header.jinja
and inputs C:\git\book\Gems\MyGem\Code\Source\
AutoGen\MyFirstNetworkComponent.AutoComponent.xml
从 O3DE 的开发分支构建安装程序
注意:如果您使用的是 O3DE 22.05.0 或更高版本,则可以跳过此部分。
为了获得 O3DE 多人游戏功能的最佳体验,我们必须获得自 O3DE 稳定版 21.11.2 发布以来实施的功能。以下是从 https://github.com/o3de/o3de/tree/development 从头开始创建新安装程序的步骤。
提示:您可以在以下位置在线获取最新的 nightly 预构建安装程序:https://o3debinaries.org/download/windows.html,请查找“Nightly Development Build”。
- 克隆 O3DE 存储库。
git clone https://github.com/o3de/o3de
2. 我假设仓库是在 c:\git\o3de 克隆的。 3. 在 c:\git\o3de\build 中创建一个构建文件夹。 4. 在构建文件夹中,配置 CMake。注意:在写这本书时,我在 2022 年 3 月 26 日的提交 b4b7e5a 处使用 development 分支测试了本书的其余部分。您可以使用以下 git 命令重置到该点。
git reset --hard b4b7e5a
C:\git\o3de\build> cmake -S .. -B .
- 在 c:\git\o3de\build\O3DE.sln 打开 Visual Studio 解决方案。
- 构建 INSTALL 目标。
7. 新引擎安装将位于 c:\git\o3de\install。 8. 打开 C:\git\o3de\install\engine.json。 9. 将 engine_name 和 restricted_name 修改为 “o3de-install”。 10. 在新引擎中配置 python。注意:此步骤需要一段时间,因为它会构建整个引擎和所有 Gem。
C:\git\o3de\install> .\python\get_python.bat
- 注册引擎。
C:\git\o3de\install> .\scripts\o3de.bat register --this-engine
- 打开 C:\Users<user>.o3de\o3de_manifest.json。
- 验证此引擎是否已注册。您应该会看到以下几行。
{
...
"engines": [
"C:/O3DE/21.11.2",
"C:/git/o3de/install",
],
...
"engines_path": {
"o3de-sdk": "C:/O3DE/21.11.2",
"o3de-install": "C:/git/o3de/install",
...
}
- 通过修改 C:\git\book\MyPro ject\project.json 切换 MyProject 以使用这个新引擎。将“engine”从“o3de-sdk”更改为“my-o3de-2111”。
"engine": "o3de-install"
- 重新编译项目。
- 重新运行 Asset Processor。它将重新处理所有资产。
C:\git\o3de\install\bin\Windows\profile\Default\AssetProcessor.exe
--project-path C:\git\book\MyProject\
现在我们可以在 O3DE 中使用多人游戏的最新和最强大的功能!
小结
注意:本章随附的源代码和资源可以在 GitHub 上找到: https://github.com/AMZN-Olex/O3DEBookCode2111/tree/ch31_enable_multiplayer_gem
我们为 MyGem 启用了多人游戏组件的代码生成。现在我们可以自由地开始研究 * 的格式是什么。AutoComponent.xml文件、它们生成的内容以及在接下来的章节中您可以对它们做什么。