SendNetworkString: Difference between revisions

From Zandronum Wiki
(3.1 is released, so Devfeature templates have been removed)
(Third example, as well as some tweaks)
Tag: Source edit
 
(4 intermediate revisions by 2 users not shown)
Line 7: Line 7:
Sends an ACS string from the server to the client(s), or from a client to the server, then executes a script on the receiver's end. If run offline, the script will be executed on the local machine like normal. Note that just like with RequestScriptPuke and NamedRequestScriptPuke, there is no guarantee that the server will receive the string from the client. If the packet containing the string is lost, the server will not receive it at all. Therefore, modders should anticipate the possibility that a string might not be received and should try to rectify the problem if necessary.
Sends an ACS string from the server to the client(s), or from a client to the server, then executes a script on the receiver's end. If run offline, the script will be executed on the local machine like normal. Note that just like with RequestScriptPuke and NamedRequestScriptPuke, there is no guarantee that the server will receive the string from the client. If the packet containing the string is lost, the server will not receive it at all. Therefore, modders should anticipate the possibility that a string might not be received and should try to rectify the problem if necessary.


==Parameters==
=== Parameters ===
*script: The script to be run on the receiver's end upon receiving the string.
* ''script'': The script to be run on the receiver's end upon receiving the string.
*string: The string that will be sent across the network.
* ''string'': The string that will be sent across the network.
*client: If specified, the string will only be sent to this client. By default, this is -1 which means that all clients receive the string. This only matters when called by the server.
* ''client'': If specified, the string will only be sent to this client. By default, this is -1 which means that all clients receive the string. This only matters when called by the server.


== Return Value ==
=== Return value ===
Returns 1 if the string could be sent successfully, or 0 if wasn't. The function might explicitly fail for a couple of reasons:
Returns 1 if the string could be sent successfully, or 0 if wasn't. The function might explicitly fail for a couple of reasons:
* The script doesn't exist.
* The script doesn't exist.
Line 18: Line 18:
* If called on the client's end, the script doesn't have the <tt>NET</tt> flag.
* If called on the client's end, the script doesn't have the <tt>NET</tt> flag.


[[category:ACS Functions]]
== 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">
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]]

Latest revision as of 13:01, 22 April 2023

This article documents a Zandronum-specific ACS feature which may not be supported by ZDoom and its other child ports.

int SendNetworkString (int script, str string [, int client])

int NamedSendNetworkString (str script, str string [, int client])

Usage

Sends an ACS string from the server to the client(s), or from a client to the server, then executes a script on the receiver's end. If run offline, the script will be executed on the local machine like normal. Note that just like with RequestScriptPuke and NamedRequestScriptPuke, there is no guarantee that the server will receive the string from the client. If the packet containing the string is lost, the server will not receive it at all. Therefore, modders should anticipate the possibility that a string might not be received and should try to rectify the problem if necessary.

Parameters

  • script: The script to be run on the receiver's end upon receiving the string.
  • string: The string that will be sent across the network.
  • client: If specified, the string will only be sent to this client. By default, this is -1 which means that all clients receive the string. This only matters when called by the server.

Return value

Returns 1 if the string could be sent successfully, or 0 if wasn't. The function might explicitly fail for a couple of reasons:

  • The script doesn't exist.
  • If called on the server's end, the client doesn't exist or the script doesn't have the CLIENTSIDE flag.
  • If called on the client's end, the script doesn't have the NET flag.

Examples

Sending a string from the client to the server:

Script "SendStringToServer" (void) CLIENTSIDE
{
    NamedSendNetworkString("ReceiveStringOnServer", "Hello Server");
}

Script "ReceiveStringOnServer" (int string) NET
{
    PrintBold(s:"Received '", s:string, s:"' from a client");
}

And now the other way around (server to client):

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");
}

Because the server has no clue which player sent the script, we need to append that data to the string:

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);
}