1

I am working on a Blockly Project in which I am passing data from the blocks as JavaScript code. I would like to alter some of the code that is output from the blocks to be a bit more beginner-friendly.

It is possible to alter the output JS code from the blocks by editing the code in the 'generators/javascript' folder, however the Variable Block with the heading "Change by " (picture below) does not seem to have a generator in the generators/javascript/variables.js file, yet it still generates code as is, without making any changes to the variables.js file.

The "Change Variable by Value" Block

Basically, I want to be able to edit the code output from this "Change" block, however I cannot find a way to do this currently. Any help would be greatly appreciated.

ggorlen
  • 44,755
  • 7
  • 76
  • 106

1 Answers1

0

In Blockly, not all of the blocks associated with a category are necessarily found within the named category file. Such is the case here.

A good way to figure out which file and function to look at is by heading over to Blockly demos - code editor which lets you export blocks as XML. Drag the block you're interested in examining the generator source for into the workspace, in this case it's the "variable -- change by" block, then export to XML:

<xml xmlns="https://developers.google.com/blockly/xml">
  <variables>
    <variable id="F;)U+)(_^:3!Z~mxvyWO">asd</variable>
  </variables>
  <block type="math_change" id="xXz{jIXrbvaz9DcE*7O6" x="338" y="88">
    <field name="VAR" id="F;)U+)(_^:3!Z~mxvyWO">asd</field>
    <value name="DELTA">
      <shadow type="math_number" id="d5W].A}ldwUj2~+0PKnj">
        <field name="NUM">1</field>
      </shadow>
    </value>
  </block>
</xml>

This shows block type="math_change", so we follow the math_ prefix to blockly/generators/javascript/math.js instead of variables.js:

blockly.javascript['math_change'] = function(block) {
  // add to a variable in place.
  var argument0 = blockly.javascript.valuetocode(block, 'delta',
      blockly.javascript.order_addition) || '0';
  var varname = blockly.javascript.variabledb_.getname(
      block.getfieldvalue('var'), blockly.variable_category_name);
  return varname + ' = (typeof ' + varname + ' == \'number\' ? ' + varname +
      ' : 0) + ' + argument0 + ';\n';
};

Add this function to your script to override the existing implementation and replace it with your desired logic.

ggorlen
  • 44,755
  • 7
  • 76
  • 106