How to save my block code with variables?

Posted on
  • Hello there.
    I'm developing a site which can control an educational robot(by MDBT42Q).

    Here's my block code that I want to save and load.
    image
    (If you can't see photo, I attached at below.)
    It looks simple. First dropdown has two options(LEFT, RIGHT) and LEFT is 0, RIGHT is 1.
    Second dropdown has two also, CW is 0 , CCW is 1.

    It's a converted javascript code.

    /* motor_move function code */
    motor_move(1, 0, 0, 0, false);
    //motor_move's parameters : motor_move(motor, direction, velocity, angle, wait);
    

    If I set 'RIGHT' motor at block code and save my code, and then I load it and convert, the code shows

    motor_move(0,0,0,0,false);
    

    So the loaded block code shows 'RIGHT' motor set, but actually robot moves 'LEFT' motor.

    How can I save block code with right parameters that I set before?

    Help me please!


    1 Attachment

    • 캡처.PNG
  • Honestly, without seeing the blockly code you're using its hard to really know what is going on.

    Do you have a link to it in GitHub or something?

  • Hi Gordon!
    Thanks for reply.

    https://github.com/junguksim/project_cod­ubot/blob/master/blockly/blockly_codubot­.js
    You can watch first block(codubot ready)'s code at first line.
    Second block(motor_move)'s code is at line 178.

    If you want more, please give me reply.

  • Thanks - I think you need to breakpoint your code eg Blockly.JavaScript.motor_move_by_angle and step through it with the chrome devtools.

    As far as I can tell:

    Blockly.JavaScript.motor_move_by_vel = function(block) {
        let vel = Blockly.JavaScript.valueToCode(this, 'vel', Blockly.JavaScript.ORDER_ASSIGNMENT) || '""';
        if(motorValid_ === false) {
            motor_ = 0;  <-----------------------  here because motorValid never got set
        }
        else {
            motor_ = block.getFieldValue("turnMotor_") <--- never gets called
    

    My guess is validate only ever gets called on user input, and not when the code is loaded.

    But either way a global motorValid_ sounds bad - for instance it's just set to the valid state of the last block checked, not to that of all the blocks. Personally I'd remove validate and just check what's going on when creating the code in Blockly.JavaScript.*

  • The reason why I use validate is, when I don't choose any value in dropdown, so the default value should be set, but unfortunately it doesn't. The value was undefined when I don't select any value in dropdown!

    if(motorValid_ === false) {
            motor_ = 0;  <-----------------------  here because motorValid never got set
        }
        else {
            motor_ = block.getFieldValue("turnMotor_") <--- never gets called
    

    So that block.getFieldValue("turnMotor_") line works well, because of validator.

    I can't think the way that just check what's going on when creating the code in Blockly.JavaScript.*.

    Can you explain with any examples?

  • I mean, you could just do:

    motor_ = block.getFieldValue("turnMotor_");
    if (motor_===undefined) motor_=0;
    

    So now you check that value inside Blockly.JavaScript.motor_move_by_vel - if it's undefined you set the default value.

    But what you were doing wouldn't work properly anyway - for instance if you make a bunch of blocks, and just the very last block doesn't have a motor set, all of the blocks will then have the motor set to 0 which I guess isn't what you want

  • Thank you Gordon!
    It works perfectly with your solution.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

How to save my block code with variables?

Posted by Avatar for user110706 @user110706

Actions