How to getFieldValue() on a shadow block?

290 views
Skip to first unread message

Chung WONG

unread,
Feb 3, 2021, 12:00:00 AM2/3/21
to Blockly
Hello,


It looks like it is not that simple to getFieldValue() when a shadow block is involved.

With this toolbox

<category name="Sound" colour="20">                                                                                        
  <block type="sound_play" id="sound_play">
   <value name="SOUND_MENU">
    <shadow type="sound_sounds_menu"></shadow>
   </value>
  </block>                                                                                                                                            
</category>

These two extensions for reusability.

Blockly.Extensions.register('shape_statement', function() {
  this.setInputsInline(true);
  this.setPreviousStatement(true, null);
  this.setNextStatement(true, null);
});

Blockly.Extensions.register('output_string', function() {
  this.setInputsInline(true);
  this.setOutputShape(Blockly.OUTPUT_SHAPE_ROUND);
  this.setOutput(true, 'String');
});

and these two blocks.
Blockly.Blocks['sound_sounds_menu'] = {
  init: function() {
    this.jsonInit({
      "message0": "%1",
      "args0": [
        {
          "type": "field_dropdown",
          "name": "SOUND_MENU",
          "options": [
            ['1', '0'],
            ['2', '1']
          ]
        }
      ],
      "extensions": ["output_string"]
    });
  }
};

Blockly.Blocks['sound_play'] = {
  init: function() {
    this.jsonInit({
      "message0": Blockly.Msg.SOUND_PLAY,
      "args0": [
        {
          "type": "input_value",
          "name": "SOUND_MENU"
        }
      ],
      "extensions": ["shape_statement"]
    });
  }
};

Blockly.JavaScript['sound_play'] = function(block) {
  block.getFieldValue('SOUND_MENU');
  return null;
};

block.getFieldValue('SOUND_MENU') returned null all the time.
It worked on other blocks without shadow block.

Do I have to treat shadow block with special care?

To be honest, I don't really get the whole idea of shadow block. the doc is not explaining it well. In the example I provided(copied from Scratch-Blocks), I guess I can just replace the shadow block with a drop down list and the result is the same?


Beka Westberg

unread,
Feb 3, 2021, 5:36:28 PM2/3/21
to blo...@googlegroups.com
Hello!

This is a simple fix =)

You can see here that your SOUND_MENU isn't a /field/ it's a value input
```
       {
          "type": "input_value", // Here
          "name": "SOUND_MENU"
        }
```
This means that you have to use the valueToCode function, not that getFieldValue function.

> In the example I provided(copied from Scratch-Blocks), I guess I can just replace the shadow block with a drop down list and the result is the same?

Scratch's render does make this confusing :/ In Scratch shadow blocks are made to look like fields, but in core Blockly they look very different!
shadow.png
The semantic difference between them is that fields are basically literals. Eg 1, true, "string", etc. But shadow blocks can be replaced with other blocks, allowing you to create expressions :D
shadows-replaced.png
If you're interested, there's a blog post where I talk about shadows vs fields, and when you might want to use one vs the other =)

I hope that helps! If you have any other questions (or want to talk more about shadows!) 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/ef4fa738-e38a-4bd5-8503-a18f71bb2d45n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages