@@ -76,9 +76,11 @@ void Engine::compile()
7676 if (block->topLevel ()) {
7777 auto section = blockSection (block->opcode ());
7878 if (section) {
79+ auto vm = std::make_shared<VirtualMachine>(target.get (), this );
80+ m_scripts[block] = vm;
81+
7982 compiler.compile (block);
8083
81- auto vm = std::make_shared<VirtualMachine>(target.get (), this );
8284 vm->setFunctions (m_functions);
8385 vm->setConstValues (compiler.constValues ());
8486 vm->setVariables (compiler.variablePtrs ());
@@ -88,7 +90,6 @@ void Engine::compile()
8890 auto b = block->inputAt (block->findInput (" custom_block" ))->valueBlock ();
8991 procedureBytecodeMap[b->mutationPrototype ()->procCode ()] = vm->bytecode ();
9092 }
91- m_scripts[block] = vm;
9293 } else
9394 std::cout << " warning: unsupported top level block: " << block->opcode () << std::endl;
9495 }
@@ -179,6 +180,24 @@ void Engine::startScript(std::shared_ptr<Block> topLevelBlock, std::shared_ptr<T
179180 }
180181}
181182
183+ /* ! Starts the script of the broadcast with the given index. */
184+ void libscratchcpp::Engine::broadcast (unsigned int index)
185+ {
186+ const std::vector<VirtualMachine *> scripts = m_broadcastMap[index];
187+ for (auto vm : scripts) {
188+ auto it = std::find (m_runningScripts.begin (), m_runningScripts.end (), vm);
189+ if (it != m_runningScripts.end ()) {
190+ // Reset the script if it's already running
191+ auto i = it - m_runningScripts.begin ();
192+ m_scriptPositions[i] = m_runningScripts[i]->bytecode ();
193+ } else {
194+ m_runningScripts.push_back (vm);
195+ m_scriptPositions.push_back (vm->bytecode ());
196+ scriptCount++;
197+ }
198+ }
199+ }
200+
182201/* ! Stops the given script. */
183202void Engine::stopScript (VirtualMachine *vm)
184203{
0 commit comments