Functions



SsScriptMaker lets you to define your own functions to use it in your scripts. Functions are useful to encapsulate pieces of code or to expand the functionality of the standard commands. But they need to be written in groovy, so only use them if you're ready to step on groovy code.



To create a new function go to the functions tab in the main panel and click on the Add Function button. You will be asked to introduce the function's name, the parameters of your function, and the code of your function. For the function's name you can type any valid name that's not already used by the SexScripts api or by a groovy special word. It must start with a letter and can only contain letters, digits, and the underscore character '_'. It can't contain any spaces. To define the parameters of your function simply write their names separated by commas. To name a parameter follow the same rules as to name any variable. If you do not want your function to take any parameters at all, leave this field empty. All the parameters of a function can be used in the function's code without needing to declare them first.



Remember that all variables you use inside your script will be out of the scope of your functions, so if you want to create a function that uses variables you use in your script, pass them to your function as parameters.



Sometimes you will want your functions to return some calculated value. To do that you can use the special groovy word return, in a statement like return <expression>, where the value of expression will be returned. Also if you do not use any return statement as the last statement of your function, the value calculated in the last statement of your function will be returned. Take a look at functions.ssm in the Examples folder to see examples of functions.



There're only one last thing to remember for you when writing a function. From a function you can call any other function like in any piece of code, and the functions that call themselves are called recursive functions. Be very careful when writing recursive functions because it's very easy to make an infinite loop of self-calling functions and your script will hang. Recursive functions are tricky, so step on them calmly. To make a function to call itself, you can't use its name like calling any other function, instead you have to use the special groovy form call(params) as if it was your function. Params should be the parameters you want to pass your function call separated by commas. To see an example of a recursive function take a look to the factorial function in the functions example.



When you're done introducing your function's name, parameters and code click on the Create Function button, and your function will be added to your script's function list. You can edit it later by double-clicking on it.



return to the contents page