Examples

From NPC for VCMP 0.4 Servers
Revision as of 04:36, 2 February 2023 by Habi (talk | contribs)
Jump to navigation Jump to search

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
1. Calling SendChat

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

2. 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");

3. 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.

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

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

5. 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();