how to get value from block?

233 views
Skip to first unread message

Naresh Chaudhari

unread,
Feb 18, 2021, 2:36:11 AM2/18/21
to Blockly
Hi All, I have created custom block which can add item dynamically using the mutation. For ex: Suppose i have added 5 items in block dynamically and want to get value of its connected block... How do i get value from the block.
I don't know where should I write code to get value as: blogger, getRegal, soloEdge

Blockly.Python['import_value'] = function (block) {
  var text_import_value = block.getFieldValue('IMPORT_VALUE');
  // TODO: Assemble Python into code variable.
  console.log("import_value Block", text_import_value)         // Getting first value as: blogger
  var code = '' + value_regal_import + '\n';
  return [code, Blockly.Python.ORDER_NONE];
};

Blockly.Python['basic_regal_imports'] = function (block) {
  var value_regal_import = Blockly.Python.valueToCode(block, 'REGAL_IMPORT', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble Python into code variable.

  console.log("Main Regal Import block", value_regal_import)   // Here nothing is displaying
  // var code = regal_import + '' + value_regal_import + '' + regal_import + '\n';
  var code = ''+value_regal_import+'\n';
  return code;
};

Capture.PNG
Help will be appreciated, Thanks in advance.

Beka Westberg

unread,
Feb 18, 2021, 5:52:43 PM2/18/21
to blo...@googlegroups.com
Hello,

Since you have a variable number of inputs, you're going to need to use a loop to loop through your inputs and collect the values =) A good example of this is the list_create_with block, which also allows you to add inputs using a mutator.

```
Blockly.Python['lists_create_with'] = function(block) {
  // Create a list with any number of elements of any type.
  var elements = new Array(block.itemCount_);
  for (var i = 0; i < block.itemCount_; i++) {
    elements[i] = Blockly.Python.valueToCode(block, 'ADD' + i,
        Blockly.Python.ORDER_NONE) || 'None';
  }
  var code = '[' + elements.join(', ') + ']';
  return [code, Blockly.Python.ORDER_ATOMIC];
};

```

As you can see, the block has an itemCount_ property which specifies how many inputs the block has. This allows the generator to loop through all of the inputs and collect their values. The code that assigns to that itemCount_ property lives here.

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

--
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/5a0254f5-6f7a-40ca-8437-1b185a83f98fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages