Calling javascript functions?

questspidey2
28 Jul 2020, 12:13

I'm not able to find in the documentation how to utilize javascript. For example, how would I go about printing a message which is the result of a javascript function?
I'm trying: msg (JS.test())
And in my javascript file, I have a function named test() which returns a string.

But I receive the following error:
Error running script: Error compiling expression 'JS.test()': FunctionCallElement: Could find not function 'JS___DOT___test()'
I've tried excluding the JS, no luck either.
Thanks for any help!!!


mrangel
28 Jul 2020, 14:00

You can call a javascript function, but you can't get it to return a value. This is because when a command is sent to the backend, Quest passes it to HandleCommand and then waits for that to return; batching up all the javascript functions that would be called in response (including the implicit JS.addText calls generated by msg), and then returns them to the JS on the frontend to run. So any function you call using the JS. syntax will be queued to run after your function finishes.

If you want to pass values from JS back to Quest, you need to use a callback. The javascript function ASLEvent takes 2 parameters: the name of a Quest function, and a string parameter.

So in this case, you would do:

JS.eval("ASLEvent('TestResponse', test());")

and create a function to print the message passed back:

<function name="TestResponse" parameters="param">
  msg (param)
</function>

(although unless your test() function returns a string that contains text processor commands, you'd likely want to do:

JS.eval("addTextAndScroll(test())")

)


questspidey2
28 Jul 2020, 14:12

Awesome info, I will try this out. Thanks for your time!