Conditionals and mutators

89 views
Skip to first unread message

Uzair Jamali

unread,
Feb 27, 2024, 2:57:56 AM2/27/24
to Blockly
Hi, how do I use mutators with if/else conditionals so that when if block is to be executed, I check some values and use mutators to dynamically change the condition value in if block, so at runtime it decides if blocks inside if/else are to be executed or not.

Thanks.

Beka Westberg

unread,
Feb 27, 2024, 2:54:30 PM2/27/24
to Blockly
Hello,

It sounds like you're trying to execute blocks based on their conditions? Blockly works slightly differently from this! In Blockly you don't execute the blocks directly, but instead you generate text-based code from them, and then use an interpreter (e.g. JSInterpreter, or eval()) to execute that text-based code.

So you can probably get this working with just the built-in if/else block!

Let me know if I didn't understand what you were asking =) If you have any further questions please reply!

Best wishes,
--Beka

Umer Nasir

unread,
Feb 28, 2024, 2:32:00 AM2/28/24
to Blockly

Hello Beka,

I think what he is trying to say is that based on the scenario mention in the screenshot attached that we need to move the character based upon the evaluation of conditional statements like if else. How can we achieve this? I think we need to evaluate this on runtime based on the character's position. Any ideas on how we can do this using blockly and JS Interpreter.?

Best wishes,
Umer


Screenshot from 2024-02-28 12-08-43.png

Beka Westberg

unread,
Feb 28, 2024, 4:07:29 PM2/28/24
to Blockly
Hello,

Could you share the text-based code that you're currently generating and the block-code generator that is generating it?

The block-code generator should probably look something like:

```
javascriptGenerator.forBlock['walk'] = function(block, generator) {
  // moveForward is a method you've defined in your application.
  return 'moveForward(10);\n';
}
```

Once I have a bit more information I can give you more specific advice!

Best wishes,
--Beka

Uzair Jamali

unread,
Feb 29, 2024, 6:53:01 AM2/29/24
to Blockly
Hello Beka,

Our move forward code has nothing to do with if/else block, as both of them are separate blocks.

Consider the image I have attached below, I need to set value of condition inside if block at runtime, as the character might not be at the same position when if block is executed as it was at the start of the code execution. You can see that I have attached walk block before if block, so character's position keep changing throughout. I need a way through which I can make blockly either go inside if block statement and run the blocks or go inside else statement and run the blocks there based on some position based calculations that I want the code to do at runtime when it is about to execute the if block. 

Right now, I have been able to change the value of condition at runtime, but that value doesn't apply for that execution and only reflects on successive executions. Is this the right approach or is there any other way through which I can accomplish my goal?

Screenshot from 2024-02-29 16-41-12.png

Thanks.

Beka Westberg

unread,
Feb 29, 2024, 11:35:03 AM2/29/24
to blo...@googlegroups.com
Hello again,

> I need a way through which I can make blockly either go inside if block statement and run the blocks or go inside else statement and run the blocks there

The interesting thing is that Blockly *doesn't* run blocks. What happens is that you generate text-based code and then you run that. For example, using `eval()` or JSInterpreter.

Blockly is actually just a very fancy string generator!

Could you paste the code of your block-code generators?

Best wishes,
--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/f0959e22-8ddd-4f78-9560-364c89392fcdn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages