![]() |
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:
However, an anonymous function has no name: PHP Code:
PHP Code:
This is why they are called "anonymous" functions. To call an anonymous function, you need to do (@variable)(); Here is an example: PHP Code:
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:
PHP Code:
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:
PHP Code:
PHP Code:
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. |
I don't appreciate you negatively speaking about my good friend Door, but nice job nonetheless!
|
I'll be honest with you, this is stupid and makes scripts even harder to read.
|
Quote:
Maybe this will motivate it a little... rewrite the example I gave in the way you'd usually do yourself. |
Quote:
|
Quote:
HTML Code:
function onCreated() { I wouldn't use your script, as for me it just makes GScript more complicated than what it should be. :( Sorry (it is good, but why not just do the loop inside the weapon using functions people can read!) |
Quote:
Quote:
Quote:
|
My behavior trees would not have been possible without anonymous functions. :)
|
Quote:
|
Quote:
|
It takes some getting used to, but I am very happy to have this this. There have been a few times where i might have found this very useful. I'll keep it in mind for the future.
|
Quote:
I don't like how it isn't named, I'd rather replicate the same function in each npc with a clear understanding to the reader what exactly is happening. |
All times are GMT +2. The time now is 01:26 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.