Vice City: Multiplayer

Server Development => Scripting and Server Management => Client Scripting => Topic started by: Sebastian on Apr 07, 2017, 10:10 PM

Title: [Snippet] Sending data to client/server
Post by: Sebastian on Apr 07, 2017, 10:10 PM
Function for sending data to client/server

I've seen you people using a low-cost function to send data to client/server ...
(which is limited only to few params, that had to be written in a specified way)

But I've created one that will do the job for as many params you want.
I'm using bytes for identification, because they are the most waste-less for the designated task.

SERVER - SendDataToClient( player, byte, ... )
[spoiler=how does it work ?]
this is a server-side function!

Those dots ("...") replaces the unlimited params you can add to this function.
BUT BE AWARE: The first parameter after player must be the identification byte, which you will use in Server::ServerData

E.g.: Sending text, integer, float to client
SendDataToClient( player, 0x01, "blabla", 215, 12.33 )
(red parameter represents the identification byte)

The row of params after byte (in our case: 0x01) doesn't matter, because the function will proceed each one by it's type.
All you have to do is reading the params in client like you used to: in the row you written them.

If you need to send those things to all players online, all you have to do is replacing player with null.
E.g.: SendDataToClient( null, 0x01, "blabla", 215, 12.33 )
[/spoiler]
function SendDataToClient( player, ... )
{
    if( vargv[0] )
    {
        local   byte = vargv[0],
                len = vargv.len();
               
        if( 1 > len ) Message( "ToClent <" + byte + "> No params specified." );
        else
        {
            Stream.StartWrite();
            Stream.WriteByte( byte );

            for( local i = 1; i < len; i++ )
            {
                switch( typeof( vargv[i] ) )
                {
                    case "integer": Stream.WriteInt( vargv[i] ); break;
                    case "string": Stream.WriteString( vargv[i] ); break;
                    case "float": Stream.WriteFloat( vargv[i] ); break;
                }
            }
           
            if( player == null ) Stream.SendStream( null );
            else if( typeof( player ) == "instance" ) Stream.SendStream( player );
            else Message( "ToClient <" + byte + "> Player is not online." );
        }
    }
    else Message( "ToClient: Not even the byte was specified..." );
}

CLIENT - SendDataToServer( byte, ... )
[spoiler=how does it work ?]
this is a client-side function!

It works the same way like SendDataToClient.
The only difference is that you don't need to set the target anymore. (player)

You just jump to the identification byte, then the rest of params.
E.g.: SendDataToServer( 0x02, "message", player.ID, pos.X)
[/spoiler]
function SendDataToServer( ... )
{
    if( vargv[0] )
    {
        local   byte = vargv[0],
                len = vargv.len();
               
        if( 1 > len ) Console.Print( "ToClent <" + byte + "> No params specified." );
        else
        {
            local pftStream = Stream();
            pftStream.WriteByte( byte );

            for( local i = 1; i < len; i++ )
            {
                switch( typeof( vargv[i] ) )
                {
                    case "integer": pftStream.WriteInt( vargv[i] ); break;
                    case "string": pftStream.WriteString( vargv[i] ); break;
                    case "float": pftStream.WriteFloat( vargv[i] ); break;
                }
            }
           
            Server.SendData( pftStream );
        }
    }
    else Console.Print( "ToClient: Not even the byte was specified..." );
}

PS: Don't be a pussy by not asking things you don't understand.
Title: Re: [Snippet] Sending data to client/server
Post by: kennedyarz on Apr 07, 2017, 11:39 PM
Great work @sseebbyy
Title: Re: [Snippet] Sending data to client/server
Post by: Anik on Apr 08, 2017, 09:29 AM
I was looking for something like this. Thanks for it. Keep it up bro. :)