Examples: Difference between revisions
No edit summary |
No edit summary |
||
Line 2: | Line 2: | ||
Suppose NPC is connected with ID 0 and RPCLib is loaded in that NPC when connecting. | Suppose NPC is connected with ID 0 and RPCLib is loaded in that NPC when connecting. | ||
RFC=Remote Function Call | RFC=Remote Function Call | ||
1. Calling SendChat | |||
<source> | <source> | ||
ChatFunc<-RFC(0, "SendChat"); | ChatFunc<-RFC(0, "SendChat"); | ||
Line 7: | Line 8: | ||
</source> | </source> | ||
Two Parameters | 2. Two Parameters | ||
Npc script | Npc script | ||
<source> | <source> | ||
Line 25: | Line 26: | ||
<source>RFC(0,"MyFunc")(100, "OKAY");</source> | <source>RFC(0,"MyFunc")(100, "OKAY");</source> | ||
Evaluate function inside function argument | 3. Evaluate function inside function argument | ||
npcscript: | npcscript: | ||
<source>function Test(val1, val2 ) | <source>function Test(val1, val2 ) | ||
Line 45: | Line 46: | ||
Calling of F means the value to be calculated when it reaches npc side. | Calling of F means the value to be calculated when it reaches npc side. | ||
Another example: Printing NPC's position on console. | 4. Another example: Printing NPC's position on console. | ||
server: | server: | ||
<source>RFC(0,"print")("My position is "); | <source>RFC(0,"print")("My position is "); | ||
Line 51: | Line 52: | ||
</source> | </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. | 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: | npcscript: | ||
<source> | <source> |
Revision as of 04:32, 2 February 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 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();