Get value from blockly variable

914 views
Skip to first unread message

Julian

unread,
Feb 2, 2019, 9:35:39 AM2/2/19
to Blockly
Hey, 

I'm having a hard time getting the value from a blockly var.

Blockly.Blocks["variables_set_counter"] = {
init: function () {
this.appendValueInput("VALUE")
.setCheck(null)
.appendField("set")
.appendField(new Blockly.FieldVariable("zaehler"), "VAR")
.appendField("to");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(330);
this.setTooltip("");
this.setHelpUrl("");
}
};
Blockly.JavaScript["variables_set_counter"] = function (block) {
var variable_var = Blockly.JavaScript.variableDB_.getName(
block.getFieldValue("VAR"),
Blockly.Variables.NAME_TYPE
);
var value_value = Blockly.JavaScript.valueToCode(
block,
"VALUE",
Blockly.JavaScript.ORDER_ASSIGNMENT
);
var code = variable_var + " = " + value_value + ";\n" + "setVar('" + block.id + "');\n";
return code;
};



Note: the "setVar(...)" is a helper for me to react when the js-interpreter is getting a var.
When I try to get the variables in my interpreter-method like this:

let allVars = Blockly.getMainWorkspace().getAllVariables();


I get the Variable-Model from blockly but no value:

0: Blockly.VariableModel
id_: "r@2;;rqJsQ*EY_An$({`"
name: "zaehler"
type: ""
workspace: Blockly.WorkspaceSvg { id: "V3E8I5+Z2Qx*h~:]`PeV", options: By.Options, RTL: false, horizontalLayout: false, toolboxPosition: 2, … }
__proto__: Object
length: 1
__proto__: Array(0)

The Variable-Model looks like this above. I get the name and the id but i need the value as well.

Thanks for any help.














dsadsadsad

Beka Westberg

unread,
Feb 2, 2019, 12:00:56 PM2/2/19
to Blockly
Hello,

Would you mind clarifying a bit when/why you want to get the information about a variable? Is it for code generation? Is it for some sort of user-facing display? Some other reason? And where is the getAllVariables() function getting called?

I think we can probably find a way to grab this information, I just want to know a bit more about the situtation before I start recommending things : ). Once the problem is figured out I'm sure someone here can help you!

--Beka

Julian

unread,
Feb 2, 2019, 6:28:43 PM2/2/19
to Blockly
Thanks for your fast response.

I need the value of a variable while stepping over the blockly-generated code with js-interpreter. In the example beneath the interpreter is stepping over moveforward();
var counter = 1;
setVar(block_id); // this is the helper I added to the code to handle when variables have to be set

Bildschirmfoto 2019-02-03 um 00.16.52.png

when the interpreter is calling setVar(block_id) i get the block by id and get the variables by getAllVariables()... The problem is that the variable-models have no value field and i really don't know know where to get it.
Maybe also my way with this extra method in the blockly-generated code is no the best practice and you know other ways to handle variables and inputs in the blockly-generated code and js-interpreter?

Thanks for your help.

Beka Westberg

unread,
Feb 2, 2019, 6:51:09 PM2/2/19
to Blockly
Hi again,

So I'm still a little fuzzy on what you're trying to do. It seems like you're trying to pass the value of the variable out of the JS-Interpreter, so then the document can do something with it? If this is the case, I think one way to do it is to set up your generator like so:

Blockly.JavaScript["variables_set_counter"] = function (block) {
 
var variable_var = Blockly.JavaScript.variableDB_.getName(
      block
.getFieldValue("VAR"), Blockly.Variables.NAME_TYPE);
 
var value_value = Blockly.JavaScript.valueToCode(
      block
, "VALUE", Blockly.JavaScript.ORDER_ASSIGNMENT);

 
// The below line is changed to pass value_value instead of block.id.
 
var code = variable_var + " = " + value_value + ";\n" + "setVar('" + value_value + "');\n";
 
return code;
};


If this isn't what you're looking for please message back! Hope it helps,
Beka

Julian

unread,
Feb 3, 2019, 7:29:41 AM2/3/19
to Blockly
Thanks a lot :)

I think to pass the value into the parameter of my helper-function getVar is a good idea :) and will work.
To clear your confusion about what I'm doing here: I want to implement a blockly-maze task where pupils have to count their steps in a maze and whenever they set or get a variable i want to visualize the values of the used variables in different drawers. The blockly-docu is not very clear here how to handle variables with the interpreter so I decided to use a helperfunction in the blockly-generated code and i remove it when i show the code to the pupils. 
Thanks again for your help and if know any other good practice handling variables with js-interpreter I would be very interested. I also would be happy to share ideas about exercise-concepts in blockly if you are interested.

Best regards,

Julian

Beka Westberg

unread,
Feb 3, 2019, 9:53:37 AM2/3/19
to Blockly
Hi Julian,
 
whenever they set or get a variable i want to visualize the values of the used variables in different drawers

Wow that sounds like a really cool idea! I like the idea of being able to "inspect" the code while it's running, almost like a debug tool. I think seeing what the variables are doing in real time would really help with concepts like loops.

 I also would be happy to share ideas about exercise-concepts in blockly if you are interested.

I would love to hear! 

--Beka

Julian

unread,
Feb 15, 2019, 7:07:16 AM2/15/19
to Blockly
Hey guys, 

thanks for the help here. I found out a much better solution in the end which includes the usage of js_interpreter.
I still use the helper-function ("setVar()") which is generated from blockly-block-code like this:
var code =
variable_var +
" = " +
value_value +
";\n" +
"setVar('" +
block.id +
"=" +
value_value +
"') ;\n";
return code;

When i initialize the js_interpreter i just place the scope of js_interpreter in the function call like this:

intp.setProperty(
scope,
"setVar",
intp.createNativeFunction(function (b) {
return intp.createPrimitive(dataObj.setVar(b.toString(), scope));
})
);

My helper-function called from interpreter looks like this:

setVar: function(raw_id, scope) {
// set uservar to value:
var var_value = scope.properties.zaehler.data;
var split_index = raw_id.lastIndexOf("=");
var b_id = raw_id.slice(0, split_index);
var value_str = raw_id.slice(split_index + 1, raw_id.length);
console.log(value_str);
this.counter += parseInt(value_str);
this.visQueue.push(["showConsoleVis", b_id, var_value]);
}

As you can see the interpreter value of the current interpreted line is in my scope of the helper-function and everyone is happy.

Andrew n marshall

unread,
Feb 19, 2019, 10:18:38 PM2/19/19
to blo...@googlegroups.com
Glad you found the js_interpreter solution. I was just about to suggest it. In addition to allowing stepping and storing intermediate variable values, it is also highly recommended for security reasons.



--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages