Basic knowledge before working with Client-side

Started by Sebastian, May 25, 2022, 11:59 AM

Previous topic - Next topic

Sebastian

If you have never put hands or worked with client-side code, then try to read this, it will help a bit.
Original discord conversation:
Link here



For using client-side, you need a main.nut no matter what, inside store/script/...here
(you can also load compiled .nut files (aka .cnut), but that's another story)
((also, if you need a main.nut doesn't mean you must stick to one file only; you can include( "otherfile.nut" ) / dofile( "otherfile.nut" ) ))

So, inside that main.nut, you have a list of events/functions that you can use here:
http://wiki.thijn.ovh/index.php?title=Client-side_Scripting_Resources

In order to send data from SERVER to CLIENT, OR VICEVERSA, you must write/call Streams
Those streams will be received in
function Server::ServerData( stream )if it's sent by server, and

and
function onClientScriptData( player ) if sent by client



Here is a stream example, from server to client:
Stream.StartWrite();
Stream.WriteInt(0); // key / id
Stream.WriteInt(51); // needed value
Stream.SendStream(player);
and here is a stream e.g. from CLIENT -> SERVER
local Data = Stream();
Data.WriteInt(0); // key / id
Data.WriteString("Explosion"); // needed value
Server.SendData(Data);
those keys I've been put into examples must be considered as cases

Sometimes, you send a stream because you want some data related death messages, but sometimes you send a stream because you want position
well, these 2 cases should have an id like. Why ID ? because they are not so heavy
so, when you send data client->server, in function onClientScriptData( player ) you will be able to do something like this:
function onClientScriptData( player )
{
    local id = Stream.ReadInt();
    switch( id ) {
        case 0: {
            // death message related
            local msg = Stream.ReadString();
            MessagePlayer( "Client data received - death message: " + msg, player );
        } break;

        case 1: {
            // position related
        } break;

        default: {
            // even was called, but with an ID not existing in this switch
        } break;
    }
}

Same goes for client-side, when receives data from server:
function Server::ServerData( stream )
{
    local id = stream.ReadInt();
    switch( id ) {
        case 0: {
            // death message related
            local reasonID = stream.ReadInt();
            Console.Print( "Death Reason ID received from Server: " + reasonID );
        } break;

        case 1: {
            // position related
        } break;

        default: {
            // even was called, but with an ID not existing in this switch
        } break;
    }
}

[spoiler="Stream or stream ??"]
be sure you don't get confused when using stream with big S or small s

if server-side, you need to use Stream with big S.
if client-side, you initiate a custom named/created variable with that Stream() function, like you have seen in the examples above

but if in client-side, using the event Server::ServerData( stream ), then you can see that the parameter is named stream with small s.
[/spoiler]

Anyway, the main idea when sending streams is to go as fast as posible
if you gonna use a string as a key, like you did, then tons of data will be used everytime you send that string key
while you could identify the cases by Integers, or even better: Bytes (as there is also Stream().WriteByte)
and that ^ uses a lot less data, and that's for identification only

habi

I hope this will be useful to someone who needs to know.

sad that the official wiki is down.

Sebastian

Quote from: habi on May 25, 2022, 05:42 PMI hope this will be useful to someone who needs to know.

sad that the official wiki is down.

With some luck, it will be back soon!