Count number of blocks in workspace

1,202 views
Skip to first unread message

Prateek Mohan

unread,
Jun 4, 2015, 11:28:04 AM6/4/15
to blo...@googlegroups.com
Hi,

I am using blockly to create a game for young kids, and as a challenge I want to have a counter for the number of blocks the user can put onto the screen. 

I'm wondering how I can keep track of the blocks in the workspace once the user has dragged and dropped one onto it.

I started by looking at the Blockly.Generator.prototype.workspaceToCode() function and tried to get the number of blocks out of it as seen below:

Blockly.Generator.prototype.workspaceToCode = function(workspace) {
  if (!workspace) {
    // Backwards compatability from before there could be multiple workspaces.
    console.warn('No workspace specified in workspaceToCode call.  Guessing.');
    workspace = Blockly.getMainWorkspace();
  }
  var code = [];
  this.init(workspace);
  var blocks = workspace.getTopBlocks(true);
  //New code below
  console.log(blocks);
  //New code above
  ...
}

I thought the "blocks" variable might contain the current blocks in the workspace but it's always empty so I might be looking at the wrong spot.

Just wondering if anyone has any thoughts on this.

Thanks,

Prateek

Blake

unread,
Jun 4, 2015, 11:55:41 AM6/4/15
to Blockly
I think what you are looking for is: Blockly.mainWorkspace.getAllBlocks().length

  1. I found this knowing that the Blockly Games Maze puzzle keeps track of the number of remaining blocks: https://blockly-games.appspot.com/maze?lang=en&level=5&skin=0
  2. Then I looked through the maze.js file and found the Maze.updateCapacity() which calls Blockly.mainWorkspace.remainingCapacity(); https://github.com/google/blockly-games/blob/master/appengine/maze/js/maze.js#L930
  3. Then I looked up the Blockly.mainWorkspace.remainingCapacity();function in https://github.com/google/blockly/blob/master/core/workspace.js#L177 and learned about getAllBlocks().length
  4. Then I tested it to verify it works.
Hope this helps. 

Blake

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

Prateek Mohan

unread,
Jun 4, 2015, 12:41:43 PM6/4/15
to blo...@googlegroups.com
Thanks for the quick reply Blake - is there perhaps a trigger as well that happens when each block is placed onto the workspace? I'm wondering if I can have a real-time counter.

toe...@extremenetworks.com

unread,
Jun 4, 2015, 1:32:55 PM6/4/15
to blo...@googlegroups.com
For that you want to post a listener for any updates on the workspace:
     workspace.addChangeListener(myUpdateFunction);
See more details at https://developers.google.com/blockly/installation/code-generators#realtime_generation

Neil Fraser

unread,
Jun 4, 2015, 2:10:25 PM6/4/15
to blo...@googlegroups.com

Prateek Mohan

unread,
Jun 4, 2015, 5:28:23 PM6/4/15
to blo...@googlegroups.com, ro...@neil.fraser.name
Thanks a lot for all the help guys - it worked!
Reply all
Reply to author
Forward
0 new messages