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.f = 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.f = function () {
return 10;
};
temp.g = 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.f = 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.f, temp.time) {
(@temp.f)();
temp.time = temp.time - 0.05;
if (temp.time > 0) {
this.scheduleevent(0.05, "LoopIt", temp.f, temp.time);
}
}
Weapon: Slow Door
PHP Code:
this.join("looper");
//#CLIENTSIDE
function onWeaponFired() {
this.ball = showimg(200, "door.png", 20, 20);
temp.f = function () {
this.ball.x += 0.1;
};
this.trigger("LoopIt", temp.f, 5);
}
Weapon: Fast Door
PHP Code:
this.join("looper");
//#CLIENTSIDE
function onWeaponFired() {
this.ball = showimg(200, "door.png", 20, 30);
temp.f = function () {
this.ball.x += 0.2;
this.ball.alpha = random(0,1);
};
this.looper("LoopIt", temp.f, 5);
}
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.