Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify in docs that variabletype needs to be set in Toolbox XML when using typed variables #2422

Closed
YannCaron opened this issue Apr 30, 2019 · 8 comments
Assignees
Labels
component: toolbox/flyout issue: docs Describes missing or incorrect documentation

Comments

@YannCaron
Copy link

YannCaron commented Apr 30, 2019

This problem lead me to craziness :-) I think it is a bug.

A want to create a custom type called 'Actor' to manage some object in my game engine.

image

But if I setup the type of the field_variable as well :

{
  "type": "field_variable",
  "name": "NAME",
  "variable": "hero",
  "validator": null,
  "variableTypes": ["Actor"],
  "defaultType": "Actor"
}

And I want to setup the value of this field within the tookbox definition (I also tried with a shadow block, same issue) :

<block type="actor_create" inline="true">
  <field name="NAME">hero</field>
</block>

NB: the block definition is into a dynamic category, so the 'hero' variable exists at the block definition time :-)

Even if 'hero' is an Actor object, it's lead to this error

Error: Variable type doesn't match this field! Type was 

I turn around from a long time with that and searched on google to find solutions, but nothing.
I know that defaultType is mandatory and everything seems OK, that's why I think it is a bug.

Here is the 'actor_create' definition :

{
  "type": "actor_create",
  "args0": [
    {
      "type": "field_variable",
      "name": "NAME",
      "variable": "hero",
      "validator": null,
      "variableTypes": [
        "Actor"
      ],
      "defaultType": "Actor"
    },
    {
      "type": "input_value",
      "name": "IMG",
      "check": "Image"
    },
    {
      "type": "input_value",
      "name": "X",
      "check": "Number"
    },
    {
      "type": "input_value",
      "name": "Y",
      "check": "Number"
    }
  ],
  "inputsInline": true,
  "colour": "%{BKY_ACTOR_HUE}",
  "message0": "%{BKY_ACTOR_CREATE_MSG}",
  "tooltip": "%{BKY_ACTOR_CREATE_HELP}",
  "helpUrl": "%{BKY_ACTOR_CREATE_URL}",
  "previousStatement": null,
  "nextStatement": null
}
@RoboErikG
Copy link
Contributor

How are you creating the 'hero' variable? Are you calling createVariable with the optional type set to Actor?
Blockly.Variables.createVariable(workspace, null, 'Actor');

@YannCaron
Copy link
Author

Hello @RoboErikG , No, I am using
Blockly.Variables.createVariableButtonHandler(button.getTargetWorkspace(), null, 'Actor'); with a button

@YannCaron
Copy link
Author

YannCaron commented May 1, 2019

Something is that 'hero' variable seems to be created 2 times :
image

PS: OK, I think I understand what's append now :
It appears it is due to that block definition in the workspace :

<block type="actor_create" inline="true">
  <field name="NAME">hero</field>
</block>

It seems that blockly try to create the variable 'hero' again when I use this declaration.

@RoboErikG
Copy link
Contributor

RoboErikG commented May 1, 2019

Ah, gotcha. You need to add a type attribute to the toolbox xml.

<field name="NAME" variabletype="Actor">hero</field>

We should probably update the documentation at the least to make this clearer. We could potentially infer the type when it's in the toolbox, but there are a lot of corner cases where that could cause problems.

@RoboErikG RoboErikG changed the title Shadow block for typed field_variable lead to an error Clarify in docs that variabletype needs to be set in Toolbox XML when using typed variables May 1, 2019
@RoboErikG RoboErikG added component: toolbox/flyout issue: docs Describes missing or incorrect documentation labels May 1, 2019
@YannCaron
Copy link
Author

Brilliant @RoboErikG It's works perfectly ! Many thanks.
I am just surprised that it is not camel case formatted like other attributes ("variableType") but anyway, the most important is that it is working and let me carry on my project :-)
Thanks again and keep up the good job 👍

@RoboErikG RoboErikG added this to the Bug Bash Backlog milestone May 2, 2019
@rachel-fenichel
Copy link
Collaborator

Oh yeah, the lack of camel casing is because attributes get forced to lowercase in setAttribute (either on XML or HTML DOM elements, I don't remember which). I agree that it's a mismatch with everything else we do.

@RoboErikG
Copy link
Contributor

Toolbox page will get a new section on variables next week.

@YannCaron
Copy link
Author

Great ! Many thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: toolbox/flyout issue: docs Describes missing or incorrect documentation
Projects
None yet
Development

No branches or pull requests

4 participants