Examples: Difference between revisions
No edit summary |
No edit summary |
||
Line 106: | Line 106: | ||
<source lang="lua"> | <source lang="lua"> | ||
RFC(npcid, F("compilestring")("print(500)"))()</source> | RFC(npcid, F("compilestring")("print(500)"))()</source> | ||
It can be userdata returned by calling F (with two sets of brackets). | <poem>It can be userdata returned by calling F (with two sets of brackets). | ||
'''F inside F''' | '''F inside F': F can be nested''</poem> | ||
<source>RFC(0,"print")(F(F("myfunction")())(999)</source> | <source>RFC(0,"print")(F(F("myfunction")())(999)</source> | ||
is equivalent to <source lang="lua" inline> print( myfunction()(999) )</source> assuming myfunction returns a function, which is called with parameter (999) and whatever this second function returns is printed. | is equivalent to <source lang="lua" inline> print( myfunction()(999) )</source> assuming myfunction returns a function, which is called with parameter (999) and whatever this second function returns is printed. |
Revision as of 17:54, 13 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();
6. Very advanced This is the highest point of RFC or F.
local script=compilestring("print(500)"); script();
will output:500
But how do you call it remotely?
compilestring("print(500)")()
F("compilestring")("print(500)") == WON'T WORK RFC(npcid, "print")(F("compilestring")("print(500)") == NICE TRY, BUT WON't WORK
The second parameter of RFC can not only be string !
RFC(npcid, F("compilestring")("print(500)"))()
It can be userdata returned by calling F (with two sets of brackets).
'F inside F': F can be nested
RFC(0,"print")(F(F("myfunction")())(999)
is equivalent to print( myfunction()(999) )
assuming myfunction returns a function, which is called with parameter (999) and whatever this second function returns is printed.