Skip to content

Commit

Permalink
CATROID-1462 Change variable by "" brick does not work
Browse files Browse the repository at this point in the history
 Loop bricks now actually change their control variable instead of continually
 setting it with calculated values. This allows for other change variable operations
 inside the loop to have an effect.
 This however furthers the possibility to create infinite loops and the resulting
 value of the control variable after the loop is not as easily predictable when using
 another change value operation inside the loop.
  • Loading branch information
Robert committed Feb 28, 2023
1 parent 9888bb9 commit 5bee466
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class ForVariableFromToAction extends LoopAction {
private boolean isRepeatActionInitialized = false;
private int fromValue;
private int toValue;
private int executedCount = 0;
private int step = 1;

@Override
Expand All @@ -55,16 +54,15 @@ public boolean delegate(float delta) {
isCurrentLoopInitialized = true;
}

setControlVariable(fromValue + step * executedCount);
setCurrentTime(getCurrentTime() + delta);

if (action != null && action.act(delta) && !isLoopDelayNeeded()) {
executedCount++;

if (Math.abs(step * executedCount) > Math.abs(toValue - fromValue)) {
if (!(controlVariable.getValue() instanceof Double)
|| (step > 0 && (double) controlVariable.getValue() >= toValue)
|| (step < 0 && (double) controlVariable.getValue() <= toValue)) {
return true;
}

changeControlVariable(step);
isCurrentLoopInitialized = false;
action.restart();
}
Expand All @@ -75,7 +73,6 @@ public boolean delegate(float delta) {
public void restart() {
isCurrentLoopInitialized = false;
isRepeatActionInitialized = false;
executedCount = 0;
super.restart();
}

Expand All @@ -101,6 +98,7 @@ private boolean interpretParameters() {
Double toInterpretation = to == null ? Double.valueOf(0d) : to.interpretDouble(scope);
toValue = toInterpretation.intValue();
setStepValue();
setControlVariable(fromValue);
return true;
} catch (InterpretationException interpretationException) {
Log.d(getClass().getSimpleName(), "Formula interpretation for this specific Brick failed.", interpretationException);
Expand All @@ -115,4 +113,8 @@ private void setStepValue() {
private void setControlVariable(int value) {
controlVariable.setValue((double) value);
}

private void changeControlVariable(int value) {
controlVariable.setValue((double) controlVariable.getValue() + (double) value);
}
}

0 comments on commit 5bee466

Please sign in to comment.