Now I got my hands on the 1.4 version I off course want to try the new features. Over the coming weeks I will take new features and write about my first tries to use them. The first item in this series is the Global Script Function.
You will not find the component in the Components Panel. You can add them by right clicking on the new “Technical Components” folder in the Outline panel and create a child of the type “Global Scripts Object”.
You can create multiple instances of this type of object and in this object you can create as many functions you want.
When you add a function you will see this screen. You can set the input parameters and you can set the return type. There are two main types :
- Void : just do things
- All others: return a value to the caller
As you can see there are many types. Including the components. Even SDK components will show up here as a type that you can return.
The Script Editor is the script editor that we know already. What you will find when you press <CTRL><SPACE> is a method “return” . This is needed when you set the return type to anything but void. With this keyword you can send the final result on its way to the caller.
Getting my feet wet
For a start I made three functions :
- setMenu type void: A function that sets all elements to disabled and the one passed in parameter to enabled
- updateButtons type button: A function that returns a component type to see how you can work with it
- presentValue type string: A complex calculation function that calculates the net present value for an amount you get in 1,2,3 and 4 years.
This is the basic menu function. You have a number of buttons and you want them all to reset. Only the current button has to be enabled. This off course can also be a CSS function where the current button gets a special CSS layout.
This works easy enough. It is a void so you basically have a central script here. What I didn’t like at all was that renaming components in design studio does not lead to renaming in the function. This was always there from the start. Something I really appreciate. I really expect that this will be available as soon as SP1 as this does hamper the advantage of a central function. Now I would continue using buttons for this as I don’t want to unintentionally break the script.
I was curious what I could do when I returned a component type. That’s why I also tried this function.
What I can do is either pick up a button as an input parameter or refer to a button in the internal var statement. For example to a template button?
I can set some properties and return the button object with the new properties.
In the scripts I cannot suffice with
BUTTON_2 = GLOBAL_SCRIPTS_1.updateButtons();
I have to assign the result to a variable and then copy those properties to my own button.
What I can do is use this function as input for another function. You run into the same issue as above. But if you create multiple void functions and want to refer to a standard button from all those functions you can use this.
Net Present Value
The final function is a financial calculation. This is mainly used in investment decisions. If I invest 100 now and get 105 back in 3 years. Is that good or bad? The present value will calculate the 105 in three years back to present value by using some kind of inverse intrest.
The formula is Amount / (1+intrest rate) ^ number of years.
In this example it was my goal to produce a list of values for each year.
As you can see instead of using a power function I used an elaborate way by multiplying. Normally I would create an SDK to do this as this is a bit too clumsy. But now my goal is to get to know the global function.
Additionally I cannot use an array as a return type. I could circumvent that by using a dropdown box for example and use additem and return that.
Instead I chose to return a string and separate the values by pipes (|).
In this script you see how I split the values up again and pass them to a listbox. In a next installment I will look at the constant datasource. I think using addcell as a method will result in me being able to create a graph visualizing the next present value amounts.
All in all I am happy that this function is added but I do think it still needs more flexibility and it doesn’t feel completed yet. The number one thing that is needed is making sure that renaming objects won’t break any script lines.