OnServerData: Difference between revisions

From NPC for VCMP 0.4 Servers
Jump to navigation Jump to search
(Created page with "<poem style="border: 2px solid #d6d2c5; background-color: #f9f4e6; padding: 1em;"> This function was added in v1.6 beta and will not work on previous versions. </poem>{{Welco...")
 
No edit summary
 
(9 intermediate revisions by the same user not shown)
Line 4: Line 4:
desc=This function is when server sends script data( Streams in squirrel04relxx )|
desc=This function is when server sends script data( Streams in squirrel04relxx )|
params=<syntaxhighlight lang="lua">( data )</syntaxhighlight>
params=<syntaxhighlight lang="lua">( data )</syntaxhighlight>
<poem>::data :  The data the server send as a blob object.
<poem>::data :  The data the server send as a blob object</poem>
</poem>|
|
retvals=This function does not return any specific values.|
retvals=This function does not return any specific values.|
example=
example=
<poem>Server code</poem>
<poem>Assuming the server squirrel script send an integer, byte, float and string respectively as first part of Server Data,</poem>
<source lang="lua">
<source lang="lua">
//scripts/main.nut
function OnServerData( data )
Stream.StartWrite();
Stream.WriteInt(400);
Stream.WriteByte(100);
Stream.WriteString("hello");
Stream.WriteFloat(28.24);
Stream.SendStream(player);
</source>
<poem>
</poem>
<poem>npc code </poem>
<source lang="lua">
function OnServerScriptData( data )
{
{
local len=data.readn('i');
local a= ReadInt(data);
len=swap4(len);//because squirrel plugin sends it inverted.
    local b= ReadByte(data);
local _integer=data.readn('i');
    local f= ReadFloat(data);
local _byte=data.readn('b');
    local str= ReadString(data);
local _strlen=data.readn('w');
    if(data.tell() < data.len())
_strlen=swap2(_strlen);
    //more items to read
local string="";
for(local i=0;i<_strlen;i++)
string+=format("%c",data.readn('c'));
 
local _float = data.readn('f');
printf("Received\n"+_integer+"\n"+_byte+"\n"+string+"\n"+_float+"\n");
}
}
</source>
</source>|
<poem>Output in npc window</poem>
relfuncs=*[[SendServerData]]
<source>Received
400
100
hello
28.24
</source>
}}
}}

Latest revision as of 11:12, 28 September 2024

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

Description:
This function is when server sends script data( Streams in squirrel04relxx )


Parameters:

( data )

data : The data the server send as a blob object


Return Values:
This function does not return any specific values.


Example

Assuming the server squirrel script send an integer, byte, float and string respectively as first part of Server Data,

function OnServerData( data )
{
	local a= ReadInt(data);
    local b= ReadByte(data);
    local f= ReadFloat(data);
    local str= ReadString(data);
    if(data.tell() < data.len())
    //more items to read
}

Related Functions

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