Skip to content

Commit efc8691

Browse files
committed
ControlBlocks: Add wait until block
1 parent f02942e commit efc8691

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/blocks/controlblocks.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ControlBlocks::ControlBlocks()
1717
addCompileFunction("control_if_else", &compileIfElseStatement);
1818
addCompileFunction("control_stop", &compileStop);
1919
addCompileFunction("control_wait", &compileWait);
20+
addCompileFunction("control_wait_until", &compileWaitUntil);
2021

2122
// Inputs
2223
addInput("SUBSTACK", SUBSTACK);
@@ -132,6 +133,13 @@ void ControlBlocks::compileWait(Compiler *compiler)
132133
compiler->addFunctionCall(&wait);
133134
}
134135

136+
void ControlBlocks::compileWaitUntil(Compiler *compiler)
137+
{
138+
compiler->addInstruction(vm::OP_CHECKPOINT);
139+
compiler->addInput(CONDITION);
140+
compiler->addFunctionCall(&waitUntil);
141+
}
142+
135143
unsigned int ControlBlocks::stopAll(VirtualMachine *vm)
136144
{
137145
vm->engine()->stop();
@@ -164,4 +172,13 @@ unsigned int ControlBlocks::wait(VirtualMachine *vm)
164172
return 0;
165173
}
166174

175+
unsigned int ControlBlocks::waitUntil(VirtualMachine *vm)
176+
{
177+
if (!vm->getInput(0, 1)->toBool()) {
178+
vm->moveToLastCheckpoint();
179+
vm->stop(true, true, false);
180+
}
181+
return 1;
182+
}
183+
167184
} // namespace libscratchcpp

src/blocks/controlblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ class LIBSCRATCHCPP_EXPORT ControlBlocks : public IBlockSection
4444
static void compileIfElseStatement(Compiler *compiler);
4545
static void compileStop(Compiler *compiler);
4646
static void compileWait(Compiler *compiler);
47+
static void compileWaitUntil(Compiler *compiler);
4748

4849
private:
4950
static unsigned int stopAll(VirtualMachine *vm);
5051
static unsigned int stopOtherScriptsInSprite(VirtualMachine *vm);
5152
static unsigned int startWait(VirtualMachine *vm);
5253
static unsigned int wait(VirtualMachine *vm);
54+
static unsigned int waitUntil(VirtualMachine *vm);
5355

5456
static inline std::unordered_map<VirtualMachine *, std::pair<std::chrono::steady_clock::time_point, int>> m_timeMap;
5557
};

0 commit comments

Comments
 (0)