@@ -1436,32 +1436,74 @@ std::shared_ptr<Block> Engine::getBlock(const std::string &id, Target *target)
14361436}
14371437
14381438// Returns the variable with the given ID.
1439- std::shared_ptr<Variable> Engine::getVariable (const std::string &id)
1439+ std::shared_ptr<Variable> Engine::getVariable (const std::string &id, Target *target )
14401440{
14411441 if (id.empty ())
14421442 return nullptr ;
14431443
1444- for (auto target : m_targets) {
1445- int index = target->findVariableById (id);
1444+ Stage *stage = this ->stage ();
1445+ int index;
1446+
1447+ // Check stage
1448+ index = stage->findVariableById (id);
1449+
1450+ if (index != -1 )
1451+ return stage->variableAt (index);
1452+
1453+ // Check currently compiled target
1454+ if (target != stage) {
1455+ index = target->findVariableById (id);
1456+
14461457 if (index != -1 )
14471458 return target->variableAt (index);
14481459 }
14491460
1461+ // Fall back to checking all the other targets
1462+ for (auto t : m_targets) {
1463+ if (t.get () != stage && t.get () != target) {
1464+ int index = t->findVariableById (id);
1465+
1466+ if (index != -1 )
1467+ return t->variableAt (index);
1468+ }
1469+ }
1470+
14501471 return nullptr ;
14511472}
14521473
14531474// Returns the Scratch list with the given ID.
1454- std::shared_ptr<List> Engine::getList (const std::string &id)
1475+ std::shared_ptr<List> Engine::getList (const std::string &id, Target *target )
14551476{
14561477 if (id.empty ())
14571478 return nullptr ;
14581479
1459- for (auto target : m_targets) {
1460- int index = target->findListById (id);
1480+ Stage *stage = this ->stage ();
1481+ int index;
1482+
1483+ // Check stage
1484+ index = stage->findListById (id);
1485+
1486+ if (index != -1 )
1487+ return stage->listAt (index);
1488+
1489+ // Check currently compiled target
1490+ if (target != stage) {
1491+ index = target->findListById (id);
1492+
14611493 if (index != -1 )
14621494 return target->listAt (index);
14631495 }
14641496
1497+ // Fall back to checking all the other targets
1498+ for (auto t : m_targets) {
1499+ if (t.get () != stage && t.get () != target) {
1500+ int index = t->findListById (id);
1501+
1502+ if (index != -1 )
1503+ return t->listAt (index);
1504+ }
1505+ }
1506+
14651507 return nullptr ;
14661508}
14671509
@@ -1507,12 +1549,12 @@ std::shared_ptr<Comment> Engine::getComment(const std::string &id, Target *targe
15071549std::shared_ptr<Entity> Engine::getEntity (const std::string &id, Target *target)
15081550{
15091551 // Variables
1510- auto variable = getVariable (id);
1552+ auto variable = getVariable (id, target );
15111553 if (variable)
15121554 return std::static_pointer_cast<Entity>(variable);
15131555
15141556 // Lists
1515- auto list = getList (id);
1557+ auto list = getList (id, target );
15161558 if (list)
15171559 return std::static_pointer_cast<Entity>(list);
15181560
0 commit comments