Get value from variable (part 2)

492 views
Skip to first unread message

Felix Wong

unread,
Mar 7, 2022, 2:42:05 PM3/7/22
to Blockly
Hi,

I was following this thread and tried to reproduce the scenario described there:

Here's the code:
Blockly.Blocks['myblock'] = {
  init: function() {
    this.appendDummyInput()
      .appendField("hello")
      .appendField(new Blockly.FieldVariable("item"), "MYVAR");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(20);
    this.setTooltip('');
    this.setHelpUrl('http://www.example.com/');
  }
};

Blockly.JavaScript['myblock'] = function(block) {
  var myvar = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('MYVAR'), Blockly.Variables.NAME_TYPE);
  alert(myvar);
  var code = 'alert("got something: " + ' + myvar + ');';
  return code;
};
However, when I ran it using the "Generating Javascript" demo, I get this error (see screenshot) with a garbage string instead of a variable.  Any hint where the problem is?  

Thanks in advance!
-Felix

Screen Shot 2022-03-07 at 11.36.11 AM.png

Maribeth Bottorff

unread,
Mar 7, 2022, 9:21:13 PM3/7/22
to Blockly
Hi, are you using a recent version of Blockly? If so, then `Blockly.Variables.NAME_TYPE` does not exist and therefore the lookup of the variable name is failing (it's giving you the id of the variable instead of the name). Try replacing that with `Blockly.Names.NameType.VARIABLE`.

For reference, looking at how the default blocks work is a handy way to figure out what's going on. In this case to answer this question I compared your code to the code for the default variable setter block: https://github.com/google/blockly/blob/master/generators/javascript/variables.js and noticed the differences. You'll also note that `variableDB_` was renamed to `nameDB_` but in that case your code still works as we aliased the old name so that you can still use it (although you might notice a deprecation warning encouraging you to change to the new one as the old API will be deleted in the future).

Hope that helps,

Maribeth

Felix Wong

unread,
Mar 7, 2022, 11:38:36 PM3/7/22
to Blockly
Hi Maribeth,

Thank you for responding.  Upon inspecting variables.js, I think you meant 'Blockly.Names.VARIABLE' and not `Blockly.Names.NameType.VARIABLE`.

So I followed your suggestion and modify the line to this:
 var myvar = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('MYVAR'), Blockly.Names.VARIABLE);
However, I am still getting the id of the variable instead of the name.  Any help is appreciated.  

Thanks
-Felix

Maribeth Bottorff

unread,
Mar 8, 2022, 12:23:39 AM3/8/22
to Blockly
Hi,

I do believe it should be `Blockly.Names.NameType.VARIABLE` but in this case we aren't exporting `NameType` from the Blockly module correctly... I think that's a bug on our end and I'll follow up.
In the meantime try just passing the string `'VARIABLE'` and I think that should work.

Please try that and let us know. Thanks,

Maribeth

Felix Wong

unread,
Mar 8, 2022, 1:33:09 AM3/8/22
to blo...@googlegroups.com
Hi Maribeth,

Yup, "VARIABLE" did the trick.  Thank you!

Thanks
-Felix

--
You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/JzVgbKEcyaw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/ff703336-c4c2-4352-9bb4-44c042fd0f51n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages