how to disable the block if it is not connected to any other block or start block

1,256 views
Skip to first unread message

Vivek

unread,
Apr 9, 2019, 7:07:54 AM4/9/19
to Blockly
Hi all,

I want to disable all the blocks if they are not between the start and stop blocks. Disable all the disconnected blocks also.
If there is any way to get this done then please tell me how I can achieve this.

Thank You
Vivek




Coda Highland

unread,
Apr 9, 2019, 7:55:12 AM4/9/19
to blo...@googlegroups.com
Do you want to disable them (that is, make them uneditable), or just not execute them? Not executing them is easy, you just hold a reference to the start block and generate code for it instead of for the whole workspace. This is what I do in my project.

Also, you might need to pay special attention to functions, since they're not connected blocks but they need to be editable and to have code generated for them.

/s/ Adam

--
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.
For more options, visit https://groups.google.com/d/optout.

Vivek

unread,
Apr 9, 2019, 8:07:43 AM4/9/19
to Blockly
Hi Adam
I am implementing this in scratch junior. I want to disable them so that they will not be executed.
If all the blocks are between start(as the first block) and stop(as the last block) then it is good but if blocks are not within the start and stop then those blocks will be disabled and will not be executed.

Please tell me the function where I can disable the block after its placed on the workspace and disable it if it is not connected to other blocks.

Thank You
Vivek
To unsubscribe from this group and stop receiving emails from it, send an email to blo...@googlegroups.com.

Beka Westberg

unread,
Apr 9, 2019, 9:51:21 AM4/9/19
to Blockly
Hello Vivek,

Have you tried using disableOrphans? This will make it so any block not connected to a top block (e.g. your start block) is disabled. It looks like it is included in scratch-blocks.

I hope that helps!
Beka
Message has been deleted

Ashish sipani

unread,
Aug 14, 2019, 2:04:08 AM8/14/19
to Blockly
Hi Beka,

I wants to disable the block which are not connected to onStart and forever block in workspace. Like as this screenshot.
Screenshot from 2019-08-14 11-25-17.jpg

Sam El-Husseini

unread,
Aug 14, 2019, 2:31:46 AM8/14/19
to Blockly
Hey Ashish, 

There's no built in way in Blockly to automatically disable all blocks that are not connected to specific blocks.
However (and this is the way MakeCode does it), you can run through all the top blocks in the workspace and disable any blocks that aren't in a specific set that you want to allow.
Something like this:
var topBlocks = workspace.getTopBlocks();
for (var i = 0; i < topBlocks.length; i++) {
 
var topBlock = topBlocks[i];
 
if (topBlock.type !== "on_start") {
    topBlock
.setEnabled(false);
 
}
}


Cheers, 
Sam

Ashish sipani

unread,
Aug 14, 2019, 3:02:18 AM8/14/19
to Blockly
Hi Sam ,

I am appreciate with you. I am also want to implement makecode feature in blockly. And where to implement this code in blockly.
Please help me and reply fast.

Thanks

Beka Westberg

unread,
Aug 14, 2019, 10:06:07 AM8/14/19
to Blockly
Hello,

You could also use the disableOrphans event listener.

It achieves the same effect sam described (disabling all blocks that are not connected to top blocks) but it does it slightly differently to work efficiently with the event system.

To add it you would do the following:
var workspace = Blockly.inject('myDiv', myOptions);
workspace
.addChangeListener(Blockly.Events.disableOrphans);

I hope that helps! If you have any further questions please reply!
Beka


On Tuesday, August 13, 2019 at 11:04:08 PM UTC-7, Ashish sipani wrote:
Reply all
Reply to author
Forward
0 new messages