SDKs

C# and Unity

Valkyr.cs, the single file P/Invoke wrapper for .NET and Unity, and an example MonoBehaviour for a Unity dedicated server.

bindings/csharp/Valkyr.cs is a single file P/Invoke wrapper over valkyr.h:

  • Engine, ClientSession, ProtectedLong and ProtectedDouble each own a SafeHandle and free the native object on Dispose, with the finalizer as a fallback.
  • Errors throw ValkyrException with the library's message.
  • Strings are marshaled as UTF-8 by hand, so .NET Framework, .NET and Mono behave the same.
  • Native exposes the raw entry points if you prefer them.

It sticks to C# 7.3 so Unity's compiler accepts it.

Status

Valkyr.cs was compiled with the Roslyn compiler against .NET Framework 4.8.1 and passes its smoke test. The Unity example below needs UnityEngine and was not compiled here.

Unity dedicated server

Put Valkyr.cs and the example in Assets/, and the native library in Assets/Plugins/x86_64/ (valkyr_ffi.dll on Windows, libvalkyr_ffi.so on Linux servers). Feed only state the server owns or validated.

ValkyrServerExample.csC#
    private void Awake()    {#if !UNITY_SERVER && !UNITY_EDITOR        // Detection belongs on the server. A client build has nothing to judge.        enabled = false;        return;#else        engine = new Engine(string.IsNullOrEmpty(configJson) ? null : configJson);        Debug.Log("valkyr " + ValkyrLib.Version + " ready");#endif    }     private void FixedUpdate()    {        if (engine == null) return;        ulong now = (ulong)clock.ElapsedMilliseconds;        foreach (var p in players)        {            if (p.body == null) continue;            Vector3 pos = p.body.position;            double yaw = Coords.YawFromUnityEulerY(p.body.eulerAngles.y);            double pitch = p.head != null ? Coords.PitchFromUnityEulerX(p.head.localEulerAngles.x) : 0.0;            bool grounded = p.controller != null && p.controller.isGrounded;            // client_ms: use the timestamp the client sent with its input            // when you have one; the server time is a stand in here.            int flags = engine.Frame(p.playerId, p.seq++, now, now, pos.x, pos.y, pos.z, yaw, pitch,                grounded, false);            if (flags > 0)            {                OnVerdicts(p, engine.LastVerdicts());            }        }    }     /// <summary>Call this from your weapon code when the server accepts a shot.</summary>

Unity is already meters and Y up, and its euler yaw matches valkyr (0 along +Z, 90 along +X). Only the pitch sign differs: Unity euler x is positive when looking down; Coords.PitchFromUnityEulerX converts it.

Smoke test

ShellShell
cargo build -p valkyr-fficsc -nologo -platform:x64 -out:SmokeTest.exe Valkyr.cs tests/SmokeTest.cscopy ..\..\target\debug\valkyr_ffi.dll .SmokeTest.exe

Without native code

If you would rather not load a native library into the game server, run valkyr-server next to it and post events over HTTP, as the Godot bridge does. The .NET SDK in the integrations project wraps that API.

Edit this page on GitHub