How to get value from variable

521 views
Skip to first unread message

Jonas Costa

unread,
Jun 28, 2021, 4:37:09 PM6/28/21
to Blockly
Hello!

The research group that I'm part of and I are struggling with getting the value from a variable.

If we place a variable block inside a value block, as shown in the image, the output is the name of the variable, not its value.

Captura de tela 2021-06-28 152317.jpg

In this case, when we run the code, the output is "number_of_ducks little ducks went out one day", instead of "4 little ducks went out one day".

However, if we place only the variable block, as shown in the image below, the code runs as expected.
Captura de tela 2021-06-28 152732.jpg
In this case, the output is "4", which is what we expect.

The code that we currently have for the value block is:

Blockly.JavaScript['rhyme_little_ducks'] = function(block) {
    Blockly.JavaScript.init(workspace);
    var value_count = Blockly.JavaScript.valueToCode(block, 'count', Blockly.JavaScript.ORDER_ATOMIC);
    var text = block.getFieldValue('TEXT');
    return ['\'' + value_count + ' ' + text + '\'', Blockly.JavaScript.ORDER_ATOMIC];
}


Any ideas on how to fix this problem?

Thank you very much!

Jason Schanker

unread,
Jun 28, 2021, 6:15:07 PM6/28/21
to Blockly
Hi,

The return statement should be [value_count + '\' ' + text + '\'', Blockly.JavaScript.ORDER_ADDITION] since value_count is storing the code string produced by the block with the dropdown, which is presumably its field value of the selected item, the string literal number_of_ducks.  This way the generated code from the rhyme_little_ducks will have number_of_ducks outside of the quotation marks so the value of this variable is evaluated when the code is run.  Your code included rhyme_little_ducks as part of the string literal so it was being interpreted literally as a string.

You'll notice that another change to make is to use Blockly.JavaScript.ORDER_ADDITION instead of Blockly.JavaScript.ORDER_ATOMIC in the return value.  By using Blockly.JavaScript.ORDER_ATOMIC, you were essentially saying that nothing could split the generated code up from this block so it would never be nested inside of parentheses, i.e., that another operator from a surrounding block couldn't be performed first with part of this expression.  However, if you put this in say a .length block, the corrected version would lead to generated code of number_of_ducks + ' little ducks went out one day'.length so that the length of only the text of this expression without the number would be taken first.  This would add the number of ducks to the number of characters in the string instead of correctly getting the number of characters in the concatenated string (i.e., (number_of_ducks + ' little ducks went out one day').length). 


Best,
Jason

Jonas Costa

unread,
Jun 28, 2021, 8:40:37 PM6/28/21
to Blockly
Hello!

Thank you very much, Jason. That was very helpful. I also needed to add a plus sign to combine both parts. The return statement that worked is [value_count + ' + \'' + text + '\'',  Blockly.JavaScript.ORDER_ADDITION], and now we get the correct output.

Best,
Jonas

Jason Schanker

unread,
Jun 28, 2021, 10:58:26 PM6/28/21
to Blockly
Happy to help, Jonas, and sorry for the mistaken omission of the extra plus!  Just as an additional note, if you're able to use ECMAScript 6 in your code, template literals can make the return values for your generators much more readable.  For example, here you could use `${value_count} + '${text}'` for the string of code.
Reply all
Reply to author
Forward
0 new messages