Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.
Originally posted by: Tim MeagherHi,
There isn't a way to define your own BRAINscript functions and use them at the moment. This has been raised as an enhancement request although I can't say anything about when that would likely be implemented.
One thing you can do is to create another parameter on your node to contain the code for the function you want to implement, then invoke this via textual substitution. Basically, using this method, you don't have to re-type the code, but the code will be inserted at each point where you use the textual substitution for the parameter.
For a simple example, consider you want to define a method that takes a year, month and day each in string form, but containing integer values (e.g. "2012", "01", "22") like:
date convertToDate(string year, string month, string day) {
intYear = year.int()
intMonth = month.int()
intDay = day.int()
return date(intYear, intMonth, intDay)
}
Then assuming you want to perform this function several times in your code, like:
date1 = convertToDate(f1, f2, f3)
date2 = convertToDate(f4, f5, f6)
There isn't really a way to do this directly, however, you can do pretty much the equivalent thing by creating a new brainscriptexpr parameter, call it "convertToDate".
Then, in convertToDate, you can use the following BRAINscriptcode:
intYear = arg0.int()
intMonth = arg1.int()
intDay = arg2.int()
retVal = date(intYear, intMonth, intDay)
Then in your main node script parameter you could have the following:
arg0 = f1
arg1 = f2
arg2 = f3
{{^convertToDate^}}
date1 = retVal
arg0 = f4
arg1 = f5
arg2 = f6
{{^convertToDate^}}
date2 = retVal
Bear in mind that since this is just using textual substitution (i.e. inserting the method code), and because BRAINscript doesn't have a concept of local variables, or scope, you have to be careful that any variable names (e.g. retVal, arg0, arg1, arg2 in this example) aren't conflicting and overwriting the values of variables defined in your main code script.
Tim.