BetterTogether 0.8.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package BetterTogether --version 0.8.0                
NuGet\Install-Package BetterTogether -Version 0.8.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BetterTogether" Version="0.8.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BetterTogether --version 0.8.0                
#r "nuget: BetterTogether, 0.8.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BetterTogether as a Cake Addin
#addin nuget:?package=BetterTogether&version=0.8.0

// Install BetterTogether as a Cake Tool
#tool nuget:?package=BetterTogether&version=0.8.0                

BetterTogether

BetterTogether is a simple multiplayer library targeting netstandard2.1, .NET 6, 7 and 8. It is heavily inspired by Playroom which is pretty good and available for most web frameworks/game engines. While Playroom has a Unity SDK, it only support WebGL builds of Unity (for now at least). This project however should run on any platform that supports Net Standard 2.1, .NET 6, 7 or 8.

Features

  • Simple API
  • Support for RPCs
  • More to come (hopefully)

Installation

Get it from NuGet or build it yourself.

dotnet add package BetterTogether

For Unity, use this link in the package manager https://github.com/ZedDevStuff/BetterTogether.git#unity or NugetForUnity which i recommend solely because you can independently update the packages in case you need to.

Usage

Setup

using BetterTogether;

// Setup a server using the fluent API
BetterServer server = new BetterServer()
    .WithMaxPlayers(2)
    .WithAdminPlayers(true) // Allow the server to have admins
    .Start(9050);

// Seting up a client without the fluent API

BetterClient client = new BetterClient();

// Fired when the client is connected to the server
client.Connected += (id, playerList) =>
{
    Console.WriteLine($"Connected as {id}");
    Console.WriteLine("Players:");
    foreach (var player in playerList)
    {
        Console.WriteLine(player);
    }
};

// Fired when another player is connected
client.PlayerConnected += (player) =>
{
    Console.WriteLine($"{player} connected");
};
client.Connect("127.0.0.1", 9050);

State

Objects need to be MemoryPackable. See MemoryPack

// SetPlayerState is used to set a player state. Only the player or the server can modify this state
client.SetPlayerState<string>("Username", "Player1");
// SetState is used to set a global state. Every player can modify this state
client.SetState<string>("foo", "bar");
// Returns the latest state of the key available on this client
client.GetState<string>("foo");
foreach(var player in client.Players)
{
    Console.WriteLine(player, client.GetPlayerState<string>(player, "Username"));
}

RPCs

// Register a RPC
client.RegisterRPC("Hello", (args) =>
{
    string message = MessagePackSerializer.Deserialize<string>(args);
    Console.WriteLine(message);
});

// Call a RPC on every client but this one
client.RpcOthers("Hello", "Hello, World!");

Packets

BetterTogether uses a simple Packet struct to transfer data. If you want to do something on the server with them, you can do so like this

public Packet? HandleData(NetPeer peer, Packet packet)
{
    // Do something with the packet or return null
    // Returning null will discard the packet
    return packet;
}

server.DataReceived = HandleData;

Unity support

Unity behaves differently, especially when it comes to threading. To use BetterTogether in Unity, you need to use some kind of dispatcher like this one inside event handlers. Here is an example using the aforementioned dispatcher

using PimDeWitte.UnityMainThreadDispatcher;

// Usual setup ignored

client.Connected += (id, playerList) =>
{
    // This must be done with any kind of event handling. RPCs for example
    UnityMainThreadDispatcher.Instance().Enqueue(() =>
    {
        Debug.Log($"Connected as {id}");
        Debug.Log("Players:");
        foreach (var player in playerList)
        {
            Debug.Log(player);
        }
    });
};
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.10.0 107 7/7/2024
0.9.1 89 6/22/2024
0.9.0 92 6/22/2024
0.8.2 91 6/22/2024
0.8.1 84 6/21/2024
0.8.0 93 6/21/2024
0.7.0 94 6/21/2024
0.6.0 100 6/20/2024
0.5.2 95 6/20/2024
0.5.1 84 6/20/2024
0.5.0 80 6/20/2024
0.4.1 98 6/20/2024
0.4.0 91 6/19/2024
0.3.0 102 6/18/2024
0.2.0 88 6/18/2024