How to get all the blocks below a statement block?

452 views
Skip to first unread message

George

unread,
Apr 11, 2021, 10:02:06 AM4/11/21
to Blockly
A C-block can get all the blocks inside of it using Blockly.JavaScript.statementToCode(block, 'DO') if the block is initialised with a { "type": "input_statement", "name": "DO" } argument

How to write  a generator for a block that gets all the blocks FOLLOWING this block and then prevent them being manually transpiled by Blockly?

So, a pseudo-C-block 

George

unread,
Apr 11, 2021, 10:26:34 AM4/11/21
to Blockly
SO question for this (with images): https://stackoverflow.com/q/67046131/3310334

George

unread,
Apr 11, 2021, 10:27:42 AM4/11/21
to Blockly
So the question would be, how to compile the attached image

Screenshot 2021-04-11 at 16.14.08.png

Into 


console.log('1');
console.log('2');
(function () {
  console.log('3');
  console.log('4');
}());

Maribeth Bottorff

unread,
Apr 13, 2021, 7:58:01 PM4/13/21
to Blockly
My first question is, why not use a block with an actual statement input? In your example, what if a user wanted additional code afterwards that shouldn't be wrapped? How would you distinguish between what should be wrapped and what shouldn't? That seems like a statement input would be the best solution to me, then you can use statementToCode like you said in your first message.

If you don't want to do that though, the "print 3" block in your example is the child of the purple block, so you could `blockToCode` on the first child of the purple block and it would return the code for all the blocks below it as well. Is that enough for what you want to do?

Maribeth

George

unread,
Apr 14, 2021, 4:06:23 AM4/14/21
to blo...@googlegroups.com
I suspect using workspace.blockToCode(block.childBlocks_[0]) will produce output like

console.log('1');
console.log('2');
(function () {
  console.log('3');
}());
console.log('3');
console.log('4');

With repetition of the below blocks, which is not what I would like:

console.log('1');
console.log('2');
(function () {
  console.log('3');
  console.log('4');
}());

And regarding your question, all following blocks should be included

-- 
You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/O50PXGa0E-s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/bd00e715-fc43-4452-8385-ecf7e2a97995n%40googlegroups.com.

Maribeth Bottorff

unread,
Apr 16, 2021, 12:58:48 PM4/16/21
to Blockly
Can you share why you don't want to use a statement input? That seems like the best fitting paradigm so knowing why you don't want to use it may help find an alternative solution. If you don't want to allow blocks after the statement input block, you can remove the nextConnection input.

Maribeth

George

unread,
Apr 16, 2021, 1:22:14 PM4/16/21
to blo...@googlegroups.com
For abstraction. For example to compile a wait () seconds block to javascript, output has to be setTimeout(function () { .... (everything following the block }, time);

Maribeth Bottorff

unread,
Apr 16, 2021, 1:59:31 PM4/16/21
to Blockly
Here's how I would make a block to do that. This screenshot is from Blockly Developer Tools. It has a numeric input field to grab the wait time, and a statement input to get the actions that should be inside the setTimeout. It only has a previousConnection, so you can't add actions that come after this that are outside of the setTimeout.

A for loop is really the same concept. You have some collection of statements and you want to wrap them in additional code (i.e. add the for loop syntax surrounding the stack of blocks). So using something that works in a similar way makes sense to me. 

Would a block like that work? If not, I think you'd have to resort to some kind of hack like checking if any of a block's parents are your special block, and changing the code generation for all other blocks to take that into account, but if you have a specific problem with the block design like I've shown here then maybe we can help you work around that problem.

wait_block.png
Reply all
Reply to author
Forward
0 new messages