How to pass dropdown value to parent block and display the corresponding output?

278 views
Skip to first unread message

Shashank Yadav

unread,
Aug 6, 2021, 1:28:02 PM8/6/21
to Blockly
I am trying to use a block which will have statement input block and then another block which is just a dropdown. I have defined all the corresponding values in the dropdown such that if user selects any one value they'll get the corresponding output but I need to pass the corresponding value to my parent block and then display the answer. 

Statement Input Block:-

Blockly.Blocks['bot'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("Bot");
    this.appendStatementInput("FIELDNAME")
        .setCheck(null);
    this.setColour(230);
 this.setTooltip("");
 this.setHelpUrl("");
  }
};

Dropdown block:-

Blockly.Blocks['ask_me_a_question'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("Ask me a question")
        .appendField(new Blockly.FieldDropdown([
          ["What is the date today?","Date"], 
          ["What is the time now?","Time"], 
          ["How are you?","Feeling"], 
          ["What is JavaScript?","JavaScript"], 
          ["What is your name?","Name"]
        ]), "FIELDNAME");
    this.setPreviousStatement(true, null);
    this.setColour(230);
 this.setTooltip("");
 this.setHelpUrl("");
  }
};

Basically I am not able get the dropdown selected question. Please help to get the respective value an display the answer in a separate place.

Javascript:

Blockly.JavaScript['bot'] = function() {
  var statements_bot = Blockly.JavaScript.statementToCode(block, 'FIELDNAME');
  var dropdown_ask_me_a_question = block.getFieldValue('FIELDNAME');

  var question;
  switch(dropdown_ask_me_a_question) {
    case "Date":
      var today = new Date();
      question = today.getDate()+'/'+(today.getMonth()+1)+'/'+today.getFullYear();
      break;
    case "Time":
      var today = new Date();
      question = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds() + " IST";
      break;
    case "Feeling":
      question = "I am good, How are you?";
      break;
    case "JavaScript":
      question = "JavaScript is a text-based programming language used both on the client-side and server-side that allows you to make web pages interactive";
      break;
    case "Name":
      question = "I am a bot created by Shashank Yadav using blockly";
      break;
}
  var code = `
  var inputTextValue = "${question}";
  `;
  return code;
};

Capture.PNG

Please help!!

Neil Fraser

unread,
Aug 7, 2021, 3:18:33 AM8/7/21
to Blockly
The first issue is that you really should be naming your fields.  Currently everything is named 'FIELDNAME'.

The second issue is that you'll need a generator function for the ask block:
Blockly.JavaScript['ask_me_a_question'] = function(block) {
  return block.getFieldValue('FIELDNAME')
};

The third issue is that the bot block's JavaScript generator function should pass 'block' as an argument:
Blockly.JavaScript['bot'] = function(block) {

And finally change the top line of the bot block's generator function to be:
var dropdown_ask_me_a_question = Blockly.JavaScript.statementToCode(block, 'FIELDNAME');
Reply all
Reply to author
Forward
Message has been deleted
0 new messages