Blockly.Block.obtain is not working

42 views
Skip to first unread message

Lalitha Noothigattu

unread,
Sep 11, 2020, 7:55:59 AM9/11/20
to blo...@googlegroups.com
Hi All,

I am trying to get a reference of block from when loading the blockly.

var blc = Blockly.Block.obtain(Blockly.getMainWorkspace(), 'static_string_value');


Blockly.Blocks['static_string_value'] = {
            init: function() {
                this.appendDummyInput()
                    .appendField(new Blockly.FieldTextInput("Static String Value"), "value");
                this.setInputsInline(true);
                this.setOutput(true);
                this.setColour('#3ea6cb');
                this.setTooltip('');
                this.setHelpUrl('http://www.example.com/');
            }
        };


Blockly.Block.obtain is throwing undefined error
I am trying to replace existing values with new blocks.

It worked fine for old blockly. Now we have upgraded to version"3.20191014.4" and Blockly.Block.obtain is not working 

Any suggestions?

Thanks & Regards
Lalitha

Beka Westberg

unread,
Sep 11, 2020, 9:45:29 AM9/11/20
to blo...@googlegroups.com
Hello,

Yeah I don't think obtains works anymore :/ The new recommended method of getting blocks programmatically is to do something like the following:

```
var block = workspace.newBlock('my_block_type');
block.initSvg();
block.render();
```

I hope that helps! If you have any further questions feel free to reply :D
--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/CAF-Qq_cX749b6g4kk8s3FTnzVyWUhGEE_fprew0XezN7pfQ%2BEg%40mail.gmail.com.

Lalitha Noothigattu

unread,
Sep 11, 2020, 9:50:08 AM9/11/20
to blo...@googlegroups.com
Hi, 

Thanks for the reply. 
I am unable to add value to the block and append to the parent block. Empty field is shown instead of new block

Screen Shot 2020-09-11 at 7.18.02 PM.png

Expected
Screen Shot 2020-09-11 at 7.19.12 PM.png

Code:

var blc = Blockly.getMainWorkspace().newBlock('static_string_value');
blc.setFieldValue(value, 'value');
this.appendValueInput('RHS_INPUT').connection.connect(blc.outputConnection);


Thanks
Lalitha


Beka Westberg

unread,
Sep 11, 2020, 10:05:10 AM9/11/20
to blo...@googlegroups.com
Hello,

Blocks obtained via newBlock need to be initialized and rendered "manually". The following code should work:

```
var blc = Blockly.getMainWorkspace().newBlock('static_string_value');
blc.initSvg();
blc.render();

blc.setFieldValue(value, 'value');
this.appendValueInput('RHS_INPUT').connection.connect(blc.outputConnection);
```

Best!
--Beka

Lalitha Noothigattu

unread,
Sep 11, 2020, 10:16:26 AM9/11/20
to blo...@googlegroups.com
Hi,

I have tried, but still not working. Any other alternative?

Thanks
Lalitha



Lalitha Noothigattu

unread,
Sep 11, 2020, 12:43:54 PM9/11/20
to blo...@googlegroups.com
Hi,
Blocks are not connecting, instead showing on top of the rules. please suggest how to connect.
Screen Shot 2020-09-11 at 10.11.19 PM.png


code:

this.appendDummyInput("RHS_value_INPUT")
                            .appendField("=");
                            if(value !== null && value !== undefined ){

                                var blc = Blockly.getMainWorkspace().newBlock('static_string_value');
                                blc.initSvg();
                                blc.render();
                                blc.setFieldValue(value, 'value');
                                this.appendValueInput('RHS_INPUT').connection.connect(blc.outputConnection);
                            }


Thanks 
Lalitha

Beka Westberg

unread,
Sep 12, 2020, 10:09:30 AM9/12/20
to blo...@googlegroups.com
Hmm... All the code looks right, so it should be connecting. Are you getting any console errors?

--Beka

Lalitha Noothigattu

unread,
Sep 12, 2020, 10:11:21 AM9/12/20
to blo...@googlegroups.com
Hi,


No error message is showing up in console. But new blocks are shown on the top with the value.

Thanks 
Lalitha


Beka Westberg

unread,
Sep 12, 2020, 10:19:04 AM9/12/20
to blo...@googlegroups.com
Sorry I don't know what could be the issue then :/ I wish I could be more helpful. Hopefully someone else here will know what to do!

--Beka

Abby Schmiedt

unread,
Sep 14, 2020, 7:42:47 PM9/14/20
to Blockly
Hello!

Some debugging you might want to try: 
1. What is this.appendValueInput('RHS_INPUT') returning? 
2. Double check that your connections are the correct type
3. I think Blockly.getMainWorkspace() is a bit concerning. Try changing to this.workspace instead. 

Is this happening in the block init method?  

Cheers, 
Abby

Andressa Oliveira

unread,
Nov 16, 2021, 8:41:09 PM11/16/21
to Blockly
Hello, 
To connect the blocklys, I got the following code:

var blc = this.workspace.newBlock("type");
blc.initSvg();
blc.render();
var parentConnection = this.getInput("input name").connection // named input or nextConnection
parentConnection.connect(blc.outputConnection);

Tks


Reply all
Reply to author
Forward
0 new messages