Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > New Scripting Engine (GS2)
FAQ Members List Calendar Today's Posts

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 01-09-2008, 12:28 AM
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
Introducing Callstack Access

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.temp.callstack.size() - 1temp.>= 0temp.--)
  {
    echo(
"call stack entry " temp.": " 
      
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.temp.callstack.size() - 1temp.>= 0temp.--)
  {
    echo(
"variable contents " temp.callstack[temp.i].foo);
  }

Upon updating the script, this will appear in RC:
Quote:
variable contents one
variable contents two
Reply With Quote
 


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 01:16 PM.


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