Display repeat block 'count' variable when highlighting

125 views
Skip to first unread message

Joan Cejudo

unread,
Dec 29, 2014, 5:22:22 PM12/29/14
to blo...@googlegroups.com
I'm trying to output some text to emphasize what's happening as the code runs together with highlighting. Specifically, I'm looking to display some text that displays the current value of 'count' when using the repeat loop, and display true/false for the conditional loops.

Any thoughts on how I might achieve this without modifying the core blocks provided with blockly? For example, passing the values onto the highlight function?

One idea for now is to grab the blocks from the id passed to highlight(id) and generating the Javascript code for that specific block. I'm able to select the block by id, but cannot generate the code for it. Do I need to create a new generator for this?

Thanks!

Carlos Galarza

unread,
Dec 31, 2014, 11:08:35 AM12/31/14
to blo...@googlegroups.com
this is an example of highlightBlock doing a bit of what you say, that works with blockly interpreter demo:
    function highlightBlock(id) {
      Blockly.mainWorkspace.highlightBlock(id);
      var currentBlock = Blockly.mainWorkspace.getBlockById(id);
      highlightPause = true;
      if (currentBlock.type == 'controls_repeat_ext') {
        var count = myInterpreter.getScope().properties.count.data;
        if (count != undefined) {
          console.log('count: ' + count);
        }
      }
      if (currentBlock.type == 'variables_set') {
        var n = myInterpreter.getScope().properties.n.data;
        if (n != undefined) {
          console.log('n: ' + n);
        }
      }
    };

Johannes Mauerer

unread,
Dec 31, 2014, 2:39:59 PM12/31/14
to blo...@googlegroups.com
You could generate the code for a specific block by calling blockToCode:

So if you have the ID of the block, retrieve it and call e.g. Blockly.Javascript.blockToCode(block) - the generator will then check if the blocktype has a function within the generator and execute it.

Joan Cejudo

unread,
Dec 31, 2014, 5:02:25 PM12/31/14
to blo...@googlegroups.com
Thanks a lot Carlos, this is perfect!

Joan Cejudo

unread,
Dec 31, 2014, 5:38:09 PM12/31/14
to blo...@googlegroups.com
Thanks for the tip Johannes! I was getting some errors about undefined using this method. Turns out it was a problem with my custom blocks. Its working now!
Reply all
Reply to author
Forward
0 new messages