The starting point of this tutorial is the completed Lesson 2 of the Pirates tutorial. If you already have a project at this point or want to follow the steps yourself in the other tutorial, you're free to do so.
It's best to clone the starting point from the C++ worker tutorial
repository, build it by running
spatial worker build UnityClient UnityWorker, and check it
works.
Here are some basic shell commands to set up everything described in the sections below. This is just provided for your convenience so that you don't have to move files around and change names. Before you run them, read through the rest of this page and make sure you know what you use the commands for. Based on your environment, you might need to make some tweaks to the commands.
This assumes you are in root directory of the tutorial project, have
Git, and building this on Windows. To build for a
different platform just change the spatial worker build command at the end:
DEST_PROJECT=`pwd`
# Go to the parent directory of Pirates
cd ..
# Clone C++ blank project
git clone git@github.com:spatialos/CppBlankProject.git
cd CppBlankProject
chmod +x worker_create.sh
./worker_create.sh Managed PirateShipMovement $DEST_PROJECT
# Build the new worker
spatial worker build PirateShipMovement --target=windows
# Go back to project root
cd ../..- Clone the C++ blank project.
- Copy the
workers/Manageddirectory to theworkers/PirateShipMovementdirectory of Pirates. - Rename the worker configuration file
spatialos.Managed.worker.jsontospatialos.PirateShipMovement.worker.json. - Find and replace all occurrences of "Managed" (case-sensitive) with "PirateShipMovement" in
the directory you copied:
- In
spatialos.PirateShipMovement.worker.json- multiple occurrences. - In
CMakeLists.txt - In
build.json - In
src/startup.cc
- In
- Copy the
CppBlankProject/schema/CMakeLists.txttoPiratesTutorial/schema/CMakeLists.txt. - Remove the
blank.schemaand generated sources for theblankcomponent.
The file should look like:
# This script is included by worker and library builds
# It is not meant to be built as a standalone library
set(GENERATED_CODE_DIR "${APPLICATION_ROOT}/generated_code/cpp")
# Schema generated code in library
set(SCHEMA_FILES
)
set(SCHEMA_SOURCE_FILES
"${GENERATED_CODE_DIR}/improbable/standard_library.cc"
"${GENERATED_CODE_DIR}/improbable/standard_library.h"
)
source_group(schema "${CMAKE_CURRENT_SOURCE_DIR}/[^/]*")
source_group(generated_code\\schema "${GENERATED_CODE_DIR}/[^/]*")
source_group(generated_code\\improbable "${GENERATED_CODE_DIR}/improbable/[^/]*")
add_library(Schema STATIC ${SCHEMA_FILES} ${SCHEMA_SOURCE_FILES})
target_include_directories(Schema SYSTEM PUBLIC "${GENERATED_CODE_DIR}")
target_link_libraries(Schema PRIVATE WorkerSdk)
In the next part of the tutorial you will add the sources for the
ShipControls component.
The worker project you copied earlier already contains a
spatialos_worker_packages.json with the required packages for the C++ SDK. You need
to make the SDK available to the worker executable when compiling.
- Create a new directory
PiratesTutorial/dependencies. - Copy the
CppBlankProject/dependencies/CMakeLists.txttoPiratesTutorial/dependencies/CMakeLists.txt.
If your project is under source control, add the following directories to your ignored files:
# Worker SDK
dependencies/worker_sdk/
# Generated code from schema
generated_code/
# Cache from generated code and other metadata
.spatialos/
To make sure everything was added correctly, build the worker you added. The
target value will be one of windows, linux, or macos based on your
platform:
# On Windows
spatial worker build PirateShipMovement --target=windows
# On Linux
spatial worker build PirateShipMovement --target=linux
# On MacOS
spatial worker build PirateShipMovement --target=macosThe first time you build there will be many warnings about missing pdb files
from some of the libraries used in the C++ SDK - you shouldn't worry about
this. If the command succeeds, you are ready to continue with the
next lesson.
Note that you won't be able to simply use
spatial worker buildto build all your workers anymore. Because the targets of the C++ worker build depend on the platform, you normally won't be able to build for all platforms at the same time.If you really want to, you can achieve this by setting up cross-compiling in CMake. As an aside, it is highly unlikely you will often need to build all workers of different types. Usually, you will only have changes that require rebuilding a certain type of worker. For this tutorial use:
spatial worker build UnityWorker UnityClientor
spatial worker build PirateShipMovement -t=<platform>to build your workers. See
spatial worker build(SpatialOS documentation) for more details.