@@ -107,8 +107,8 @@ void Engine::resolveIds()
107
107
const auto &blocks = target->blocks ();
108
108
for (auto block : blocks) {
109
109
auto container = blockSectionContainer (block->opcode ());
110
- block->setNext (getBlock (block->nextId ()));
111
- block->setParent (getBlock (block->parentId ()));
110
+ block->setNext (getBlock (block->nextId (), target. get () ));
111
+ block->setParent (getBlock (block->parentId (), target. get () ));
112
112
113
113
if (container) {
114
114
block->setCompileFunction (container->resolveBlockCompileFunc (block->opcode ()));
@@ -117,16 +117,16 @@ void Engine::resolveIds()
117
117
118
118
const auto &inputs = block->inputs ();
119
119
for (const auto &input : inputs) {
120
- input->setValueBlock (getBlock (input->valueBlockId ()));
120
+ input->setValueBlock (getBlock (input->valueBlockId (), target. get () ));
121
121
122
122
if (container)
123
123
input->setInputId (container->resolveInput (input->name ()));
124
124
125
125
InputValue *value = input->primaryValue ();
126
126
std::string id = value->valueId (); // no reference!
127
- value->setValuePtr (getEntity (id));
127
+ value->setValuePtr (getEntity (id, target. get () ));
128
128
assert (input->secondaryValue ()->type () != InputValue::Type::Variable && input->secondaryValue ()->type () != InputValue::Type::List); // secondary values never have a variable or list
129
- input->secondaryValue ()->setValuePtr (getEntity (input->secondaryValue ()->valueId ()));
129
+ input->secondaryValue ()->setValuePtr (getEntity (input->secondaryValue ()->valueId (), target. get () ));
130
130
131
131
// Add missing variables and lists
132
132
if (!value->valuePtr ()) {
@@ -149,7 +149,7 @@ void Engine::resolveIds()
149
149
const auto &fields = block->fields ();
150
150
for (auto field : fields) {
151
151
std::string id = field->valueId (); // no reference!
152
- field->setValuePtr (getEntity (id));
152
+ field->setValuePtr (getEntity (id, target. get () ));
153
153
154
154
if (container) {
155
155
field->setFieldId (container->resolveField (field->name ()));
@@ -179,7 +179,7 @@ void Engine::resolveIds()
179
179
block->updateInputMap ();
180
180
block->updateFieldMap ();
181
181
182
- auto comment = getComment (block->commentId ());
182
+ auto comment = getComment (block->commentId (), target. get () );
183
183
block->setComment (comment);
184
184
185
185
if (comment) {
@@ -209,7 +209,7 @@ void Engine::resolveIds()
209
209
assert (target);
210
210
211
211
for (auto field : fields) {
212
- field->setValuePtr (getEntity (field->valueId ()));
212
+ field->setValuePtr (getEntity (field->valueId (), target ));
213
213
214
214
if (container) {
215
215
field->setFieldId (container->resolveField (field->name ()));
@@ -1411,47 +1411,99 @@ const std::unordered_map<std::shared_ptr<Block>, std::shared_ptr<Script>> &Engin
1411
1411
}
1412
1412
1413
1413
// Returns the block with the given ID.
1414
- std::shared_ptr<Block> Engine::getBlock (const std::string &id)
1414
+ std::shared_ptr<Block> Engine::getBlock (const std::string &id, Target *target )
1415
1415
{
1416
1416
if (id.empty ())
1417
1417
return nullptr ;
1418
1418
1419
- for (auto target : m_targets) {
1420
- int index = target->findBlock (id);
1419
+ int index;
1420
+
1421
+ if (target) {
1422
+ index = target->findBlock (id);
1423
+
1421
1424
if (index != -1 )
1422
1425
return target->blockAt (index);
1423
1426
}
1424
1427
1428
+ for (auto t : m_targets) {
1429
+ index = t->findBlock (id);
1430
+
1431
+ if (index != -1 )
1432
+ return t->blockAt (index);
1433
+ }
1434
+
1425
1435
return nullptr ;
1426
1436
}
1427
1437
1428
1438
// Returns the variable with the given ID.
1429
- std::shared_ptr<Variable> Engine::getVariable (const std::string &id)
1439
+ std::shared_ptr<Variable> Engine::getVariable (const std::string &id, Target *target )
1430
1440
{
1431
1441
if (id.empty ())
1432
1442
return nullptr ;
1433
1443
1434
- for (auto target : m_targets) {
1435
- 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
+
1436
1457
if (index != -1 )
1437
1458
return target->variableAt (index);
1438
1459
}
1439
1460
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
+
1440
1471
return nullptr ;
1441
1472
}
1442
1473
1443
1474
// Returns the Scratch list with the given ID.
1444
- std::shared_ptr<List> Engine::getList (const std::string &id)
1475
+ std::shared_ptr<List> Engine::getList (const std::string &id, Target *target )
1445
1476
{
1446
1477
if (id.empty ())
1447
1478
return nullptr ;
1448
1479
1449
- for (auto target : m_targets) {
1450
- 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
+
1451
1493
if (index != -1 )
1452
1494
return target->listAt (index);
1453
1495
}
1454
1496
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
+
1455
1507
return nullptr ;
1456
1508
}
1457
1509
@@ -1469,35 +1521,40 @@ std::shared_ptr<Broadcast> Engine::getBroadcast(const std::string &id)
1469
1521
}
1470
1522
1471
1523
// Returns the comment with the given ID.
1472
- std::shared_ptr<Comment> Engine::getComment (const std::string &id)
1524
+ std::shared_ptr<Comment> Engine::getComment (const std::string &id, Target *target )
1473
1525
{
1474
1526
if (id.empty ())
1475
1527
return nullptr ;
1476
1528
1477
- for (auto target : m_targets) {
1478
- int index = target->findComment (id);
1529
+ int index;
1530
+
1531
+ if (target) {
1532
+ index = target->findComment (id);
1533
+
1479
1534
if (index != -1 )
1480
1535
return target->commentAt (index);
1481
1536
}
1482
1537
1538
+ for (auto t : m_targets) {
1539
+ index = t->findComment (id);
1540
+
1541
+ if (index != -1 )
1542
+ return t->commentAt (index);
1543
+ }
1544
+
1483
1545
return nullptr ;
1484
1546
}
1485
1547
1486
1548
// Returns the entity with the given ID. \see IEntity
1487
- std::shared_ptr<Entity> Engine::getEntity (const std::string &id)
1549
+ std::shared_ptr<Entity> Engine::getEntity (const std::string &id, Target *target )
1488
1550
{
1489
- // Blocks
1490
- auto block = getBlock (id);
1491
- if (block)
1492
- return std::static_pointer_cast<Entity>(block);
1493
-
1494
1551
// Variables
1495
- auto variable = getVariable (id);
1552
+ auto variable = getVariable (id, target );
1496
1553
if (variable)
1497
1554
return std::static_pointer_cast<Entity>(variable);
1498
1555
1499
1556
// Lists
1500
- auto list = getList (id);
1557
+ auto list = getList (id, target );
1501
1558
if (list)
1502
1559
return std::static_pointer_cast<Entity>(list);
1503
1560
@@ -1506,6 +1563,16 @@ std::shared_ptr<Entity> Engine::getEntity(const std::string &id)
1506
1563
if (broadcast)
1507
1564
return std::static_pointer_cast<Entity>(broadcast);
1508
1565
1566
+ // Blocks
1567
+ auto block = getBlock (id, target);
1568
+ if (block)
1569
+ return std::static_pointer_cast<Entity>(block);
1570
+
1571
+ // Comments
1572
+ auto comment = getComment (id, target);
1573
+ if (comment)
1574
+ return std::static_pointer_cast<Entity>(comment);
1575
+
1509
1576
return nullptr ;
1510
1577
}
1511
1578
0 commit comments