Limit block creation to one instance?

955 views
Skip to first unread message

Lyssa Neel

unread,
Sep 28, 2016, 12:00:21 PM9/28/16
to Blockly
Hi,
Is there a way to limit a user to creating no more than one instance of a particular block? Like a block element named "maxInstances" that I have totally missed?

Barring that, I thought I might be able to disable the block in the toolbar if it is already on the workspace.  Does anyone have other suggestions? Zero instances of the block is fine for our application, so I don't want to make it an un-deletable block on the workspace at startup.

Thanks in advance,
Lyssa

Rachel Fenichel

unread,
Sep 28, 2016, 4:01:08 PM9/28/16
to Blockly
Hi Lyssa,

There's nothing built in to do that.  Disabling in the toolbox if it's in the workspace sounds like the best solution.

Cheers,
Rachel

--
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.

Wolfgang Koehler

unread,
Sep 28, 2016, 4:06:54 PM9/28/16
to Blockly
I created this:
//************************************
//find all blocks on workspace by type
//************************************
function getBlocksByType(type) {
  var blocks = [];
  for (var blockID in workspace.blockDB_) {
    if (workspace.blockDB_[blockID].type == type) {
      blocks.push(workspace.blockDB_[blockID]);
    }
  }
  return(blocks);
}

and then I check if getBlocksByType(type).length > 1 in which case I do a workspace.undo(false)

Wolfgang Koehler

unread,
Sep 28, 2016, 4:18:55 PM9/28/16
to Blockly
Here is also a suggestion from Neil Fraser regarding a similar topic: "If a linear search of workspace.getAllBlocks is too slow, one suggestion is to listen to the event stream for creation and deletion events and build up a list of the blocks of the type you are interested in."


On Wednesday, September 28, 2016 at 6:00:21 PM UTC+2, Lyssa Neel wrote:

Lyssa Koton Neel

unread,
Sep 29, 2016, 10:22:01 AM9/29/16
to blo...@googlegroups.com
Thank you Rachel and Wolfgang.
If anyone is interested, in this thread:
https://groups.google.com/forum/?hl=en#!searchin/blockly/toolbox/blockly/vkCpEzre8Lo/sv5UZJL4Bx0J
back in 2014, Andres Nebel suggested using the
Blockly.Flyout.prototype.filterForCapacity_function which is
apparently how Blockly decides to disable blocks on the toolbar.

-- Phyllis Koton Neel, Ph.D.
> --
> 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/pSaLjq30tUg/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

Neil Fraser

unread,
Dec 1, 2021, 2:06:53 PM12/1/21
to Blockly
Note that in 2017 this issue was resolved with the addition of the 'maxInstances' map in the configuration.
Just a note for anyone finding this thread in the archive.

Reply all
Reply to author
Forward
0 new messages