1313#include < scratchcpp/block.h>
1414#include < scratchcpp/variable.h>
1515#include < scratchcpp/list.h>
16+ #include < scratchcpp/costume.h>
1617#include < cassert>
1718#include < iostream>
1819#include < thread>
@@ -208,9 +209,13 @@ void Engine::startScript(std::shared_ptr<Block> topLevelBlock, std::shared_ptr<T
208209
209210void Engine::broadcast (unsigned int index, VirtualMachine *sourceScript, bool wait)
210211{
212+ if (index < 0 || index >= m_broadcasts.size ())
213+ return ;
214+
211215 bool previousSkipFrame = m_skipFrame;
212216 skipFrame ();
213217 const std::vector<Script *> &scripts = m_broadcastMap[index];
218+ auto broadcast = m_broadcasts[index];
214219
215220 for (auto script : scripts) {
216221 std::vector<VirtualMachine *> runningBroadcastScripts;
@@ -229,7 +234,7 @@ void Engine::broadcast(unsigned int index, VirtualMachine *sourceScript, bool wa
229234 m_scriptsToRemove.erase (std::remove (m_scriptsToRemove.begin (), m_scriptsToRemove.end (), vm), m_scriptsToRemove.end ());
230235 assert (std::find (m_scriptsToRemove.begin (), m_scriptsToRemove.end (), vm) == m_scriptsToRemove.end ());
231236
232- auto &scripts = m_runningBroadcastMap[index ];
237+ auto &scripts = m_runningBroadcastMap[broadcast. get () ];
233238
234239 for (auto &pair : scripts) {
235240 if (pair.second ->script () == script)
@@ -264,12 +269,20 @@ void Engine::broadcast(unsigned int index, VirtualMachine *sourceScript, bool wa
264269 if (it == runningBroadcastScripts.end ()) {
265270 auto vm = script->start (target);
266271 m_runningScripts.push_back (vm);
267- m_runningBroadcastMap[index ].push_back ({ sourceScript, vm.get () });
272+ m_runningBroadcastMap[broadcast. get () ].push_back ({ sourceScript, vm.get () });
268273 }
269274 }
270275 }
271276}
272277
278+ void Engine::broadcastByPtr (Broadcast *broadcast, VirtualMachine *sourceScript, bool wait)
279+ {
280+ auto it = std::find_if (m_broadcasts.begin (), m_broadcasts.end (), [broadcast](std::shared_ptr<Broadcast> b) { return b.get () == broadcast; });
281+
282+ if (it != m_broadcasts.end ())
283+ this ->broadcast (it - m_broadcasts.begin (), sourceScript, wait);
284+ }
285+
273286void Engine::stopScript (VirtualMachine *vm)
274287{
275288 assert (vm);
@@ -411,7 +424,20 @@ void Engine::setStageHeight(unsigned int height)
411424
412425bool Engine::broadcastRunning (unsigned int index, VirtualMachine *sourceScript)
413426{
414- const auto &scripts = m_runningBroadcastMap[index];
427+ if (index < 0 || index >= m_broadcasts.size ())
428+ return false ;
429+
430+ return broadcastByPtrRunning (m_broadcasts[index].get (), sourceScript);
431+ }
432+
433+ bool Engine::broadcastByPtrRunning (Broadcast *broadcast, VirtualMachine *sourceScript)
434+ {
435+ auto it = m_runningBroadcastMap.find (broadcast);
436+
437+ if (it == m_runningBroadcastMap.cend ())
438+ return false ;
439+
440+ const auto &scripts = it->second ;
415441
416442 for (const auto &pair : scripts) {
417443 if (pair.first == sourceScript)
@@ -570,6 +596,10 @@ int Engine::findBroadcastById(const std::string &broadcastId) const
570596void Engine::addBroadcastScript (std::shared_ptr<Block> whenReceivedBlock, std::shared_ptr<Broadcast> broadcast)
571597{
572598 auto id = findBroadcast (broadcast->name ());
599+
600+ if (id == -1 )
601+ return ;
602+
573603 if (m_broadcastMap.count (id) == 1 ) {
574604 std::vector<Script *> &scripts = m_broadcastMap[id];
575605 // TODO: Do not allow adding existing scripts
@@ -579,7 +609,7 @@ void Engine::addBroadcastScript(std::shared_ptr<Block> whenReceivedBlock, std::s
579609
580610 // Create a vector of running scripts for this broadcast
581611 // so we don't need to check if it's there
582- m_runningBroadcastMap[id ] = {};
612+ m_runningBroadcastMap[m_broadcasts[id]. get () ] = {};
583613 }
584614}
585615
0 commit comments