Help with using provideFunction_ or alternative

89 views
Skip to first unread message

Psycho Buddha

unread,
Dec 28, 2022, 5:11:34 PM12/28/22
to Blockly
I would like to use a simple function (that I have defined in a different file) in my code generator, but can't really wrap my head around it.

I am looking into the provideFunction_ method and I am wondering if is it possible to pass it an external function, so instead of this:Screenshot 2022-12-28 at 5.05.14 PM.png

I would like to do something similar to this:

Screenshot 2022-12-28 at 5.08.02 PM.png

Mark Friedman

unread,
Dec 28, 2022, 7:58:08 PM12/28/22
to blo...@googlegroups.com
The answer to your question depends a bit on how you intend to run the code that Blockly is generating for you.  If you are loading (or eval-ing) that code into a JavaScript environment that already has your function defined then all you should need to do is have your generator output a string that calls the function. You don't need to use provideFunction_ or import myFunc in that case.  

However, if you want your Blockly code generator to place the definition of myFunc in its generated code then you probably want to use provideFunction_.  The purpose of that function is to put the definition of a function in your generated code. If you do want Blockly to generate your function definition for you then you'll need to do it in the way that provideFunction_ wants you to do it.  See the documentation for provideFunction_ for a little more info.  Note that you could, potentially, import your myFunc function and use myFunc.toString() to generate the string that provideFunction_ wants.  You'd have to process it a little bit to substitute javascriptGenerator.FUNCTION_NAME_PLACEHOLDER_ for the name of the function in the resulting string.

Hope this helps!

-Mark


--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/29dbd99d-de51-4591-ac17-8d2e84709e3dn%40googlegroups.com.

Psycho Buddha

unread,
Dec 29, 2022, 10:10:47 AM12/29/22
to Blockly
Thanks Mark,

That answers my doubts. The environment will be aware of the function, so I will be able just to call it with a string output. And I can use the recommended way of provideFunction_ method for testing purposes.

Happy holidays!
Reply all
Reply to author
Forward
0 new messages