[Script Module] SendClientData / SendServerData

DizzasTeR

  • VC:MP Beta Tester
  • Infinite loops defy infinity by overflowing memory
  • Posts: 1,147
[Script Module] SendClientData / SendServerData
«  »Last edited
Easily send data from server to client(s) or from client to server. Without having to use a thousand "Stream Types" or using stupid concatenations of strings and splitting.

This can work for official plugin as well but you will have to do some necessary editing (which is a few lines, I might look into that and do it myself later perhaps)

Use it carefully, it COULD have issues, I did as much thorough testing as I could, but still.

Supports:
- Simple data types (integers, strings, bool, null)
- Static arrays (These are arrays made via local myVar = [];)
- Tables

— Server module
Attachment file: SqClient
Provides function: SendClientData( [Array of players to send it to, empty array = send to all], param1, param2, ... );

Load this file from sqmod.ini
Note: You need to have this function somewhere defined, from this you can extract data, and even setup custom signal calls
Code: [Select]
function onServerReceiveClientData(player, ...) {
local data = vargv;
SqLog.Inf("onServerReceiveClientData");
}

— Client module
Attachment file: DClientData.nut
Provides function: SendServerData( param1, param2, ... );

Load this file as first from every other client file (Thats how I did my testings) — include("DClientData.nut");

Note: Make sure you setup a stream type like this for the magic to happen:
Code: [Select]
function Server::ServerData( stream )
{
    // here you receive the data you send from server
    local streamType = stream.ReadInt();
    switch( streamType )
    {
        case 0x64697a7a:
        {
onClientDecodeData(stream);
        }
        break;
    }
}

Note: Make sure you have this function defined somewhere in client side:
Code: [Select]
function onClientReceiveData(...) {
}

— Usage

Server:
Code: [Select]
SqCore.On().PlayerSpawn.Connect(function(player, ...) {
local Table = {
DizzasTeR = "IQ 900",
IQ = 900,
Abuse = ["This is too much"]
};
local Array = [Table, "Client System", "Swag Level", 9000];
SendClientData([player], "Request", "Advanced", 100, Array);
});

function onServerReceiveClientData(player, ...) {
local data = vargv;
// Going through the data array and extracting data
}

Client
Code: [Select]
function onClientReceiveData(...) {
local param1 = vargv[0];
local ResponseTable = {
Message = "Sending data to server!",
MyCode = 1
}
if(param1 == "Request") {
SendServerData("Response", ResponseTable);
}
}

In Action



("Request" + "Advanced" + 100 + Array = 4 elements)


("Response" + ResponseTable = 2 elements)

P.S: That stream type number is not random :D