Code Monkey home page Code Monkey logo

Comments (3)

WopsS avatar WopsS commented on September 26, 2024

Hello,

Yes it is possible you need to write an enum with all your possible values, e.g.:

enum PossibleTypes
{
    Type_Int,
    Type_Double,
    Type_Bool

    // etc..
}

after that you need to write a byte from enum to message before you write a value (integer, double, float, bool, etc.), e.g.: (Note: This is for client-side.)

NetOutgoingMessage netOutgoingMessage = this.netClient.CreateMessage();

netOutgoingMessage.Write((byte)PossibleTypes.Type_Int);
netOutgoingMessage.Write(30);
netOutgoingMessage.Write((byte)PossibleTypes.Type_Double);
netOutgoingMessage.Write(3890.6879);
netOutgoingMessage.Write((byte)PossibleTypes.Type_Bool);
netOutgoingMessage.Write(true);
netOutgoingMessage.Write((byte)PossibleTypes.Type_Double);
netOutgoingMessage.Write(58.65);
netOutgoingMessage.Write((byte)PossibleTypes.Type_Int);
netOutgoingMessage.Write(78);

// TODO: Write your custom values.

this.netClient.SendMessage(netOutgoingMessage, NetDeliveryMethod.ReliableOrdered);

On the server side or other client side you can read them by reading the first byte and cast it to enum value after that read the next bytes for the specific type, e.g.:

while (netIncomingMessage.PositionInBytes < netIncomingMessage.LengthBytes - 1)
{
    PossibleTypes Type = (PossibleTypes)netIncomingMessage.ReadByte();

    if (Type == PossibleTypes.Type_Int)
    {
       Console.WriteLine(netIncomingMessage.ReadInt32());
    }
    else if (Type == PossibleTypes.Type_Double)
    {
        Console.WriteLine(netIncomingMessage.ReadDouble());
    }
    else if (Type == PossibleTypes.Type_Bool)
    {
        Console.WriteLine(netIncomingMessage.ReadBoolean().ToString());
    }
}

In the end you can create a new message and send it to other clients (that on server side).

from lidgren-network-gen3.

MaZyGer avatar MaZyGer commented on September 26, 2024

Thank you very much. I thought about something like that but I didn't know how to know how many incoming messages holds types in it.
But I see it now
while (netIncomingMessage.PositionInBytes < netIncomingMessage.LengthBytes - 1)

from lidgren-network-gen3.

WopsS avatar WopsS commented on September 26, 2024

@MaZyGer with pleasure :)

from lidgren-network-gen3.

Related Issues (20)

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.