LibRPC Examples

From NPC for VCMP 0.4 Servers
Jump to navigation Jump to search

The new version of LibRPC (of this article) is about to be released

New functions Fa, RFCa was added to library for raw calling of functions with a specified environment class or instance. Earlier the 'this' was always the roottable. By using "compilestring", one do not need Fa and RFCa.

The Problem

F("MyFunction")(p1, p2, ... )
On serverside, it is equivalent to
MyFunction.call(getroottable(), p1, p2, ... )

Suppose i want to callMyFunction.call(m, p1, p2, ... ) where m is a table other than roottable. Then it was not possible.
It is for this reason that new function Fa is introduced.

The Working

This leads to error

Fa("MyFunction")(m, p1, p2, ... )

because the key MyFunction will be looked inside table m. So if MyFunction is in roottable, the correct code will be

Fa(F("rawget")("MyFunction"))(m, p1, p2, ... )

When the parameter is not "string", then it will work as expected.

Example One

Let us see an example. Suppose that in roottable of server a table 'C' exists which has a slot named 'a' whose value is 3. Now we are printing that value on server console from npc ( npc must be admin to print. Not only print, but actually for executing any functions remotely ). On serverside, what we want is

print(C.rawget("a"))

Corresponding remote function call would be

RFC("print")( Fa("rawget")( F("rawget")("C"), "a" ) )

Note: F will rawget from roottable while Fa will rawget from its first parameter. F("rawget")("C") will return the table C.

Example Two - Kicking the player

Calling FindPlayer(0).Kick remotely requires

RFCa("Kick")(F("FindPlayer")(0))

Here RFCa ( parallel of RFC, as Fa is to F ) looks for key 'Kick' after pushing it's first 2nd order parameter ( FindPlayer(0) ). Alternatively, you can use this too using KickPlayer

RFC("KickPlayer")(F("FindPlayer")(0))

Tough One

How will you do this:

FindPlayer(0).Cash=2000

?

To understand this, let us see how the 'Cash' function is stored.

print(CPlayer.__setTable.Cash)
[SCRIPT]  (function : 0x033A4CC8)

and

CPlayer.__setTable.Cash.call(FindPlayer(0),2001)
print(FindPlayer(0).Cash)
[SCRIPT]  2001

So we got what we wanted: CPlayer.__setTable.Cash.call(instance, cash ) The corresponding remote code would be (tested):

RFCa(Fa("rawget")(Fa("rawget")(F("rawget")("CPlayer"),"__setTable"),"Cash"))(F("FindPlayer
")(0),2000)