renciso218 / blockly

Automatically exported from code.google.com/p/blockly
0 stars 0 forks source link

Renaming index variable in for-each and count loops can break nested loops #17

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
To reproduce:
1/ Create a count loop block. Note default index variable is 'x'.
2/ Create another count loop block or for-each loop block as the action within 
the first block. Note default index variable is 'x' for the new block.
3/ Rename the index variable for the inner loop using the rename variable 
context menu to 'y'.
4/ The index variable for the inner AND outer loops as been renamed to 'y'.

This makes creating nested loops difficult. The simplest solution may be to 
ensure that when adding a count or for-each loop block the default index 
variable is unique to start with.

At present the only way to create nested loops is to drag on one loop block at 
a time and rename the variable straight away before dragging on any other loop 
blocks. Not end of the world but easy to forget.

This was seen using the source corresponding to svn revision 218.

Thanks

Gary

Original issue reported on code.google.com by ratcliff...@gmail.com on 7 Jun 2012 at 9:46

GoogleCodeExporter commented 8 years ago
Looking at this further I can see that what uses should be doing is creating a 
new variable for the loop index from the context menu. However the few people 
I've asked to try using Blockly wih nested loops all tried rename rather than 
new. Possibly because they didn't realise the loop index was a variable.

For what its worth I've attached the source of a function I added to the core 
variables.js file. This generates a unique variable name starting from 'a' 
through to 'z' and then 'a1' etc.

This function is then called in the language/control.js file for the count and 
for-each loops:

this.addInput('with', 
Blockly.LOCAL_VARIABLE).setText(Blockly.Variables.generateUniqueName());

Feel free to use the attached code as you see fit.

Gary

Original comment by ratcliff...@gmail.com on 7 Jun 2012 at 1:00

Attachments:

GoogleCodeExporter commented 8 years ago
like this idea , patched in r405.

The variable will start from i to z, and skip "l" to avoid i/l confusion.

Original comment by gasolin on 2 Sep 2012 at 8:56