SendServerData: Difference between revisions

From NPC for VCMP 0.4 Servers
Jump to navigation Jump to search
No edit summary
No edit summary
Line 5: Line 5:
desc=This will send data( results in calling onClientScriptData of squirrel-server )|
desc=This will send data( results in calling onClientScriptData of squirrel-server )|
params=<syntaxhighlight lang="lua">(data)</syntaxhighlight>
params=<syntaxhighlight lang="lua">(data)</syntaxhighlight>
<poem>::data: The blob which needs to be formatted as below.
<poem>::data: A blob which is written with one or more of [[WriteByte]], [[WriteInt]], [[WriteFloat]] and [[WriteString]].
Inorder for the squirrel-plugin of server to read what is send :
1. The length of string is written before the string as two bytes.
2. The word written in (1) should be big endian. Here swap2 function can be used (2 bytes-and hence the name)
</poem>|
</poem>|
example=
example=
Line 14: Line 11:
<source lang="lua">
<source lang="lua">
local b=blob();
local b=blob();
 
WriteInt(b, 1000);
b.writen( 1234, 'i' );
WriteByte(b, 100);
 
WriteFloat(b, 356.456);
b.writen( 10, 'b' ); //b - byte
WriteString(b, "Hello");
 
b.writen( swap2(5), 'w' ); //w - word
 
b.writen('h','c'); //c - signed char
b.writen('e','c');
b.writen('l','c');
b.writen('l','c');
b.writen('o','c');
 
b.writen(100.789, 'f'); //f float
 
SendServerData(b);
SendServerData(b);
</source>
</source>
<poem>Output from server</poem>
<poem>Output from server</poem>|
<source lang="lua">
function onClientScriptData(p){
print(Stream.ReadInt());print(Stream.ReadByte());
print(Stream.ReadString());print(Stream.ReadFloat());
}
[SCRIPT]  1234
[SCRIPT]  10
[SCRIPT]  hello
[SCRIPT]  100.789</source>|
retvals=true on success, false on failure( instead of blob, some other instance of a class is passed )|
retvals=true on success, false on failure( instead of blob, some other instance of a class is passed )|
relfuncs=*[[onServerData]]
relfuncs=*[[onServerData]]
}}
}}

Revision as of 05:28, 3 February 2023

This function was added in v1.6 beta and will not work on previous versions.


Description:
This will send data( results in calling onClientScriptData of squirrel-server )


Parameters:

(data)

data: A blob which is written with one or more of WriteByte, WriteInt, WriteFloat and WriteString.


Return Values:
true on success, false on failure( instead of blob, some other instance of a class is passed )


Example

Input npcscript

local b=blob();
WriteInt(b, 1000);
WriteByte(b, 100);
WriteFloat(b, 356.456);
WriteString(b, "Hello");
SendServerData(b);

Output from server

Related Functions

The following functions may be helpful as they relate to this function in one way or another.