"Main" block

160 views
Skip to first unread message

Eric Edem

unread,
Nov 24, 2020, 11:00:06 AM11/24/20
to Blockly
Hey All. Has anyone discovered a good pattern for creating a main block?

What I'd like to do is describe where the program begins, along with some system-level behaviors via inputs on that block. The important thing here is that it would be the only entry point, and there could only be one of them.

The way I have approached this so far is to override the workspaceToCode() generator method, and only generate code for that block. This is a little bit funky but it does work. The thing that's missing is whether there is a good way to limit a user to one block.

Maybe some scheme where the program comes with that block by default and can't delete it or create a new one?

Best,
Eric

nekom...@gmail.com

unread,
Nov 24, 2020, 11:41:36 AM11/24/20
to Blockly
Hi! I guess this is what you are looking for: workspace.addChangeListener(Blockly.Events.disableOrphans);

This will switch every block (which is not a top level block*) to its disabled state when it's not connected to a top level block. These blocks will be ignored by the Blockly.JavaScript.workspaceToCode(workspace); call.

* A block considered to be a top level block if it does not call this.setPreviousStatement(true);

Eric Edem

unread,
Nov 24, 2020, 12:39:45 PM11/24/20
to blo...@googlegroups.com
Ah nice, that's a great idea.

I guess the follow-up question I have is whether there is a way to restrict how many of a certain type of block you can have. I only want a user to describe one of these blocks, because otherwise it will be ambiguous for the system to determine which one to run.

--
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/9hWsqz0zBYg/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/5370b1c2-09d4-44dd-8406-e2eb7b6da96en%40googlegroups.com.

nekom...@gmail.com

unread,
Nov 24, 2020, 3:23:39 PM11/24/20
to Blockly
You can preset a block during page load, so your users won't have to add any.

let xml_text = "<xml><block type='start_block' deletable='false' x='250' y='50'></block></xml>";
let xml = Blockly.Xml.textToDom(xml_text);
Blockly.Xml.domToWorkspace(xml, workspace);

Place this code after you've created the workspace and injected the config options.

Sam El-Husseini

unread,
Nov 24, 2020, 3:43:15 PM11/24/20
to blo...@googlegroups.com
You can use the maxInstances config to set a maximum for the number of instances of a certain block type. 

Here’s a demo:
Cheers,
Sam

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/c4bc8ae7-2a9d-4b44-9865-a0230f5533fen%40googlegroups.com.

Eric Edem

unread,
Nov 24, 2020, 7:37:02 PM11/24/20
to blo...@googlegroups.com
Ok thanks a lot, these are great examples.

What ended up working the best for me is initializing the workspace with an undeletable block and removing that block from the toolbox.

Thanks,
Eric

Reply all
Reply to author
Forward
0 new messages