Get value from another blockly

865 views
Skip to first unread message

Riderman Sousa

unread,
Jul 11, 2017, 11:15:35 PM7/11/17
to Blockly
After select the car, I need to get the car selected in the next statement


I removed some code leaving just important parts

Blockly.Blocks.car_selection = {
  init() {
    const cars = STORE.getCars();
    const carsSelection = cars.map(car => [car.name, car._id]);
    this.appendStatementInput('CarSelection')
      .appendField('Select the Car')
      .appendField(new Blockly.FieldDropdown(carsSelection), 'Car');
    this.setNextStatement(true, 'Number');
  },
};

Blockly.Blocks.car_movement = {
  init() {
    this.appendValueInput('Movement')
      .setCheck('Array')
      .appendField('Go X')
      .appendField(new Blockly.FieldNumber(0, 0, 20000), 'posX')
      .appendField('Y')
      .appendField(new Blockly.FieldNumber(0, 0, 20000), 'posY');
    this.setPreviousStatement(true, 'Number');
    this.setNextStatement(true, null);
  },
};



And the code ...
In the car_movement blockly, how can I get the car selected?


Blockly.JSON.car_selection = function(block) {
  const car = block.getFieldValue('Car');

  const statements = statementsCode
    .split('\n')
    .filter(it => it.length > 0)
    .map(it => JSON.parse(it));

  const command = {
    topic: `to/car/${robotIdentifier}`,
    packet: statements,
  };

  return `${JSON.stringify(command, null, 2).trim()}\n`;
};

Blockly.JSON.car_movement = function(block) {
  const x = block.getFieldValue('posX');
  const y = block.getFieldValue('posY');

  // TODO HOW TO GET CAR ID FROM PREVIOUS BLOCKLY ?
  // const car = STORE.getCar(carId);  <-- I NEED THE CAR ID

  const jsonSpec = {
    x,
    y,
    currentX: car.x,
    currentY: car.y,
  };

  return `${JSON.stringify(jsonSpec).trim()}\n`;

Erik Pasternak

unread,
Jul 12, 2017, 4:12:55 PM7/12/17
to Blockly
Hi Riderman,

I'm not sure I understand how you're using JSON as a programming language. Typically blockly generates pure text that can then be interpreted, but you seem to be creating an object hierarchy and then converting it to text. Do you have another process that is parsing the json strings and then executing code based on the object order?

Without further info about how you're using the JSON, I would implement this by making the car part of car_selection, and make keeping track of which car each statement acts on part of the execution environment instead of explicitly adding it to each statement.

Cheers,
Erik
Message has been deleted

Andrew Stratton

unread,
Jul 13, 2017, 5:05:02 PM7/13/17
to Blockly
You can use getSurroundParent().

i.e. in car_movement, you can call block.getSurroundParent(), which will return the parent block - from which you can then get values, etc. e.g. you would need something like:

// in car_movement

block
.getSurroundParent().getFieldValue('Car')

You will probably need to check that the parent block is the right type of block - and recurse upwards until you find a block of the right type - or finish.

I have a function that does this for me - I've cut it down and pasted it below:

    var getParent = function (block, valid_ids) {
        var found = false;
        var check_block = block.getSurroundParent();
        if (check_block != null) {
            do {
                if (valid_ids.indexOf(check_block.type) !== -1) {
                    found = check_block;
                } else {
                    check_block = check_block.getSurroundParent();
                }
            } while ((found === false) && check_block);
        }
        if (found === false) {
            found = undefined; //TODO code refactor?
        }
        return found;
    };

This works when you pass in the current block, and a list of ids of blocks (e.g. the id of the car block) that you want to find as a parent - this allows you to find the 'nearest' parent block of certain types. So you could have 'select the car' and 'select the bus' containing blocks that can both be accessed by contained blocks for generation.

Hope this makes sense
  Andy Stratton

Reply all
Reply to author
Forward
0 new messages