Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-15-2010, 03:37 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Post Script callstack functions

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() - 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() - temp.offset].scriptcallobject;
}

function 
getOriginalFunction()
{
  
temp.callstack getcallstack();
  
  return 
temp.callstack[0];
}

function 
getOriginalObject()
{
  
temp.callstack getcallstack();
  
  return 
temp.callstack[0].scriptcallobject;

__________________
Skyld
Reply With Quote
  #2  
Old 06-16-2010, 06:21 AM
Switch Switch is offline
o.o
Switch's Avatar
Join Date: Jan 2007
Location: Philadelphia
Posts: 3,038
Switch has a spectacular aura about
Send a message via MSN to Switch
Could be very useful for security.
__________________
Oh squiggly line in my eye fluid. I see you lurking there on the peripheral of my vision.
But when I try to look at you, you scurry away.
Are you shy, squiggly line?
Why only when I ignore you, do you return to the center of my eye?
Oh, squiggly line, it's alright, you are forgiven.
Reply With Quote
Reply

Tags
callstack


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 12:10 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.