Typed variables issue

217 views
Skip to first unread message

Katrin Sattler

unread,
May 15, 2018, 5:58:19 AM5/15/18
to Blockly
I've tried to use typed variables as described in the guide (  https://developers.google.com/blockly/guides/create-custom-blocks/variables#typed_variable_blocks) . I've added a dynamic category, but as soon as I try to display a block that uses a typed variable in its JSON definition, my dynamic category gets broken and displays all blocks on top of each other:



As soon as I remove the line "variableTypes": ["gVar"] from my JSON definition, the menu is working perfectly and looks like this:



here is my JSON code:

    {
       "type": "grammarreference",
       "message0": "Use Grammar %1",
       "args0": [
         {
           "type": "field_variable",
           "name": "grammarName",
           "variable": "grammar1",
           "variableTypes": ["gVar"]
         }
       ],
       "previousStatement": [
          "grammar",
          "rule",
          "rulereference",
          "grammarreference"
        ],
       "nextStatement": [
        "rule",
        "rulereference",
        "grammarreference"
         ],
       "colour": 270,
       "tooltip": "",
       "helpUrl": ""
     },

I  am creating the variable using the following Code:

  Blockly.Variables.createVariable(a.getTargetWorkspace(), null, 'gVar');

Everything seems to be fine with the variable itself. It is added to allVariables and the type 'gVar' is assigned correctly.

I've already tried to download the latest blockly version and to use different browsers. The result is always the same. Can you tell me what I am doing wrong?

Rachel Fenichel

unread,
May 15, 2018, 8:25:33 PM5/15/18
to Blockly
What's your code for your dynamic category?

Katrin Sattler

unread,
May 17, 2018, 5:42:44 AM5/17/18
to Blockly
my dynamic category code:

Blockly.Parsers.Rules.FlyoutCat=function(a){
 //debugger;
 var b=[],
 c=goog.dom.createDom("button");
 c.setAttribute("text","Create new Rule");
 c.setAttribute("callbackKey","CREATE_VARIABLE_RULE");
 b.push(c);
 c=goog.dom.createDom("button");
 c.setAttribute("text","Create new Grammar");
 c.setAttribute("callbackKey","CREATE_VARIABLE_GRAMMAR");
 b.push(c);

 a.registerButtonCallback("CREATE_VARIABLE_RULE",Blockly.Parsers.Rules.onCreateVariableButtonClick_Rule);
 a.registerButtonCallback("CREATE_VARIABLE_GRAMMAR",Blockly.Parsers.Rules.onCreateVariableButtonClick_Grammar);

 a=Blockly.Parsers.Rules.FlyoutBlocks(a);
 return b=b.concat(a)
};




/**
 * Construct the blocks required by the flyout for the non terminal category.
 * @param {!Blockly.Workspace} workspace The workspace this flyout is for.
 * @return {!Array.<!Element>} Array of XML block elements.
 */
Blockly.Parsers.Rules.FlyoutBlocks = function(workspace) {
 debugger;
 
  var allVars=workspace.getAllVariables();
  var xmlList = [];
 
  var hasRule = false; // makes sure that only one rule Block will be shown
  var hasGrammar = false;
  var block;
  var blockType = "";
 
  if (0<allVars.length) {
       
      for(var curVar of allVars){
      
       var curType = curVar.type;
      
       if (curType == "rule" || curType == "gVar" ){
       
        // rule / grammar Block
        if ((curType == "rule" && !hasRule) || (curType == "gVar" && !hasGrammar)){
         if (curType == "gVar") {
          blockType = "grammar" ;
         } else {
          blockType = "rule";
         }
         var blockText = '<xml>' + 
         '<block type="' +blockType + '">' +
         '<field name="name">' + curVar.name + '</field>' +
         '</block>' +
         '</xml>';
         block = Blockly.Xml.textToDom(blockText).firstChild;
         xmlList.push(block);
        
         if(curType == "rule") hasRule = true;
         if(curType == "gVar") hasGrammar = true;
        }
       
       
        // reference Block
        blockText = '<xml>' +
        '<block type="' +curVar.type + 'reference">' +
        '<field name="' +curVar.type + 'Name">' + curVar.name + '</field>' +
        '</block>' +
        '</xml>';
        block = Blockly.Xml.textToDom(blockText).firstChild;
        xmlList.push(block);
       }
      }
     
  }
  return xmlList;
};

Rachel Fenichel

unread,
May 17, 2018, 3:28:27 PM5/17/18
to Blockly
I can't reproduce the exact problem you're having, because I don't have all of the block definitions so I can't get the flyout to load all the way anyway.  I have some guesses about what the problem is.  If they're not right, we can keep debugging.

I think the problem is that the variable field now has a single allowed type ('gVar') but no default type.  The default type is '' if not specified.  '' is not in the array of allowed types, and so there's an error.

If this is the problem you should be able to see this error in the console: 
      Invalid default type '' in the definition of a FieldVariable

if so, the fix is to add this to your block definition JSON:

           "variableTypes": ["gVar"],
           
"defaultType": "gVar"


If that wasn't it, the next debug steps are:
- What errors are you seeing in the console?
- Send a minimal example (if you're on github I suggest using gists) that includes all block definitions, functions, etc. I should be able to use it in place of playground.html for debugging.

Cheers,
Rachel

Bennet Preimess

unread,
May 19, 2018, 7:29:08 AM5/19/18
to Blockly
Reply all
Reply to author
Forward
0 new messages