Native CSML Functions

Native CSML Functions

CSML functions are a simple way to define simple functional code that can be reused across all of your flows. They are defined in any flow file and use the same CSML syntax that is available in any other steps.
Conceptually, they can be viewed as simple placeholders that you can insert at any place in any flow, to perform repetitive tasks.
Declaring a function is similar to declaring a step with some minor differences:
    the name of the function is preceded by the keyword fn ,
    there is a list of parameters between parentheses after the name of the function,
    but also, like a step declaration, it ends with a semicolon
1
fn my_function_name(param1, param2, param3, ...):
2
/** do something **/
3
return some_val
Copied!
Here is an example of a simple my_double_add() function, that takes 2 parameters, multiplies each of them by 2, then adds those numbers together, and returns the result:
1
fn my_double_add(x, y):
2
do dbl_x = x * 2
3
do dbl_y = x * 2
4
return dbl_x + dbl_y
5
6
start:
7
do value = my_double_add(1, 2)
8
say "The result is: {{value}}" // 6
9
goto end
Copied!
There is no limit on how many functions a flow can have, and of course you can call a function from within another function. The above example could be rewritten as:
1
fn my_double(num):
2
return num * 2
3
4
fn my_add(a, b):
5
return a + b
6
7
fn my_double_add(x, y):
8
do dbl_x = my_double(x)
9
do dbl_y = my_double(y)
10
return my_add(dbl_x, dbl_y)
Copied!

Limitations

CSML native functions are isolated from the bot itself and can't interact with the memory or display messages. They have only access to non-state-binding keywords: do, if, foreach and return.
1
// Forbidden
2
fn my_func(a):
3
say "hello"
4
remember something = 42
5
hold
6
goto somestep
Copied!
However, other builtins can be used!
1
// Accepted
2
fn my_func(a):
3
do x = 0
4
foreach (item) in a {
5
do x = x + item.value
6
if (x > 42) break
7
}
8
do HTTP("https://myapi.com/").post().send()
9
return x
Copied!

Importing Functions From Other Flows

Functions are by default attached to the flow where they are written. However, you can import functions from other flows to reuse them elsewhere!
1
// import this function specifically from this flow
2
import my_function from flow_2
3
4
start:
5
do value = my_function(1, 2)
6
say value
7
goto end
Copied!
You can also use a global import by not defining the flow from which a function can be imported.
1
// import a function with this name from any other flow
2
import my_function
Copied!
Warning! If there is more than one flow defining a function with the same name, it will just import one of them randomly.
Don't use the same name on several functions, or specify which flow you want to import from

Advanced usage

You can import multiple functions at once, and even rename them:
1
// import several functions at once
2
import {my_function, other_function} from flow_2
3
4
// you can rename a function
5
import my_function as better_name from flow_2
6
7
start:
8
do value = better_name(42)
9
say "The result is {{value}}"
Copied!
Last modified 1yr ago