@@ -1057,7 +1057,7 @@ void Engine::addGreenFlagScript(std::shared_ptr<Block> hatBlock)
10571057 addHatToMap (m_greenFlagHats, m_scripts[hatBlock].get ());
10581058}
10591059
1060- void Engine::addBroadcastScript (std::shared_ptr<Block> whenReceivedBlock, int fieldId , Broadcast *broadcast)
1060+ void Engine::addBroadcastScript (std::shared_ptr<Block> whenReceivedBlock, Field *field , Broadcast *broadcast)
10611061{
10621062 assert (!broadcast->isBackdropBroadcast ());
10631063 Script *script = m_scripts[whenReceivedBlock].get ();
@@ -1073,19 +1073,22 @@ void Engine::addBroadcastScript(std::shared_ptr<Block> whenReceivedBlock, int fi
10731073 m_broadcastMap[broadcast] = { script };
10741074
10751075 addHatToMap (m_broadcastHats, script);
1076- addHatField (script, HatField::BroadcastOption, fieldId );
1076+ addHatField (script, HatField::BroadcastOption, field );
10771077}
10781078
1079- void Engine::addBackdropChangeScript (std::shared_ptr<Block> hatBlock, int fieldId )
1079+ void Engine::addBackdropChangeScript (std::shared_ptr<Block> hatBlock, Field *field )
10801080{
10811081 Stage *stage = this ->stage ();
10821082
10831083 if (!stage)
10841084 return ;
10851085
1086- // TODO: This assumes the first field holds the broadcast pointer, maybe this is not the best way (e. g. if an extension uses this method)
1087- assert (hatBlock->fieldAt (0 ));
1088- const std::string &backdropName = hatBlock->fieldAt (0 )->value ().toString ();
1086+ if (!field) {
1087+ assert (false );
1088+ return ;
1089+ }
1090+
1091+ const std::string &backdropName = field->value ().toString ();
10891092 auto backdrop = stage->costumeAt (stage->findCostume (backdropName));
10901093
10911094 if (!backdrop)
@@ -1107,19 +1110,19 @@ void Engine::addBackdropChangeScript(std::shared_ptr<Block> hatBlock, int fieldI
11071110 m_backdropBroadcastMap[broadcast] = { script };
11081111
11091112 addHatToMap (m_backdropChangeHats, script);
1110- addHatField (script, HatField::Backdrop, fieldId );
1113+ addHatField (script, HatField::Backdrop, field );
11111114}
11121115
11131116void Engine::addCloneInitScript (std::shared_ptr<Block> hatBlock)
11141117{
11151118 addHatToMap (m_cloneInitHats, m_scripts[hatBlock].get ());
11161119}
11171120
1118- void Engine::addKeyPressScript (std::shared_ptr<Block> hatBlock, int fieldId )
1121+ void Engine::addKeyPressScript (std::shared_ptr<Block> hatBlock, Field *field )
11191122{
11201123 Script *script = m_scripts[hatBlock].get ();
11211124 addHatToMap (m_whenKeyPressedHats, script);
1122- addHatField (script, HatField::KeyOption, fieldId );
1125+ addHatField (script, HatField::KeyOption, field );
11231126}
11241127
11251128void Engine::addTargetClickScript (std::shared_ptr<Block> hatBlock)
@@ -1607,15 +1610,15 @@ void Engine::addHatToMap(std::unordered_map<Target *, std::vector<Script *>> &ma
16071610 map[target] = { script };
16081611}
16091612
1610- void Engine::addHatField (Script *script, HatField field, int fieldId )
1613+ void Engine::addHatField (Script *script, HatField hatField, Field *targetField )
16111614{
16121615 auto it = m_scriptHatFields.find (script);
16131616
16141617 if (it == m_scriptHatFields.cend ())
1615- m_scriptHatFields[script] = { { field, fieldId } };
1618+ m_scriptHatFields[script] = { { hatField, targetField } };
16161619 else {
16171620 auto &fieldMap = it->second ;
1618- fieldMap[field ] = fieldId ;
1621+ fieldMap[hatField ] = targetField ;
16191622 }
16201623}
16211624
@@ -2045,8 +2048,6 @@ std::vector<std::shared_ptr<Thread>> Engine::startHats(HatType hatType, const st
20452048 allScriptsByOpcodeDo (
20462049 hatType,
20472050 [this , hatType, &optMatchFields, &newThreads](Script *script, Target *target) {
2048- auto topBlock = script->topBlock ();
2049-
20502051 if (!optMatchFields.empty ()) {
20512052 // Get the field map for this script
20522053 auto fieldMapIt = m_scriptHatFields.find (script);
@@ -2061,20 +2062,20 @@ std::vector<std::shared_ptr<Thread>> Engine::startHats(HatType hatType, const st
20612062 assert (fieldIt != fieldMap.cend ());
20622063
20632064 if (fieldIt != fieldMap.cend ()) {
2064- assert (topBlock-> findFieldById ( fieldIt->second ) );
2065+ assert (fieldIt->second );
20652066
20662067 if (std::holds_alternative<std::string>(fieldValue)) {
2067- if (topBlock-> findFieldById ( fieldIt->second ) ->value ().toString () != std::get<std::string>(fieldValue)) {
2068+ if (fieldIt->second ->value ().toString () != std::get<std::string>(fieldValue)) {
20682069 // Field mismatch
20692070 return ;
20702071 }
20712072 } else if (std::holds_alternative<const char *>(fieldValue)) {
2072- if (topBlock-> findFieldById ( fieldIt->second ) ->value ().toString () != std::string (std::get<const char *>(fieldValue))) {
2073+ if (fieldIt->second ->value ().toString () != std::string (std::get<const char *>(fieldValue))) {
20732074 // Field mismatch
20742075 return ;
20752076 }
20762077 } else {
2077- if (topBlock-> findFieldById ( fieldIt->second ) ->valuePtr ().get () != std::get<Entity *>(fieldValue)) {
2078+ if (fieldIt->second ->valuePtr ().get () != std::get<Entity *>(fieldValue)) {
20782079 // Field mismatch
20792080 return ;
20802081 }
@@ -2103,7 +2104,7 @@ std::vector<std::shared_ptr<Thread>> Engine::startHats(HatType hatType, const st
21032104 }
21042105
21052106 // Start the thread with this top block
2106- newThreads.push_back (pushThread (topBlock, target));
2107+ newThreads.push_back (pushThread (script-> topBlock () , target));
21072108 },
21082109 optTarget);
21092110
0 commit comments