SendNetworkString: Difference between revisions
Third example, as well as some tweaks
m (Tweaked example slightly) |
(Third example, as well as some tweaks) Tag: Source edit |
||
(One intermediate revision by the same user not shown) | |||
Line 19: | Line 19: | ||
== Examples == | == Examples == | ||
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"> | <syntaxhighlight lang="c"> | ||
Script "ClientScript" (void) CLIENTSIDE | Script "ClientScript" (void) CLIENTSIDE | ||
{ | { | ||
// | // One way to append the PlayerNumber is to write it as a character to the start of the string | ||
NamedSendNetworkString("ReceiveStringOnServer", "Hello Server"); | // We need to +1 because 0 would terminate the string early | ||
NamedSendNetworkString("ReceiveStringOnServer", StrParam(c:(ConsolePlayerNumber()+1), s:"Hello Server")); | |||
} | } | ||
Script "ReceiveStringOnServer" (int | Script "ReceiveStringOnServer" (int packet) NET | ||
{ | { | ||
Print(s:"Received '", s: | int plynum = GetChar(packet, 0)-1; | ||
str string = StrRight(packet, StrLen(packet)-1); | |||
Print(s:"Received '", s:string, s:"' from client ", d:plynum); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[category:ACS functions]] | [[category:ACS functions]] |