Graal Forums  

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

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #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
 


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:42 PM.


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