Dynamically adding variables

2,469 views
Skip to first unread message

mark.b...@canstudios.com

unread,
Aug 13, 2015, 10:25:54 AM8/13/15
to Blockly
Hi

Is it possible to dynamically create variables with JavaScript code so they can be used (and selectable) in Blockly? E.g. a user creates two variable names ('foo' and 'bar') in my app. I want these two variables to be available in Blockly and use them as if they were created in the Block Factory?

Thanks
Mark

Hendrik Diel

unread,
Sep 21, 2015, 9:58:29 AM9/21/15
to Blockly
There is a function Blockly.Variables.alllVariables() in core/variables.js that returns all used variables. It checks every Block in the workspace if it's a variable and adds them to an array wich is returned then. I guess all blocks using variables use it so if you would like to add predefined variables you would have to modify this function.

Hendrik Diel

unread,
Sep 21, 2015, 10:15:57 AM9/21/15
to Blockly
Since i need a similar feature for my own application i coded a quick solution. I append it to this post. You can include the file to your index.html after loading all the other blockly scripts you might have and then call Blockly.Variables.addPredefinedVar( "variableName" ) somewhere in your code.
predefinedVars.js

mark.b...@canstudios.com

unread,
Sep 21, 2015, 10:28:52 AM9/21/15
to Blockly
Thanks for this. The project I was working on ended up taking a different direction so I didn't get to experiment much more. If we do go back to this, I'll give it a go, thanks!

Hendrik Diel

unread,
Sep 21, 2015, 10:37:28 AM9/21/15
to Blockly
You're welcome. There was a bug putting used predefined variables in the varlist twice so here is a updated version.

A thing to be beware of when using this script: When the user renames a predefined variable it creates a new variable instead. You might want to handle this in another way ..
predefinedVars.js

Tiago Taveira

unread,
Dec 3, 2015, 12:12:54 PM12/3/15
to Blockly
Hi Hendrik,

First of all thank you for implementing this little addon!

Secondly, did you explore changing Blockly to display only one pair of blocks (set + use blocks) when you display the Variables category on the toolbox?

I need to add a large set of predefined variables, and Blockly displays 2 blocks for each defined variable by default...
Ideally, I'd like to have just one pair, and then the user would select the needed variable from the dropdown.

Thank you,

-Tiago Taveira

Tiago Taveira

unread,
Dec 3, 2015, 12:40:17 PM12/3/15
to Blockly
Nevermind, I found one possible approach: change the Blockly.Variables.flyoutCategory function in core/variables.js to just create one variables_set and one variables_get block.
It currently iterates all variables, creating those 2 blocks for each one.

-Tiago Taveira

Michiel Erasmus

unread,
Jul 22, 2017, 11:12:35 AM7/22/17
to Blockly, mark.b...@canstudios.com

FYI. One can create variables in Code generator;
Blockly.Variables.predefinedVars.push("MyVariableName");

It will be listed in Variables toolbox :).

regards,
Michiel Erasmus


Op donderdag 13 augustus 2015 16:25:54 UTC+2 schreef mark.b...@canstudios.com:
Reply all
Reply to author
Forward
0 new messages