Skip to content

Commit

Permalink
Allow using script values in While Loop (needs migration)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaltby committed Apr 11, 2024
1 parent 500ccf3 commit e49c5ca
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,7 @@
"EVENT_GROUP_ACTIONS" : "Actions",
"EVENT_GROUP_BOOLEAN": "Boolean",
"EVENT_GROUP_COUNTER": "Counter",
"EVENT_LOOP_WHILE_EXPRESSION": "Loop While Math Expression",

"// 10": "Menu -----------------------------------------------------",

Expand Down
26 changes: 26 additions & 0 deletions src/lib/compiler/scriptBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5558,6 +5558,32 @@ extern void __mute_mask_${symbol};
this._addNL();
};

whileScriptValue = (
value: ScriptValue,
truePath: ScriptEvent[] | ScriptBuilderPathFunction = []
) => {
const loopId = this.getNextLabel();
const endLabel = this.getNextLabel();

this._addComment(`While`);
this._label(loopId);

const [rpnOps, fetchOps] = precompileScriptValue(
optimiseScriptValue(value)
);
const localsLookup = this._performFetchOperations(fetchOps);
this._addComment(`-- Calculate value`);
const rpn = this._rpn();
this._performValueRPN(rpn, rpnOps, localsLookup);
rpn.stop();

this._ifConst(".EQ", ".ARG0", 0, endLabel, 1);
this._compilePath(truePath);
this._jump(loopId);
this._label(endLabel);
this._addNL();
};

ifVariableTrue = (
variable: string,
truePath: ScriptEvent[] | ScriptBuilderPathFunction = [],
Expand Down
44 changes: 35 additions & 9 deletions src/lib/events/eventLoopWhile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ const autoLabel = (fetchArg, args) => {

const fields = [
{
key: "expression",
label: l10n("FIELD_EXPRESSION"),
description: l10n("FIELD_EXPRESSION_DESC"),
type: "matharea",
rows: 5,
placeholder: "e.g. $health >= 0...",
defaultValue: "",
key: "condition",
label: l10n("FIELD_CONDITION"),
description: l10n("FIELD_CONDITION_DESC"),
type: "value",
defaultValue: {
type: "lt",
valueA: {
type: "variable",
value: "LAST_VARIABLE",
},
valueB: {
type: "number",
value: 10,
},
},
},
{
key: "true",
Expand All @@ -30,9 +38,9 @@ const fields = [
];

const compile = (input, helpers) => {
const { whileExpression } = helpers;
const { whileScriptValue } = helpers;
const truePath = input.true;
whileExpression(input.expression || "0", truePath);
whileScriptValue(input.condition, truePath);
};

module.exports = {
Expand All @@ -42,4 +50,22 @@ module.exports = {
groups,
fields,
compile,
presets: [
{
id: "EVENT_LOOP_WHILE_EXPRESSION",
name: l10n("EVENT_LOOP_WHILE_EXPRESSION"),
description: l10n("EVENT_LOOP_WHILE_DESC"),
groups: ["EVENT_GROUP_MATH", "EVENT_GROUP_CONTROL_FLOW"],
subGroups: {
EVENT_GROUP_MATH: "EVENT_GROUP_CONTROL_FLOW",
EVENT_GROUP_CONTROL_FLOW: "EVENT_GROUP_MATH",
},
values: {
condition: {
type: "expression",
value: "",
},
},
},
],
};

0 comments on commit e49c5ca

Please sign in to comment.