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`;
// in car_movement
block.getSurroundParent().getFieldValue('Car') 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; };