I decided to release these because I think they may help people better understand how to use the callstack. The callstack allows you to trace what functions were calling what in order to reach the current place in the script. For instance, if you have three functions and they all call each other, they will appear in the callstack in that order of execution.
This is very simple code that gives you control over what functions are allowed to call what, or simply to just find out more about the calling NPC/function in case you want to send back callback events. On my server I have had this in a class called "security", since this is probably the easiest way of doing it.
Simply paste the following code into the "security" class (or whatever you want to name it) on your server, and then in the NPCs you want to use it in, just use
this.join("security"); and then you can use the functions.
Once the class has been joined, the following functions are available:
- getCallingFunction(): returns the calling function
- getCallingObject(): returns the calling object
- getOriginalFunction(): returns the original calling function
- getOriginalObject(): returns the original calling object
For example, we will assume we have four functions,
onCreated(),
function1(),
function2() and
function3(), where
onCreated calls 1, 1 calls 2, and 2 calls 3.
The functions
getCallingFunction() and
getCallingObject() return the function/object of the function that is
directly calling the function you are in now (in the example, in
function3, these functions will get
function2).
The functions
getOriginalFunction() and
getOriginalObject() instead get the first function that was
called (in the example, these will get
onCreated regardless of whether used from
function1,
function2 or
function3).
This allows you to do things like:
PHP Code:
if ("something" in this.getCallingObject().joinedclasses)
... to see if the calling object has a certain class joined to it.
PHP Code:
if (this.getOriginalObject().name == "DB_Something" &&
this.getOriginalFunction().name == "onCreated")
... to enforce that the chain of functions can only be called originally from
DB_Something.onCreated().
PHP Code:
temp.obj = this.getOriginalObject();
temp.obj.trigger("Event", parameters);
temp.obj.trigger("OtherEvent", parameters);
... to send events or function calls to the original calling NPC.
The
getCallingFunction() and
getCallingObject() functions have an
optional parameter "offset", which lets you offset a number of steps further back into the callstack. For instance, if you want to have your callstack checking in a separate function in your code, you might need to use an offset of 1 (i.e.
this.getCallingObject(1)) so that you are excluding your new function from the callstack check.
Anyway, here's the code for the class below. Enjoy.
PHP Code:
// Scripted by Skyld
function getCallingFunction(temp.offset)
{
temp.callstack = getcallstack();
if (temp.offset < 0)
{
temp.offset = 0;
}
if (temp.callstack.size() < (temp.offset + 3))
{
// the stack is not big enough, usually this means
// that there is no previous function call
return false;
}
return temp.callstack[temp.callstack.size() - 3 - temp.offset];
}
function getCallingObject(temp.offset)
{
temp.callstack = getcallstack();
if (temp.offset < 0)
{
temp.offset = 0;
}
if (temp.callstack.size() < (temp.offset + 3))
{
// the stack is not big enough, usually this means
// that there is no previous function call
return false;
}
return temp.callstack[temp.callstack.size() - 3 - temp.offset].scriptcallobject;
}
function getOriginalFunction()
{
temp.callstack = getcallstack();
return temp.callstack[0];
}
function getOriginalObject()
{
temp.callstack = getcallstack();
return temp.callstack[0].scriptcallobject;
}