Functions


Functions are a newly revamped feature in skQuery. They utilize a variety of hacky workarounds in order to create a very dynamic environment for functions.

Returns and Soft Returns
Having a return is the most important part of a function. If you never hit a return, the code that called the function will never continue. You should treat functions with caution, because if you forget to return, it may cause unexpected behavior.
command /test:
trigger:
access "no return"
message "This code will never run, because the function doesn't return."
function "no return":
stop

Functions are guaranteed to run before the code that calls it, so delays will carry over to the caller.
command /test:
trigger:
access "in a sec"
message "You saw me 5 seconds after running the command!"
function "in a sec":
wait 5 seconds
return

Soft returns will continue both the code that called it AND the function. A normal return acts like a stop effect.
command /test:
trigger:
access "soft"
access "hard"
function "soft":
soft return
broadcast "This message will still be executed!"
function "hard":
return
broadcast "This message will never be seen :("


Higher level access
At this point you might be wondering "Why is the effect to run a function access? There are literally hundreds of better word choices!". This is because functions use a small hack that allows them to access local variables and event-variables from the code that called it. This is the reason that return statements don't have parameters that let you return objects; you should instead be setting local variables in the parent.

In order to access the parent event, you need to add an access pragma directly before the line of code that will access the parent. The following code will print "success":
command /test:
trigger:
set {_test} to "failure"
access "test"
message "%{_test}%"
function "test":
$ access
set {_test} to "success"
return

The following will print "failure":
command /test:
trigger:
set {_test} to "failure"
access "test"
message "%{_test}%"
function "test":
set {_test} to "success"
return

Keep in mind, when using an access pragma, the next line will only be able to access code in the scope of the caller event. You will not be able to access local variables that have been declared in the function itself, without the access pragma. In order to carry data between the caller and the function, you must use transient objects.

Transient Objects
Transient objects are objects shared between an instance of a function and the caller code that can only be used within functions. They can only be read one time after they are set, as it is only a temporary bridge between the function and the caller. The following code will print "my function is doing fine":
command /test:
trigger:
access "test"
message "%{_test}%"
function "test":
set {_function} to "my function"
set transient "output" to "%{_function}% is doing fine"
$ access
set {_test} to transient "output"
return

The following code will print " is doing fine" (improper usage of a function):
command /test:
trigger:
access "test"
message "%{_test}%"
function "test":
set {_function} to "my function"
$ access
set {_test} to "%{_function}% is doing fine"
return


Examples



Testing code used:
command /testfunction:
trigger:
access "testfunction" from "testing"
broadcast "%{_test}%"
function "testfunction":
broadcast "lol"
set transient "testoutput" to "xD - %parameter 1%"
$ access
set {_test} to transient "testoutput"
wait 2 seconds
soft return
wait 2 seconds
broadcast "finished!"

1