GithubHelp home page GithubHelp logo

entwined's Introduction

Entwined

An advanced Bopl Battle networking library.

How to use

Instead of a big text wall on how everything works, there will be an example Hello World program to demonstrate the basic functionality.

[BepInDependency("com.entwinedteam.entwined")]
[BepInPlugin("com.yourname.myFirstEntwinedPlugin", "My First Entwined Plugin", 1.0.0)]
internal class MyPlugin : MonoBehaviour
{
    static EntwinedPacketChannel<string> helloWorldChannel;
    private void Awake()
    {
        helloWorldChannel = new EntwinedPacketChannel<string>(this, new StringEntwiner());

        helloWorldChannel.OnMessage += OnMessage;
    }

    private void OnGUI()
    {
        if (GUI.Button(new Rect(15, 120, 100, 40), "Send Packet"))
        {
            helloWorldChannel.SendMessage("Hello World!");
        }
    }

    private static void OnMessage(string payload, PacketSourceInfo sourceInfo)
    {
        Logger.LogInfo($"{sourceInfo.SenderSteamName}: {payload}");
    }
}

The first line, [BepInDependency("com.entwinedteam.entwined")], ensures that your plugin will load after Entwined.

Next up is:

static EntwinedPacketChannel<string> helloWorldChannel;
private void Awake()
{
    helloWorldChannel = new EntwinedPacketChannel<string>(this, new StringEntwiner());

    helloWorldChannel.OnMessage += OnMessage;
}

This creates a new isolated channel that automatically encodes and decodes strings and adds the function OnMessage. This function is run whenever a client broadcasts a packet on the channel. PacketSourceInfo contains various information about the client who sent the message.

private static void OnMessage(string payload, PacketSourceInfo sourceInfo)
{
    Logger.LogInfo($"{sourceInfo.SenderSteamName}: {payload}");
}

Because we passed StringEntwiner to our channel we do not need to decode a byte array, and can easily log the string to the console.

OnGUI creates a button and runs the following code when it is pressed:

helloWorldChannel.SendMessage("Hello World!");

This automatically encodes the string and broadcasts it to all of the connected clients.

entwined's People

Contributors

codemob-dev avatar

Stargazers

Benjamin M. avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.