Skip to content

Commit

Permalink
Merge pull request #318 from sensebox/fix/loop-var-init
Browse files Browse the repository at this point in the history
init var in loop if not previously defined
  • Loading branch information
mariopesch authored Jul 2, 2024
2 parents d00deff + 58e3703 commit a41e5c8
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/components/Blockly/generator/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ Blockly['Arduino']['controls_for'] = function (Block) {
Block.getFieldValue('VAR')
).name;


const allVars = Blockly.getMainWorkspace()
.getVariableMap()
.getAllVariables();
const myVar = allVars.filter((v) => v.name === loopIndexVariable)[0];
var initVariable = "";
if (Blockly.Arduino.variables_[loopIndexVariable + myVar.type] == undefined) {
initVariable = "int "; // alternatively set to 'myVar.type' but that could lead to issues if users choose a char or a boolean
}

const branch = Blockly['Arduino'].statementToCode(Block, 'DO');

Expand Down Expand Up @@ -71,6 +78,7 @@ Blockly['Arduino']['controls_for'] = function (Block) {

return (
'for (' +
initVariable +
loopIndexVariable +
' = ' +
startNumber +
Expand Down

0 comments on commit a41e5c8

Please sign in to comment.