[SNIPPET] Server Echo ( ... )

Started by Sebastian, Feb 25, 2017, 11:02 PM

Previous topic - Next topic

Sebastian

It is a basic thing, but some still might not be aware of it. (like me)
It was done by @happymint in order to help me, but we concluded to share it.

With this function you will be able to print messages into server's console.
:)
Usage: ServerEcho(" test ");


SERVER's NUT
1. Place this in the top
enum StreamType {
    StringEcho = 0xFA
}
2. And this in onClientScriptData( player )
    if (Stream.GetReadSize() < 1) {
        return;
    }
    switch (Stream.ReadByte())
    {
        case StreamType.StringEcho: {
            print(Stream.ReadString());
        } break;
    }


server/store/script/main.nut
3. Place this in client.nut
enum StreamType {
    StringEcho = 0xFA
}

function ServerEcho(...) {
    local m;
    if( vargv.len() <= 0 ) {
        Console.Print( "- ServerEcho() Error - Please specify a message!" );
        return;
    }
    else if (vargv.len() == 1) {
        m = typeof(vargv[0]) == "string" ? vargv[0] : vargv[0].tostring();
    } else {
        vargv.insert(0, this);
        m = format.acall(vargv);
    }
    local s = Stream();
    s.WriteByte(StreamType.StringEcho);
    s.WriteString(m);
    Server.SendData(s);
}

PS: If the param will be simply numbers, they will be auto-convert.
(so ServerEcho(353) will result into "353")

kennedyarz

The truth is that I do not see a necessary use, but good "that we will do" anyway good work: P

jWeb

Quote from: kennedyarz on Feb 26, 2017, 12:27 AMThe truth is that I do not see a necessary use

Actually it may be quicker to notice an error message on the server console instead of having to constantly look in the debuglog.txt whenever it's updated.

Sure it doesn't protect you from compile-time errors but run-time errors can do just fines.

Sebastian

Quote from: jWeb on Feb 26, 2017, 12:31 AMActually it may be quicker to notice an error message on the server console

That's exactly what I needed it for. :)

rulk

very handy indeed, I preferred using the console even back in the 0.3z squirrel days, very kind of you to share.
We are all god's children.

Sebastian

First topic was updated with the fixed version.
There was Server::SendData(s);, while it had to be Server.SendData(s);