Introduction
This is possibly my most wanted feature in GScript for some time: callstack access.
Callstack access allows you to see which objects and functions are calling other objects or functions. This means you can add better script security by, say, checking which NPCs are trying to call your bank functions, for example. It also can be used for script debugging.
Does it work on all servers yet? Probably not. Try it, and if it doesn't work, you can request your NPC-Server to be updated with it.
How callstack access works
First, let's examine how to get the contents of the callstack.
The
getCallStack() function returns an array of links. The array that
getCallStack() returns is a chronological list of the function call chain.
The important thing to recognise about these links is that they are
not direct links to the calling function or NPC themselves, but instead they are an object linking to the temp. scope of the calling functions. Yes, this means that you can access temp. variables of functions earlier in the call chain. (See section below)
There is also a sub-object named "scriptcallobject", which is a link to the object that is calling the function.
Finally, there is another variable called "name" which returns the name of the calling function.
Code Example
Now, just to make the example a bit more straight-forward, we'll demonstrate this with just two NPCs; TestNPC, and TestNPC2.
Here is the code in TestNPC:
PHP Code:
function onCreated()
{
this.testCallStack();
}
public function testCallStack()
{
temp.callstack = getCallStack();
for (temp.i = temp.callstack.size() - 1; temp.i >= 0; temp.i --)
{
echo("call stack entry " @ temp.i @ ": " @
temp.callstack[temp.i].scriptcallobject.name @ "." @ temp.callstack[temp.i].name);
}
}
(You'll notice that the
for (temp.foo: bar) iterator is not used here, or otherwise you might overwrite temp variables.)
And then TestNPC2:
PHP Code:
function onCreated()
{
TestNPC.testCallStack();
}
Updating the script of TestNPC will reveal this in RC:
Quote:
call stack entry 1: TestNPC.testCallStack
call stack entry 0: TestNPC.onCreated
|
Updating TestNPC2 instead reveals this:
Quote:
call stack entry 1: TestNPC.testCallStack
call stack entry 0: TestNPC.onCreated
|
This shows the order in which functions have been called in order to reach the current function.
Accessing temp variables
Instead of using scriptcallobject, you can access temp names directly, i.e.:
PHP Code:
function onCreated()
{
temp.foo = "two";
this.testCallStack();
}
public function testCallStack()
{
temp.foo = "one";
temp.callstack = getCallStack();
for (temp.i = temp.callstack.size() - 1; temp.i >= 0; temp.i --)
{
echo("variable contents " @ temp.callstack[temp.i].foo);
}
}
Upon updating the script, this will appear in RC:
Quote:
variable contents one
variable contents two
|