Creating default variables

91 views
Skip to first unread message

Matteo Mannai

unread,
Nov 28, 2024, 6:08:16 AM11/28/24
to Blockly
Hi everyone, i'm new to Blockly and i'm trying to learn it step by step. 
I was trying  to pre-create variable blocks inside the toolbox with  values that are stored in an array.
This is what i'm doing:

variableNames.forEach((name) => {
      if (!workspace.getVariable(name)) {
        workspace.createVariable(name);
      }
    });

The problem is that the variables blocks are created, but each one has "i" value, and not the values that are stored inside my array. 
How can i get rid of the "i" variable and show only my values? 
Thank you!

a.png

Christopher Allen

unread,
Nov 28, 2024, 7:25:53 AM11/28/24
to blo...@googlegroups.com
Hello Matteo,

I was trying  to pre-create variable blocks inside the toolbox with  values that are stored in an array.
This is what i'm doing:

variableNames.forEach((name) => {
      if (!workspace.getVariable(name)) {
        workspace.createVariable(name);
      }
    });

The problem is that the variables blocks are created, but each one has "i" value, and not the values that are stored inside my array. 
How can i get rid of the "i" variable and show only my values? 

I tried adding your code to the playground, and it worked as expected—I got several variables_get blocks with different variables selected in their respective dropdowns—so now i'm trying to work out what you are doing differently.  I'm especially puzzled about yoru Variables category getting populated with multiple variables_get blocks for the same variable.  A few questions that might help unravel this mystery:
  • Do you make any other calls to createVariable()?
  • Can you provide the toolbox definition you are using?  I don't need the whole thing, but I do want to see any parts of it that relate to variables—such as any instances of a variables_get block (even as a shadow attached to a block in a different category—as well as in particular the definition you are using for the Variables category.
  • Are you deserialising any blocks into the workspace?

Best wishes,

Christopher
 

Matteo Mannai

unread,
Nov 29, 2024, 6:23:41 AM11/29/24
to Blockly
Hi and thanks for answering me!

No, this is the only call "createVariable()" that i'm doing in the entire code. 
I now realized the problem could be in the variable section toolbox! This is how i declared it before:

{
        kind: "category",
        name: "Variables",
        colour: "#A55B80",
        contents: columnNames.map((name) => ({
          kind: "block",
          type: "variables_get",
          fields: {
            VAR: name,
          },
        })),
      },

and this is how it is now
{
        kind: "category",
        name: "Variables",
        colour: "#A55B80",
        contents: [
          {
            kind: "block",
            type: "variables_get",
          },
        ],
      },

I still get an "item" variable that i don't know where it comes from but this is definitely an improvment! Any idea about the "item" variable? Thank you so much!
Reply all
Reply to author
Forward
0 new messages