How to get block input_value's for generators
See original GitHub issueHow can we access the blocks input values in a generator?
Here is the Motion - Move Steps block definition:
Blockly.Blocks['motion_movesteps'] = { init: function() { this.jsonInit({ "message0": "move %1 steps", "args0": [ { "type": "input_value", "name": "STEPS" } ], "category": Blockly.Categories.motion, "extensions": ["colours_motion", "shape_statement"] }); } };
if we create a generator for this block as:
Blockly.Python['motion_movesteps'] = function(block) { var duration = block.getFieldValue('STEPS'); return duration + '\n'; };
we would want the getFieldValue function to return the input value on the block. However the duration variable returns null.
I noticed in the Scratch block definition the type is ‘input_value’, as oppose to ‘field_value’ in Blockly, and that in the xml generated for that block the input value actually appears inside a ‘shadow’ tag.
So, is there is simple way to access this input value via the generator?
Many thanks for the help…
Issue Analytics
- State:
- Created 6 years ago
- Comments:6
Top GitHub Comments
Sorry, have solved this.
Was using getFieldValue instead of valueToCode.
Changing to valueToCode in the generator has solved the problem. Final line to get input value looks like:
var duration = Blockly.Python.valueToCode(block, 'STEPS', Blockly.JavaScript.ORDER_ATOMIC) || '0'
@smaameri thank you so much for detailed explanation, in my case I just wanna define a common block with a text input field in horizontal mode, then get the input value.