Yello Beka.
I am soory i keep asking weustions anyways.
I've ran into another problem :/
So i am making a block wich creates objects.
Now i tried to make it like the list blocks
this is the code:
Blockly.Blocks['create_object'] = {
init: function() {
this.appendDummyInput()
.appendField("create object");
this.setOutput(true, null);
this.setColour("#F99EA3");
this.setTooltip("Create a new object with some value's (optionally) ");
this.setHelpUrl("");
this.setInputsInline(true);
this.setMutator(new Blockly.Mutator(['object']));
},
updateShape_: function() {
for (let i = 1; this.getInput('o' + i); i++) {
this.removeInput('o' + i);
}
// Rebuild block.
// Observe how it is looking at the `this.elseifCount_` property
for (let i = 1; i <= this.objectCount_; i++) {
this.appendValueInput('o' + i).appendField("").setAlign(Blockly.ALIGN_RIGHT);
}
},
decompose: function(workspace) {
var topBlock = workspace.newBlock('object_top_block');
topBlock.initSvg();
var connection = topBlock.getInput('object').connection;
for (var i = 0; i < this.objectCount_; i++) {
var c = workspace.newBlock('object');
c.initSvg();
connection.connect(c.previousConnection);
connection = c.nextConnection;
}
return topBlock;
},
compose: function(topBlock) {
// This line changed. Now you should actually be accessing the first case min-block
let toppBlock = topBlock.getInputTargetBlock('object');
// Count number of inputs.
this.objectCount_ = 0;
const valueConnections = [null];
while (toppBlock && !toppBlock.isInsertionMarker()) {
switch (toppBlock.type) {
case 'object':
this.objectCount_++;
valueConnections.push(toppBlock.valueConnection_);
break;
default:
throw TypeError('Unknown block type: ' + toppBlock.type);
}
toppBlock = toppBlock.nextConnection &&
toppBlock.nextConnection.targetBlock();
}
// Observe how it calls `updateShape_`
this.updateShape_();
this.reconnectChildBlocks_(
valueConnections);
},
mutationToDom: function() {
// You *must* create a <mutation></mutation> element.
// This element can have children.
var container = Blockly.utils.xml.createElement('mutation');
container.setAttribute('items', this.objectCount_);
return container;
},
domToMutation: function(xmlElement) {
this.objectCount_ = parseInt(xmlElement.getAttribute('items'), 10);
// This is a helper function which adds or removes inputs from the block.
this.updateShape_();
},
/**
* Reconnects child blocks.
* @param {!Array<?RenderedConnection>} valueConnections List of
* value connections for 'if' input.
* @param {!Array<?RenderedConnection>} statementConnections List of
* statement connections for 'do' input.
* @param {?RenderedConnection} elseStatementConnection Statement
* connection for else input.
* @this {Block}
*/
reconnectChildBlocks_: function(
valueConnections ) {
for (let i = 1; i <= this.objectCount_; i++) {
Blockly.Mutator.reconnect(valueConnections[i], this, 'o' + i);
}
},
saveConnections: function(topBlock) {
let clauseBlock = topBlock.getInput('object').connection;
let i = 1;
while (clauseBlock) {
switch (clauseBlock.type) {
case 'object': {
const inputIf = this.getInput('o' + i);
clauseBlock.valueConnection_ =
inputIf && inputIf.connection.targetConnection;
i++;
break;
}
default:
throw TypeError('Unknown block type: ' + clauseBlock.type);
}
clauseBlock = clauseBlock.nextConnection &&
clauseBlock.nextConnection.targetBlock();
}
},
};
And throws the following error in both compose and saveConnections
I am not sure what i've done wrong so that's why i am asking for some help.
THANKS
Op vrijdag 21 januari 2022 om 14:45:39 UTC+1 schreef Sketch: