Skip to content

Commit c6954ec

Browse files
committed
Engine: Add support for matching field values by pointer
1 parent be6c341 commit c6954ec

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/engine/internal/engine.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ void Engine::broadcast(unsigned int index)
204204

205205
void Engine::broadcastByPtr(Broadcast *broadcast)
206206
{
207-
startHats(HatType::BroadcastReceived, { { HatField::BroadcastOption, broadcast->name() } }, nullptr);
207+
startHats(HatType::BroadcastReceived, { { HatField::BroadcastOption, broadcast } }, nullptr);
208208
}
209209

210210
void Engine::startBackdropScripts(Broadcast *broadcast)
@@ -1292,7 +1292,8 @@ void Engine::allScriptsByOpcodeDo(HatType hatType, F &&f, Target *optTarget)
12921292
delete targetsPtr;
12931293
}
12941294

1295-
std::vector<std::shared_ptr<VirtualMachine>> Engine::startHats(HatType hatType, const std::unordered_map<HatField, std::string> &optMatchFields, Target *optTarget)
1295+
std::vector<std::shared_ptr<VirtualMachine>>
1296+
Engine::startHats(HatType hatType, const std::unordered_map<HatField, std::variant<std::string, const char *, Entity *>> &optMatchFields, Target *optTarget)
12961297
{
12971298
// https://github.com/scratchfoundation/scratch-vm/blob/f1aa92fad79af17d9dd1c41eeeadca099339a9f1/src/engine/runtime.js#L1818-L1889
12981299
std::vector<std::shared_ptr<VirtualMachine>> newThreads;
@@ -1318,9 +1319,21 @@ std::vector<std::shared_ptr<VirtualMachine>> Engine::startHats(HatType hatType,
13181319
if (fieldIt != fieldMap.cend()) {
13191320
assert(topBlock->findFieldById(fieldIt->second));
13201321

1321-
if (topBlock->findFieldById(fieldIt->second)->value().toString() != fieldValue) {
1322-
// Field mismatch
1323-
return;
1322+
if (std::holds_alternative<std::string>(fieldValue)) {
1323+
if (topBlock->findFieldById(fieldIt->second)->value().toString() != std::get<std::string>(fieldValue)) {
1324+
// Field mismatch
1325+
return;
1326+
}
1327+
} else if (std::holds_alternative<const char *>(fieldValue)) {
1328+
if (topBlock->findFieldById(fieldIt->second)->value().toString() != std::string(std::get<const char *>(fieldValue))) {
1329+
// Field mismatch
1330+
return;
1331+
}
1332+
} else {
1333+
if (topBlock->findFieldById(fieldIt->second)->valuePtr().get() != std::get<Entity *>(fieldValue)) {
1334+
// Field mismatch
1335+
return;
1336+
}
13241337
}
13251338
}
13261339
}

src/engine/internal/engine.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <chrono>
1111
#include <mutex>
1212
#include <set>
13+
#include <variant>
1314

1415
#include "blocksectioncontainer.h"
1516

@@ -186,7 +187,8 @@ class Engine : public IEngine
186187
template<typename F>
187188
void allScriptsByOpcodeDo(HatType hatType, F &&f, Target *optTarget);
188189

189-
std::vector<std::shared_ptr<VirtualMachine>> startHats(HatType hatType, const std::unordered_map<HatField, std::string> &optMatchFields, Target *optTarget);
190+
std::vector<std::shared_ptr<VirtualMachine>>
191+
startHats(HatType hatType, const std::unordered_map<HatField, std::variant<std::string, const char *, Entity *>> &optMatchFields, Target *optTarget);
190192

191193
static const std::unordered_map<HatType, bool> m_hatRestartExistingThreads; // used to check whether a hat should restart existing threads
192194

0 commit comments

Comments
 (0)