Search code examples
variablesgithubjdeveloperblockly

Blockly How to create a Variable to the workspace (developer variable)


I want to create a Developer Variable to the workspace in Blockly, but I cannot find the necessary function/method.

I do not want to create the variable over a button. The variable should be included even if there is no block in the workspace.

With these two functions I can get the already created variables:

var variables = workspace.getAllVariables();
var dev_var = Blockly.Variables.allDeveloperVariables(workspace);

But what is the setting function?


Solution

  • Developer variables are variables that will never be visible to the user, but will exist in the generated code. If that's what you're looking for: there's no API for it, but here are some things you can do.

    • If you want to reserve the name so that users can't accidentally override your variable, call yourGenerator.addReservedWords('var1,var2,...'). You can initialize the variable in your wrapper code.
    • If you really want Blockly to both reserve and declare the variable for you, you could override the init function on your generator.

    On the other hand, if what you want is a user-visible variable that always shows up in the toolbox, without the user creating it, you should call yourWorkspace.createVariable('variable_name').