The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
SlightlyLoony
Tera Contributor

find_real_file.pngShe seems a bit skeptical about this, but it's true — starting with the Fall 2010 release, you'll be able to define functions whose definitions are automatically included when you use them.

This is much different than a feature you may already be familiar with: global business rules. All global business rules are loaded (included) before you run any script. The more global business rules you define, the less efficient this process is.

On-demand functions, on the other hand, are only loaded (included) when you actually use them in a script. That means you can define as many of them as you'd like without any loss of efficiency.

So how does this work?

Suppose you're taking a computer science course at your neighborhood bar, and during the section titled Margaritas and Math you learned about greatest common divisors (GCDs) and the recursive form of the Euclidean Algorithm. Naturally you'd want to add a nice GCD function to your system, 'cause you just never know when it might be useful to know the GCD of two numbers. Since this is definitely not the sort of function that would be used in every script you write, it's a good candidate for an on-demand function. So how would you make one of these?

find_real_file.pngThe first step is to write your function in a Script Include (navigate to System Definition → Script Includes and click New). I recommend following a convention of prefixing the name of any on-demand function you write with "u_" to be certain you don't accidentally collide with the name of any system-supplied function. Note that it is vital for the name field of the Script Include to exactly match the name of the function you define in the script part; otherwise you'll get a nasty error. While you may define multiple functions in a Script Include, the only one that will load automatically on-demand is the one whose name is in the name field. So here I called my new on-demand GCD function "u_gcd". Now all I need is a test script:


var a = u_gcd(21560, 12747);
gs.log(a);

Manually factoring those numbers I get (with a little help from Factoris😞
21560 = 23 × 5 × 72 × 11
12747 = 3 × 7 × 607
By inspection the GCD is 7, and indeed that's the answer I get when I run the test code.

You may be wondering where you can use an on-demand function. The answer is simple: anywhere, anytime, in any script. That means in other Script Includes, in Business Rules, in condition scripts, in UI Actions, in Probes, in Sensors...you get the idea. Anywhere!

9 Comments