Block Connection Events

936 views
Skip to first unread message

Jared G

unread,
Mar 4, 2015, 12:04:49 PM3/4/15
to blo...@googlegroups.com
Hi,

Is there anyway to subscribe to a connection event for a block? That is, if a user connects two blocks together, does Blockly fire any events for this?

Basically my use case is that we have a block that is currently using the inbuilt mutator mechanic, but we want it to auto-expand/shrink when a user connects a block since they might not know ahead of time how many they need. From my research in the source, it doesn't seem that I can do this currently.

If I need to make this change on my own, what would be the best place to go about doing so?

Thanks & Regards,
Jared

Neil Fraser

unread,
Mar 4, 2015, 12:14:21 PM3/4/15
to blo...@googlegroups.com
Blocks can define an 'onchange' function that gets fired whenever anything changes.  You can add code to check if the event is one that you are interested in.  Here are a couple of examples:

https://github.com/google/blockly/blob/master/blocks/loops.js#L251
https://github.com/google/blockly/blob/master/blocks/logic.js#L304

--
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.
For more options, visit https://groups.google.com/d/optout.



--

Jared G

unread,
Mar 4, 2015, 5:15:46 PM3/4/15
to blo...@googlegroups.com, ro...@neil.fraser.name
Awesome! Thank you, this should help me do what I need to do.

Jared

Cody Blakeney

unread,
Jan 6, 2017, 12:02:56 AM1/6/17
to Blockly, ro...@neil.fraser.name
Hi Neil, 

I have a similar problem

I have been looking at the examples. I can't seem to figure out how one would take the 'onchange' function and limit it to acting only when a block has made a connection.

my use case is slightly different.

I am trying to modify the 'math_arithmetic' block to allow use with vector math (with a self created vector block).

When connecting a block of type "Vector" I want it to limit the options for operators. 
getting it to provide the appropriate options hasn't been a problem using onchange as I have it replace the fieldDropdown with a new version with appropriate options, but because it does it when anything changes instead of just a new connection it is constantly overriding the selection of the fieldDropdown by the user. Any ideas?   

Ricardo Rodriguez

unread,
Aug 9, 2019, 11:21:00 AM8/9/19
to Blockly
Cody,

Did you find a solution to this?  I have the same requirement you do, it seems.  I want to be able to restrict the types of blocks added to a list block, and I want to do the check at the point a connection is made, i.e. show a dialog stating why the connection cannot be made.

Basically, something similar to the "setCheck" method with value inputs, with connections.

Coda Highland

unread,
Aug 9, 2019, 11:30:12 AM8/9/19
to blo...@googlegroups.com
You can't limit when the event fires, but what you CAN do is just validate that the dropdown is in the right mode based on the attachment on every event. It shouldn't take too long so it shouldn't have a noticeable impact on performance, and you only need to have it actually change anything if the current attachment doesn't match the current state of the dropdown.

/s/ Adam

Ricardo Rodriguez

unread,
Aug 9, 2019, 11:36:56 AM8/9/19
to Blockly
Cody,

Thanks for the reply!

Not sure what you mean by "dropdown is in the right mode." 

What dropdown are you referring to?  The variable block to which I'm connecting the list to?

I'm actually creating my own list block, and I'm trying to restrict the connection types to that list block.  For instance, If I make a "string list block," only want strings to attach to it.  And if I do try to attach another block that isn't a string, some sort of connection event will trigger for me to check the respective connection block type.

The items you add to a list are not inputs, so it's a little different, in that I can't seem to find a connection listener/event to subscribe to.



On Friday, August 9, 2019 at 11:30:12 AM UTC-4, Coda Highland wrote:
You can't limit when the event fires, but what you CAN do is just validate that the dropdown is in the right mode based on the attachment on every event. It shouldn't take too long so it shouldn't have a noticeable impact on performance, and you only need to have it actually change anything if the current attachment doesn't match the current state of the dropdown.

/s/ Adam

To unsubscribe from this group and stop receiving emails from it, send an email to blo...@googlegroups.com.

Coda Highland

unread,
Aug 9, 2019, 11:58:53 AM8/9/19
to blo...@googlegroups.com
Ah, sorry, I'm not Cody. I'm Coda. Close, but no cigar. ;)

I do apologize for getting confused. Changing up the dropdown was something from the post you were replying to, not anything related to your own question. Feel free to disregard THAT part.

The items you add to a list aren't inputs, they're values. But the connection that lets you add the item to the list IS an input, and you can validate the attached block that way. You should be able to validate all of the attachments on all of the inputs on every change event. As I said, that shouldn't be too expensive. If the validation fails, you can disconnect the block and pop up an error message as you see fit.

That said... what's wrong with setCheck()? That's really what it's designed for.

/s/ Adam



Reply all
Reply to author
Forward
0 new messages