Get Output only when blocks are connected

132 views
Skip to first unread message

AAGAM JAIN

unread,
May 15, 2021, 2:40:27 AM5/15/21
to Blockly
I have created a statement input and a dropdown and I want to display answers only when the two statements are connected in the workspace. Here is my code: 
Blockly.Blocks['bot'] = {
  init: function() {
    this.appendStatementInput("BOT")
        .setCheck(null)
        .appendField("bot");
    this.setColour(225);
    this.setTooltip("");
    this.setHelpUrl("");
  }
};

var code="";
Blockly.JavaScript['bot'] = function(block) {
  var statements_bot = Blockly.JavaScript.statementToCode(block, 'BOT');
  return code;
};

Blockly.Blocks['dropdown'] = {
  init: function() {
    this.appendValueInput("dropdown_ques")
      .appendField("Ask me a Question: ")
      .appendField(new Blockly.FieldDropdown([["select", "select"], ["What is the date today?", "date"], ["What is the time now?", "time"], ["How are you?", "howru"], ["What is Javascript?", "javascript"], ["What is your name?", "yourname"]]), "Questions");
    this.setInputsInline(false);
    this.setPreviousStatement(true, null);
    this.setColour(300);
    this.setTooltip("");
    this.setHelpUrl("");
  }
};

Blockly.JavaScript['dropdown'] = function (block) {
  var question = block.getFieldValue('Questions');
  // If Question is not selected
  var ques = "Please select a question";

  // Question 1 answer
  var today = new Date();
  var dd = String(today.getDate()).padStart(2, '0');
  var mm = String(today.getMonth() + 1).padStart(2, '0'); 
  var yyyy = today.getFullYear();
  var dateToday = dd + '/' + mm + '/' + yyyy;
  
  // Question 2 answer
  var hh = String(today.getHours()).padStart(2,'0');
  var mi = String(today.getMinutes()).padStart(2,'0');
  var ss = String(today.getSeconds()).padStart(2,'0');
  var timeToday = hh + ':' + mi + ':' + ss;

  // Question 3 answer
  var how = "I am fine.";

  // Question 4 answer
  var js = "JavaScript is the Programming Language for the Web.";

  // Question 5 answer
  var myName = "My name is Aagam Jain."
  
  if (question === "select") {
    code = `var inputTextValue ="${ques}";`;
  }
  else if (question === "date") {
    code = `var inputTextValue = "${dateToday}";`;
  }
  else if (question === "time") {
    code = `var inputTextValue = "${timeToday}";`;
  }
  else if (question === "howru") {
    code = `var inputTextValue = "${how}";`;
  }
  else if (question === "javascript") {
    code = `var inputTextValue = "${js}";`;
  }
  else if (question === "yourname") {
    code = `var inputTextValue = "${myName}";`;
  }
  return code;
};

Beka Westberg

unread,
May 15, 2021, 3:04:02 PM5/15/21
to blo...@googlegroups.com
Hello,

Your block-code generators look a little bit different from what I'm used to, so forgive me if I misunderstand anything hehe. Are you putting your 'dropdown' blocks inside of your 'bot' block, and then you want to generate "code" that contains the answers?  I think that usually you would generate code that finds the answers instead of encoding them directly. Eg:
```
Blockly.JavaScript['dropdown'] = function (block) {
  var question = block.getFieldValue('Questions');
  return 'alert(getAnswer(' + question + ');\n';
};
```
So I just want to make sure I'm understanding!

Now I think that if you want to check if the blocks are connected, your best bet is to programmatically check if the statement input of your 'bot' block has a target block. For example:
```
var bot = myWorkspace.getBlocksByType('bot')[0];
var target = bot.getInputTargetBlock('BOT');
if (target) {  // there is a block connected (presumably a question)
  // generate code.
}
```

I hope that helps! If you have any further questions, please reply!
--Beka

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/f1c77e9d-cba0-494a-b1f0-a114e4512f18n%40googlegroups.com.
Message has been deleted

AAGAM JAIN

unread,
May 16, 2021, 2:39:32 AM5/16/21
to Blockly
Thank you so much. It's working but I didn't understand the working of the return statement you mentioned above. If you could explain? 

AAGAM JAIN

unread,
May 16, 2021, 3:19:16 AM5/16/21
to Blockly
Should I also change the return statement of the bot as if only the bot is present it is showing the answer to the previous question selected? I want it to work both ways i.e. it should work if and only if they are connected or else it should not work.
On Sunday, May 16, 2021 at 12:34:02 AM UTC+5:30 bekawe...@gmail.com wrote:

Beka Westberg

unread,
May 17, 2021, 6:15:56 PM5/17/21
to blo...@googlegroups.com
Hello,

So generally blockly generates code strings which are then run using some sort of interpreter.

For example, if you have a group of blocks like this:
2+2.png
It would generate the code string "2 + 2". It would not generate 4 or "4".

It seems like your current setup is trying to do more of the second thing, where the logic lives in the generator. There's nothing wrong with doing it this way =) It's just a bit different from how people normally use Blockly hehe. So I wanted to make sure I understood what you were trying to say.

Does that answer your question? Or did I miss the mark again? hehe

Best wishes,
--Beka

Reply all
Reply to author
Forward
0 new messages