Examples: Difference between revisions
No edit summary |
No edit summary |
||
Line 94: | Line 94: | ||
<source lang="lua"> | <source lang="lua"> | ||
local script=compilestring("print(500)"); | local script=compilestring("print(500)"); | ||
script();</source> | script();</source>will output:<source lang="lua" inline>500</source> | ||
will output: | <poem> | ||
<source lang="lua" inline>500</source> | But how do you call it remotely? | ||
<source lang="lua" inline>compilestring("print(500)")()</source> | |||
</poem> |
Revision as of 17:42, 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)")()