Getting a list of blocks that are currently placed on workspace?

35 views
Skip to first unread message

Breno Alves

unread,
Jul 17, 2024, 12:35:24 PM (5 days ago) Jul 17
to Blockly
Hello everyone, i'm making an educational game using blockly and currently having two big problems, all blocks in my workspace are custom blocks i made and they all work as intended.

First problem (more urgent) is that i really need to get something like an array of all the blocks that are currently placed, either by ID or block name...

Second problem is that i cant seen to find a way to reference placed blocks, for example, i want to highlight blocks as my game runs, i know there's a blockly function called BlockSvg.addSelect() but i cant seen to find a way to implement it and make it work on the blocks i want!

Can anyone help me? i've been cracking my head against this for a few weeks on and off now, its hindering my project too much.

Thank you all very much for your time, happy coding!

Beka Westberg

unread,
Jul 18, 2024, 12:44:09 PM (4 days ago) Jul 18
to Blockly
Hello,

For your first problem, why do you want to get an array of all of the blocks? You can call `workspace.getAllBlocks`. But if I know what you're trying to do I might be able to recommend a more specific method!

For you first problem, have you checked out our docs on highlighting blocks during code execution? You definitely don't want to use `addSelect` because selection is different from highlighting. Hopefully that gives you a good place to start =)

I hope that helps! If you have any further questions please reply!
--Beka

Breno Alves

unread,
Jul 18, 2024, 12:56:59 PM (4 days ago) Jul 18
to Blockly
Hello Beka, yeah so about the first problem, yes i've tried using getAllBlocks, it returns me almost what i want, an array of the placed block objects, but i dont know how i can get information from said block objects, for example, lets say:

(running in a for loop)
if( blockList[0].type = "blockA"){
    print something to terminal
}
and so on  so i can do specific thing inside the game based on which blocks are placed!

About the second problem, no i havent read this highlight section but it looks promising, i'll check it out, thank you very much!

Christopher Allen

unread,
7:58 AM (10 hours ago) 7:58 AM
to blo...@googlegroups.com
Hi Breno,

Hello Beka, yeah so about the first problem, yes i've tried using getAllBlocks, it returns me almost what i want, an array of the placed block objects, but i dont know how i can get information from said block objects, for example, lets say:

(running in a for loop)
if( blockList[0].type = "blockA"){
    print something to terminal
}
and so on  so i can do specific thing inside the game based on which blocks are placed!

That sort of thing should indeed work fine, except you've got an assignment (=) instead of equality comparison (===).  Try e.g.:

    workspace.addChangeListener((e) => {
      for (const block of workspace.getAllBlocks()) {
        if (block.type === 'text_print') {
          console.log('Found print block');
        }
      }
    });

If that's not working for you then perhaps you can be more specific about what information you want to get from the blocks and/or in what way it is not working.

Best wishes,

Christopher 

Breno Alves

unread,
2:27 PM (4 hours ago) 2:27 PM
to blo...@googlegroups.com
Thank you very much Christopher, this indeed works, seems like i had the answer all along but wasn't piecing things together very well 😂

--
You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/BjCIctji4CE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/CAN0w5eZde6%2BjByjAabFmiJ3Ng81u4tvjHZjVFaDG6_ECfDsO3A%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages