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 03-02-2010, 02:36 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Lexically Binding a Function

I was playing with function closures in GS2, trying to make them do something useful, and after hacking away at the callstack for awhile, I finally managed to do this.

PHP Code:
join("utility_bind");
//#CLIENTSIDE
function foobar(temp.atemp.b) {
  
temp.5;
  
temp.baz = function (temp.x) {
    
temp.scope this.getScope();
    return 
temp.scope.temp.scope.temp.scope.temp.x;
  };
  
this.bind(temp.baz);
  return 
temp.baz;
}
function 
onCreated() {
  
temp.this.foobar(510);
  echo(
temp.f(5)); // echos 25

(Beware that copying this directly will add a space after every line, screwing up //#CLIENTSIDE)

Basically, you can bind the current state of the outer function (foobar) to any function you pass (temp.baz), and it will be immutable until the bound function executes this.getScope() and stores it.


The magic is attached.


getCallStack() only works clientside in v6, so you'll need that to see this in action.
Attached Files
File Type: txt utility_bind.txt (246 Bytes, 358 views)

Last edited by WhiteDragon; 03-02-2010 at 05:00 AM..
Reply With Quote
  #2  
Old 03-02-2010, 03:46 AM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
Quote:
Originally Posted by WhiteDragon View Post
For some reason getCallStack() isn't working in v5 so until that is fixed this only works in v6.
getCallStack() works fine for me in v5.
__________________

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts
Reply With Quote
  #3  
Old 03-02-2010, 03:48 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Quote:
Originally Posted by coreys View Post
getCallStack() works fine for me in v5.
Discovered that it is only clientside for v6, but also exists serverside (for both versions obviously).

Last edited by WhiteDragon; 03-02-2010 at 05:00 AM..
Reply With Quote
  #4  
Old 03-02-2010, 05:15 AM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
Quote:
Originally Posted by WhiteDragon View Post
Discovered that it is only clientside for v6, but also exists serverside (for both versions obviously).
Oh, no wonder. I've never tried using it clientside.
__________________

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts
Reply With Quote
Reply


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 09:18 PM.


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