SendNetworkString: Difference between revisions

Third example, as well as some tweaks
No edit summary
Tag: Source edit
(Third example, as well as some tweaks)
Tag: Source edit
 
(3 intermediate revisions by the same user not shown)
Line 19: Line 19:


== Examples ==
== Examples ==
{{noexamples}}
Sending a string from the client to the server:
<syntaxhighlight lang="c">
Script "SendStringToServer" (void) CLIENTSIDE
{
    NamedSendNetworkString("ReceiveStringOnServer", "Hello Server");
}
 
Script "ReceiveStringOnServer" (int string) NET
{
    PrintBold(s:"Received '", s:string, s:"' from a client");
}
</syntaxhighlight>
 
And now the other way around (server to client):
<syntaxhighlight lang="c">
Script "SendStringToClient0" (void)
{
    // Since the third argument (client) was specified, the string will only be sent to PlayerNumber 0
    NamedSendNetworkString("ReceiveStringOnClient", "Hello Client 0", 0);
}
 
Script "ReceiveStringOnClient" (int string) CLIENTSIDE
{
    Print(s:"Received '", s:string, s:"' from the server");
}
</syntaxhighlight>
 
Because the server has no clue which player sent the script, we need to append that data to the string:
<syntaxhighlight lang="c">
Script "ClientScript" (void) CLIENTSIDE
{
    // One way to append the PlayerNumber is to write it as a character to the start of the string
    // We need to +1 because 0 would terminate the string early
    NamedSendNetworkString("ReceiveStringOnServer", StrParam(c:(ConsolePlayerNumber()+1), s:"Hello Server"));
}
 
Script "ReceiveStringOnServer" (int packet) NET
{
    int plynum = GetChar(packet, 0)-1;
    str string = StrRight(packet, StrLen(packet)-1);
    Print(s:"Received '", s:string, s:"' from client ", d:plynum);
}
</syntaxhighlight>


[[category:ACS functions]]
[[category:ACS functions]]