@@ -212,10 +212,14 @@ void Engine::broadcast(unsigned int index, VirtualMachine *sourceScript, bool wa
212212 if (index < 0 || index >= m_broadcasts.size ())
213213 return ;
214214
215+ broadcastByPtr (m_broadcasts[index].get (), sourceScript, wait);
216+ }
217+
218+ void Engine::broadcastByPtr (Broadcast *broadcast, VirtualMachine *sourceScript, bool wait)
219+ {
215220 bool previousSkipFrame = m_skipFrame;
216221 skipFrame ();
217- const std::vector<Script *> &scripts = m_broadcastMap[index];
218- auto broadcast = m_broadcasts[index];
222+ const std::vector<Script *> &scripts = m_broadcastMap[broadcast];
219223
220224 for (auto script : scripts) {
221225 std::vector<VirtualMachine *> runningBroadcastScripts;
@@ -234,7 +238,7 @@ void Engine::broadcast(unsigned int index, VirtualMachine *sourceScript, bool wa
234238 m_scriptsToRemove.erase (std::remove (m_scriptsToRemove.begin (), m_scriptsToRemove.end (), vm), m_scriptsToRemove.end ());
235239 assert (std::find (m_scriptsToRemove.begin (), m_scriptsToRemove.end (), vm) == m_scriptsToRemove.end ());
236240
237- auto &scripts = m_runningBroadcastMap[broadcast. get () ];
241+ auto &scripts = m_runningBroadcastMap[broadcast];
238242
239243 for (auto &pair : scripts) {
240244 if (pair.second ->script () == script)
@@ -269,20 +273,12 @@ void Engine::broadcast(unsigned int index, VirtualMachine *sourceScript, bool wa
269273 if (it == runningBroadcastScripts.end ()) {
270274 auto vm = script->start (target);
271275 m_runningScripts.push_back (vm);
272- m_runningBroadcastMap[broadcast. get () ].push_back ({ sourceScript, vm.get () });
276+ m_runningBroadcastMap[broadcast].push_back ({ sourceScript, vm.get () });
273277 }
274278 }
275279 }
276280}
277281
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-
286282void Engine::stopScript (VirtualMachine *vm)
287283{
288284 assert (vm);
@@ -595,21 +591,16 @@ int Engine::findBroadcastById(const std::string &broadcastId) const
595591
596592void Engine::addBroadcastScript (std::shared_ptr<Block> whenReceivedBlock, Broadcast *broadcast)
597593{
598- auto id = findBroadcast (broadcast->name ());
599-
600- if (id == -1 )
601- return ;
602-
603- if (m_broadcastMap.count (id) == 1 ) {
604- std::vector<Script *> &scripts = m_broadcastMap[id];
594+ if (m_broadcastMap.count (broadcast) == 1 ) {
595+ std::vector<Script *> &scripts = m_broadcastMap[broadcast];
605596 // TODO: Do not allow adding existing scripts
606597 scripts.push_back (m_scripts[whenReceivedBlock].get ());
607598 } else {
608- m_broadcastMap[id ] = { m_scripts[whenReceivedBlock].get () };
599+ m_broadcastMap[broadcast ] = { m_scripts[whenReceivedBlock].get () };
609600
610601 // Create a vector of running scripts for this broadcast
611602 // so we don't need to check if it's there
612- m_runningBroadcastMap[m_broadcasts[id]. get () ] = {};
603+ m_runningBroadcastMap[broadcast ] = {};
613604 }
614605}
615606
0 commit comments