SDKs

C++ e Unreal Engine

valkyr.hpp, o wrapper C++17 header only sobre a C ABI, com notas para chamá-lo a partir de um servidor dedicado da Unreal Engine.

bindings/cpp/valkyr.hpp é um wrapper C++17 header only sobre o valkyr.h:

  • valkyr::Engine, valkyr::ClientSession, valkyr::ProtectedI64 e valkyr::ProtectedF64 são donos RAII, só de movimento, dos handles C.
  • Strings de entrada e saída são std::string; as strings da biblioteca são copiadas e liberadas para você.
  • Sem exceções: falhas retornam std::nullopt, false ou um código negativo VALKYR_ERR_*, e valkyr::last_error() devolve a mensagem. Isso mantém o wrapper utilizável na Unreal, que compila com exceções desativadas.

Exemplo

example.cppC++
valkyr::Engine engine;if (!engine) {    std::fprintf(stderr, "engine: %s\n", valkyr::last_error().c_str());    return 1;}// A wall 10 m in front of the origin.engine.set_world(R"({"colliders":[{"min":{"x":-1,"y":0,"z":9},"max":{"x":1,"y":3,"z":10}}]})"); // Positions come from an Unreal style world (cm, Z up): 2000 cm per// 100 ms forward is 20 m/s, far above the default limit.int32_t flags = 0;for (uint64_t i = 0; i < 60; ++i) {    valkyr::Vec3 p = valkyr::from_unreal_cm(static_cast<double>(i) * 200.0, 0.0, 0.0);    int32_t n = engine.frame(3, i, i * 100, i * 100 + 20, p.x, p.y, p.z,                             valkyr::yaw_from_unreal(0.0), valkyr::pitch_from_unreal(0.0),                             true, false);    if (n < 0) {        std::fprintf(stderr, "frame: %s\n", valkyr::last_error().c_str());        return 1;    }    flags += n;}std::printf("cpp flags raised: %d\n", flags);if (auto t = engine.trust(3, 6000)) {    std::printf("cpp trust: probability=%.3f trust=%.1f action=%d\n", t->probability, t->trust,                static_cast<int>(t->action));}

Para compilar:

ShellShell
# Windows (x64 Native Tools prompt)cl /std:c++17 /EHsc /I ..\..\crates\valkyr-ffi\include /I . example.cpp ..\..\target\release\valkyr_ffi.dll.lib# Linuxg++ -std=c++17 -I../../crates/valkyr-ffi/include -I. example.cpp ../../target/release/libvalkyr_ffi.a -lpthread -ldl -lm -o example_cpp

Status

O wrapper e o exemplo compilam com MSVC (/std:c++17 /W4 /WX) e rodam em cargo test -p valkyr-ffi --test c_examples. As notas sobre a Unreal abaixo não foram compiladas dentro de um projeto Unreal.

Unreal Engine

A detecção fica no servidor dedicado. Crie um valkyr::Engine por partida, por exemplo na sua subclasse de AGameModeBase, e alimente a partir de:

  • o RPC do servidor que recebe o movimento do cliente, depois que o seu componente de movimento validou: um frame por jogador a cada tick do servidor;
  • o código de armas no lado do servidor: um shot por tiro aceito;
  • RestartPlayer ou HandleStartingNewPlayer: um evento spawn com o time.

Proteja o código com GetNetMode() == NM_DedicatedServer ou HasAuthority() e não inclua o engine nos builds de cliente.

Unidades e eixos

A Unreal usa centímetros e Z para cima, com yaw 0 ao longo de +X. O valkyr usa metros e Y para cima, com yaw 0 ao longo de +Z e 90 ao longo de +X.

mappingText
valkyr.x = Unreal.Y / 100valkyr.y = Unreal.Z / 100valkyr.z = Unreal.X / 100valkyr yaw   = Unreal yaw     (forward maps to forward, right to right)valkyr pitch = Unreal pitch

valkyr::from_unreal_cm, valkyr::yaw_from_unreal e valkyr::pitch_from_unreal implementam essa conversão.

Linkagem

YourGame.Build.csC#
// YourGame.Build.csusing System.IO;using UnrealBuildTool; public class YourGame : ModuleRules{    public YourGame(ReadOnlyTargetRules Target) : base(Target)    {        PCHUsageMode = PCHUsageMode.UseExplicitOrSharedPCHs;        PublicDependencyModuleNames.AddRange(new[] { "Core", "CoreUObject", "Engine" });         string Valkyr = Path.Combine(ModuleDirectory, "..", "ThirdParty", "Valkyr");        PublicIncludePaths.Add(Path.Combine(Valkyr, "include"));        if (Target.Platform == UnrealTargetPlatform.Win64)        {            string Lib = Path.Combine(Valkyr, "lib", "Win64");            PublicAdditionalLibraries.Add(Path.Combine(Lib, "valkyr_ffi.dll.lib"));            PublicDelayLoadDLLs.Add("valkyr_ffi.dll");            RuntimeDependencies.Add("$(BinaryOutputDir)/valkyr_ffi.dll", Path.Combine(Lib, "valkyr_ffi.dll"));        }        else if (Target.Platform == UnrealTargetPlatform.Linux)        {            // Static linking avoids shipping a .so next to the server binary.            PublicAdditionalLibraries.Add(Path.Combine(Valkyr, "lib", "Linux", "libvalkyr_ffi.a"));        }    }}

Com PublicDelayLoadDLLs, carregue a DLL antes da primeira chamada, por exemplo com FPlatformProcess::GetDllHandle no StartupModule do seu módulo.

Editar esta página no GitHub