Graal Forums  

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

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 05-03-2010, 11:10 PM
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
An Intro to Anonymous Functions

This is something that I've been ranting about for awhile on the forums, so I'll be writing a little post to let everyone know about them.

Anonymous Function? What's that?

Usually, you see functions like this:
PHP Code:
function onCreated() {
}

function 
myExampleFunction() {
  return 
10;

These functions have names: onCreated and myExampleFunction.


However, an anonymous function has no name:
PHP Code:
function onCreated() {
  
temp.= function () {
    return 
10;
  };

Here, temp.f is not the function's name, it is just a pointer to it. You could easily do something like this
PHP Code:
function onCreated() {
  
temp.= function () {
    return 
10;
  };
  
temp.temp.f;

Now what is the function's name? Both temp.g and temp.f point to the same function.

This is why they are called "anonymous" functions.


To call an anonymous function, you need to do (@variable)();

Here is an example:
PHP Code:
function onCreated() {
  
temp.= function () {
    return 
10;
  };
  
temp.output = (@temp.f)();

After this runs, temp.output would equal 10.

Note: there reason for that crazy calling syntax is because of a bug where calling the function after doing certain things to it breaks. Doing it this way guarantees that it'll run. Help me ask Stefan if you want it fixed. ;-)




Is there anything different about anonymous functions besides not having a name?

The cool part about anonymous functions is that you can pass them to other functions.

For example:

Normal coding
PHP Code:
function onCreated() {
  
temp.something 10;
  
this.example(temp.something);
}
function 
example(temp.whatever) {
  echo(
temp.whatever);

Let's use anonymous functions!
PHP Code:
function onCreated() {
  
temp.something = function () {
    return 
10;
  };
  
this.example(temp.something);
}
function 
example(temp.whatever) {
  echo((@
temp.whatever)());


Now, this does absolutely nothing different from the first version except making you type more.

But it should be clear that you can pass around the function you create, then call it somewhere else.



So what do I use this for?

Separating your code into pieces (also known as abstraction), in order to get rid of duplicate code.



I'll just hit you right off the bat with an example:

One class, and two weapons...


Class: looper
PHP Code:
//#CLIENTSIDE
function onLoopIt(temp.ftemp.time) {
  (@
temp.f)();
  
temp.time temp.time 0.05;
  if (
temp.time 0) {
    
this.scheduleevent(0.05"LoopIt"temp.ftemp.time);
  }

Weapon: Slow Door
PHP Code:
this.join("looper");
//#CLIENTSIDE
function onWeaponFired() {
  
this.ball showimg(200"door.png"2020);
  
temp.= function () {
    
this.ball.+= 0.1;
  };
  
this.trigger("LoopIt"temp.f5);

Weapon: Fast Door

PHP Code:
this.join("looper");
//#CLIENTSIDE
function onWeaponFired() {
  
this.ball showimg(200"door.png"2030);
  
temp.= function () {
    
this.ball.+= 0.2;
    
this.ball.alpha random(0,1);
  };
  
this.looper("LoopIt"temp.f5);

Slow Door would show an image and move it for 5 seconds.
Fast Door would show an image and move it twice as fast, for 5 seconds, and PULSE LIKE CRAZY!

Yes, I just abstracted a timeout/scheduleevent out of my code. You can use this technique anywhere you want, to extract code from any portion of your script and store in a reusable form (such as a class).


If you want an exercise, start with the previous code, and make the looper call a function (which you would pass in) when it is done looping. That code could hide the ball, for example.





This alone is already very powerful, but it you can do even more with it come v6, and I'll write an article for that when it comes out.


Feel free to ask any questions.

Last edited by WhiteDragon; 05-04-2010 at 12:35 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 11:48 PM.


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