rapid-router
rapid-router copied to clipboard
Add while loops with conditions
What is the problem that you wish to solve? Please describe. We want to provide an alternative for the "for" loop and use "while" with a condition instead.
Describe the solution you'd like
We already have a "repeat while" block. We'll need to add other blocks to be able to build a while loop with a counter: a logic compare block, a variable, a number and an increment variable block. The first three were attempted on the new-blocks
branch. What's missing from there is that I manually added logic_compare
, variables_get
and math_number
to the game_block
table. From the investigations I made so far, it seems that the following are needed to make the new blocks work:
- Declare them in
blocklyCustomBlocks.js
- once in theinitCustomBlocksDescription
function for Blockly (e.g.Blockly.Blocks["variables_get"]
) and another time ininitCustomBlocksPython
for Python (e.g.Blockly.Python["variables_get"]
) - Add logic for the new blocks to
blocklyCompiler.js
- an example isocargo.BlocklyCompiler.prototype.logicCompareCondition
which is being used inocargo.BlocklyCompiler.prototype.getCondition
. This seems to work for the compare block, but the variable block logic is missing (we need a place to store the variables, maybe an object onwindow
will be sufficient?), this has to be completed inocargo.BlocklyCompiler.prototype.getValue
. - In
blocklyControl.js
, we probably need to add the new blocks toocargo.BlocklyControl.prototype.activeBlocksCount
(there's a TODO comment there) - We may need to customise the
math_number
block and add something likenumber:
in front of the input. Also, the increment block is missing from that branch/investigation - there is a default block calledmath_change
, though this might need to be customised to make it more obvious that it increments a variable. - When generating the Python code, we'll need to initialise the variable to 0 before the loop - this has not been done yet, but the place for this is probably in
blocklyCustomBlocks.js
- when generating the "while" loop, we can check if the condition contains a variable and initialise it to 0 before the loop.
Some links that helped during investigation:
- Blockly docs - https://developers.google.com/blockly/guides/overview
- Blockly default blocks - https://blockly-demo.appspot.com/static/demos/code/index.html - I used the XML tab on this to get the names of the blocks included by default in Blockly.
- Custom block builder - https://blockly-demo.appspot.com/static/demos/blockfactory/index.html - This helps generating the code for the custom blocks in Blockly and Python. Make sure "Block Definition" is set to JavaScript, then this code can be copied to
blocklyCustomBlocks.js
. The Python code underneath can also be copied, though it may need some changes.
This is the current state on the new-blocks
branch - the comparison works, but the variable is just 0 and doesn't increment.