How to add a variable to the toolbox?

952 views
Skip to first unread message

Eric Lundby

unread,
Jun 14, 2016, 11:47:17 AM6/14/16
to Blockly
Hello,

I have been able to successfully add all of my blocks to the toolbox, with two exceptions, Variables and Functions. I want to add a variable to the Variables category, however, I am confused on how it differs from the other categories with the: 
<category name="Variables" custom="VARIABLE"></category>

My application has a global variable that is already defined, and I want to add a getter and a setter to the Variables category. I am using a fork of BlocklyDuino if that changes anything.

Can anyone point me in the right direction? I have tried chasing down the code path that adds a variable to simply call this at startup, but I haven't been successful.  

Wolfgang Koehler

unread,
Jun 15, 2016, 4:50:39 AM6/15/16
to Blockly

Eric Lundby

unread,
Jun 15, 2016, 10:46:38 AM6/15/16
to Blockly
Thanks Wolfgang. I've been up and down that page before. All it says is:

There are two categories that have special behaviours. Variable and function categories are defined with no contents, but with a 'custom' property of 'VARIABLE' or 'PROCEDURE' respectively. These categories will be populated automatically with the appropriate blocks.


As I've said, I have been successful modifying the toolbox for other categories, which is all that link is useful for. 

The variables category does not operate in the same way. How can I manually add a getter/setter block?

Rachel Fenichel

unread,
Jun 15, 2016, 2:02:27 PM6/15/16
to Blockly
Blockly.Variables.allVariables() walks the list of blocks in the workspace and makes a list of all of the variables that it finds.  Sounds like you want to automatically have your variable be included in the list, regardless of whether there is a block using it yet, so change allVariables() to make sure your global is in the list before it returns.

Eric Lundby

unread,
Jun 15, 2016, 3:46:31 PM6/15/16
to Blockly
Thank you so much Rachel. That was exactly what I needed. 
Reply all
Reply to author
Forward
0 new messages