Dynamically creating another block

565 views
Skip to first unread message

kate@yoon

unread,
Feb 26, 2019, 2:47:21 AM2/26/19
to Blockly
Hi,

I am making a blockly project where I have made  custom blocks. In one of the custom block I have a text field with default text as "save" and NAME as "EXP_SAVE"
I want the value of the text entered to be saved in an array and create the below "exp_saved"  block with the same array values as dropdowns dynamically.
Below is my block json file for the same.
 "args0": [
    {
      "type": "field_input",
      "name": "EXP_SAVE",
      "text": "save"
    },
{
  "type": "exp_saved",
  "message0": "%1",
  "args0": [
    {
      "type": "field_dropdown",
      "name": "NAME",
      "options": [
        [
          "option",
          "OPTIONNAME"
        ]
      ]
    }
  ],
  "output": null,
  "colour": 230,
  "tooltip": "Saved Expressions",
  "helpUrl": ""
}

Beka Westberg

unread,
Feb 26, 2019, 9:59:11 AM2/26/19
to Blockly
Hello,

Creating the entire block dynamically might be tricky, but if you just want to create a dynamic dropdown that's easily doable. The main downside is that the block with the dynamic dropdown will need to be defined in Javascript rather than Json. Your code might look something like this:

Blockly.Blocks['exp_saved'] = {

  init
: function() {
   
this.appendDummyInput()
         
// This is where the dynamic dropdown is created.
         
.appendField(new Blockly.FieldDropdown(this.dynamicOptions), 'NAME');
   
this.setColour(230);
   
this.setTooltip("Saved Expressions");
   
this.setHelpUrl("");
 
},


 
// This returns the options for your dynamic dropdown.
  dynamicDropdown
: function() {
   
var options = [];
   
for (var i = 0; i < mySavedExpressionArray.length; i++) {
      options
.push(mySavedExpressionArray[i], 'OPTION' + i);
   
}
   
return options;
 
}
};

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

Sayali Kate

unread,
Feb 28, 2019, 3:17:53 AM2/28/19
to blo...@googlegroups.com
Hi,

Thanks for your suggestions.how can i pass my array inside one function into the block defination. So as to dynamically populate my dropdowns.

Function saved()
{
Saved[counter] = exp_saved;
Counter ++;
}

I want to define this array inside your suggested block defination

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

Amber B

unread,
Feb 28, 2019, 10:14:57 AM2/28/19
to Blockly
I'm a bit confused by your use case here. A few questions:

  1. What exactly is the Saved array? What does the saved function do? It doesn't seem like it populates the options available in the dropdown... is it supposed to run when an option is selected?
  2. Do you want the values of Counter and Saved to be independent for each instance of the block (e.g., one block may have a different set of dropdown options than another), or the same for every instance of the block (e.g. all of the blocks will have the same set of dropdown options)?

Beka Westberg

unread,
Feb 28, 2019, 10:19:05 AM2/28/19
to Blockly
Hello,

So it depends a little bit on what you want to accomplish. If you want a global list that all dropdowns share you can just make the options array a global variable. If you want a list that is per-block specific you can define the array on the block like so:

Blockly.Blocks['exp_saved'] = {

  init: function() {
    // Here's the array definition.
    this.mySavedExpressionArray = [];
    this.appendDummyInput()
          // You have to add the binding so that "this" refers to the correct object.
          .appendField(new Blockly.FieldDropdown(this.dynamicOptions.bind(this)), 'NAME');
    this.setColour(230);
    this.setTooltip("Saved Expressions");
    this.setHelpUrl("");
  },

  dynamicDropdown: function() {
    var options = [];
    // These things changed to call this.mySavedExpressionArray.
    for (var i = 0; i <  this.mySavedExpressionArray.length; i++) {
      options.push( this.mySavedExpressionArray[i], 'OPTION' + i);
    }
    return options;
  }
};

And then your save function will need to access that block to add to the array, either through getBlockById() or itterating through the array returned by getBlocksByType(), or some other method.

I hope that answers your question! If you were looking for advice about something different, could you provide an example of how you want it to work? Best wishes!
Beka

Sayali Kate

unread,
Mar 2, 2019, 4:29:52 AM3/2/19
to blo...@googlegroups.com
Hi, 

How can i get all the childblocks of a  block. I tried doing gettopBlocks.childBlocks_ but it returns me an array and again the next child inside that array


Please help

Beka Westberg

unread,
Mar 2, 2019, 9:52:07 AM3/2/19
to Blockly
Hello,

Have you tried any of these public getters? getDescendants, getChildren, getNext.

--Beka

Sayali Kate

unread,
Mar 2, 2019, 1:02:33 PM3/2/19
to blo...@googlegroups.com
Hi, 

I have tried getchildren()[0]; but it returns me only the child block below the top block.I want to iterate through each child and get their type to store in an array. So that I can give return statements accordingly.
Please help.

Beka Westberg

unread,
Mar 3, 2019, 10:04:44 AM3/3/19
to Blockly
Hello,

I'm sorry I'm a little confused about what you're trying. Does your code look like this:

var topBlocks = myWorkspace.getTopBlocks();
for (var i = 0; i < topBlocks.length; i++) {
 
var children = topBlocks[i].getChildren();
 
for (var i = 0; i < children.length; i++) {
   
var child = children[i];
   
// Doing things with child block etc...
 
}
}

If so please reply with how this is and is not working for your particular situation. If not please reply with your code, and what kind of info you're looking to collect. I.e. do you want to know all the types in a workspace? All the types in a stack? If a certain type exists in a stack? etc

If you want info about block types you could also try Blockly.utils.getBlockTypeCounts() this returns of map of types to the number of blocks of that type in a block stack.

Thank you and good luck!
Beka
Reply all
Reply to author
Forward
0 new messages