Examples: Difference between revisions

From NPC for VCMP 0.4 Servers
Jump to navigation Jump to search
(Created page with ".")
 
No edit summary
Line 1: Line 1:
.
This page contains example for using Remote Call Functions.
Suppose NPC is connected with ID 0 and RPCLib is loaded in that NPC when connecting.
RFC=Remote Function Call
<source>
ChatFunc<-RFC(0, "SendChat");
ChatFunc("Hello I am an NPC");
</source>
 
Two Parameters
Npc script
<source>
function MyFunc( a, b )
{
...
}
</source>
server script
<source>
MyFuncRemote=RFC(0, "MyFunc");
MyFuncRemote(100, "Okay");
...
MyFuncRemote(200, "Test"); //at a later time.
</source>
or in one line:
<source>RFC(0,"MyFunc")(100, "OKAY");</source>
 
Evaluate function inside function argument
npcscript:
<source>function Test(val1, val2 )
{
print("val1 is "+val1+" and val2 is "+val2+" \n");
}
function Calc(val2)
{
  if(val2>50)return 1;
  else return 0;
}
</source>
server:
<source>
TestRemote<-RFC(0,"Test");
TestRemote(50.0, "Calc(50.0)" ) ==WRONG
TestRemote(50.0, F("Calc")(50.0)) ==RIGHT
</source>
Calling of F means the value to be calculated when it reaches npc side.
 
Another example: Printing NPC's position on console.
server:
<source>RFC(0,"print")("My position is ");
RFC(0,"print")(F("GetMyPos")());
</source>
 
Passing arrays: Huge arrays can also be passed. Raknet takes care of splitting the array into 4 or 5 message fragments and combine them at destination.
npcscript:
<source>
function LenArray(a)
{
print("You passed an array of length "+a.len());
}</source>
server:
<source>
a<-array(1000,500);
RFC(0,"LenArray")(a);</source>
output: (npc console)
<source>You passed an array of length 1000</source>
 
Further advances.
You may create some class which has several members and each members point to a remote function.
<source>
class MyRFuncs
{
chat=null;print=null;command=null;quit=null;
}
</source>
and create an array, say a.
a<-array(100)
and when an npc is joined,
<source>
a[0].print=RFC(0,"print");
a[0].chat=RFC(0,"SendChat");
a[0].command=RFC(0,"SendCommand");
a[0].quit=RFC(0,"QuitServer");
</source>
and later use it like:
<source>a[0].print("This is printed on a console\n"); a[0].chat("Hello everyone");
//or
a[0].quit();
</source>

Revision as of 15:24, 28 January 2023

This page contains example for using Remote Call Functions. Suppose NPC is connected with ID 0 and RPCLib is loaded in that NPC when connecting. RFC=Remote Function Call

ChatFunc<-RFC(0, "SendChat");
ChatFunc("Hello I am an NPC");

Two Parameters Npc script

function MyFunc( a, b )
{
...
}

server script

MyFuncRemote=RFC(0, "MyFunc");
MyFuncRemote(100, "Okay");
...
MyFuncRemote(200, "Test"); //at a later time.

or in one line:

RFC(0,"MyFunc")(100, "OKAY");

Evaluate function inside function argument npcscript:

function Test(val1, val2 )
{
print("val1 is "+val1+" and val2 is "+val2+" \n");
}
function Calc(val2)
{
   if(val2>50)return 1;
   else return 0;
}

server:

TestRemote<-RFC(0,"Test");
TestRemote(50.0, "Calc(50.0)" ) ==WRONG
TestRemote(50.0, F("Calc")(50.0)) ==RIGHT

Calling of F means the value to be calculated when it reaches npc side.

Another example: Printing NPC's position on console. server:

RFC(0,"print")("My position is ");
RFC(0,"print")(F("GetMyPos")());

Passing arrays: Huge arrays can also be passed. Raknet takes care of splitting the array into 4 or 5 message fragments and combine them at destination. npcscript:

function LenArray(a)
{
print("You passed an array of length "+a.len());
}

server:

a<-array(1000,500);
RFC(0,"LenArray")(a);

output: (npc console)

You passed an array of length 1000

Further advances. You may create some class which has several members and each members point to a remote function.

class MyRFuncs
{
chat=null;print=null;command=null;quit=null;
}

and create an array, say a. a<-array(100) and when an npc is joined,

a[0].print=RFC(0,"print");
a[0].chat=RFC(0,"SendChat");
a[0].command=RFC(0,"SendCommand");
a[0].quit=RFC(0,"QuitServer");

and later use it like:

a[0].print("This is printed on a console\n"); a[0].chat("Hello everyone"); 
//or 
a[0].quit();