Skip to content

Commit 149f832

Browse files
committed
Properly lookup list when creating missing monitor
1 parent cf0fd3a commit 149f832

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/blocks/listblocks.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,21 @@ extern "C" void data_showlist(Target *target, List *list)
269269
* Since this case doesn't occur frequently,
270270
* we can look up the list by ID.
271271
*/
272-
auto index = target->findListById(list->id());
273-
monitor = target->engine()->createListMonitor(target->listAt(index), "data_listcontents", "LIST");
272+
const auto &targets = target->engine()->targets();
273+
274+
for (const auto &target : targets) {
275+
auto index = target->findListById(list->id());
276+
277+
if (index != -1) {
278+
monitor = target->engine()->createListMonitor(target->listAt(index), "data_listcontents", "LIST");
279+
break;
280+
}
281+
}
282+
283+
if (!monitor) {
284+
assert(false);
285+
return;
286+
}
274287
}
275288

276289
monitor->setVisible(true);

test/blocks/list_blocks_test.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,49 @@ TEST_F(ListBlocksTest, ShowList_Global_Nonexistent)
654654
ASSERT_TRUE(monitor2->visible());
655655
}
656656

657+
TEST_F(ListBlocksTest, ShowList_Global_Nonexistent_FromSprite)
658+
{
659+
auto stage = std::make_shared<Stage>();
660+
auto sprite = std::make_shared<Sprite>();
661+
662+
auto list1 = std::make_shared<List>("a", "list1");
663+
list1->append("item1");
664+
list1->append("item2");
665+
stage->addList(list1);
666+
auto list2 = std::make_shared<List>("b", "list2");
667+
list2->append("Hello");
668+
list2->append("world");
669+
stage->addList(list2);
670+
671+
m_engine->setTargets({ stage, sprite });
672+
673+
ScriptBuilder builder1(m_extension.get(), m_engine, sprite);
674+
builder1.addBlock("data_showlist");
675+
builder1.addEntityField("LIST", list1);
676+
677+
ScriptBuilder builder2(m_extension.get(), m_engine, sprite);
678+
builder2.addBlock("data_showlist");
679+
builder2.addEntityField("LIST", list2);
680+
681+
ScriptBuilder::buildMultiple({ &builder1, &builder2 });
682+
m_engine->run();
683+
684+
// Missing monitors should be created
685+
builder1.run();
686+
687+
Monitor *monitor1 = list1->monitor();
688+
ASSERT_TRUE(monitor1);
689+
ASSERT_TRUE(monitor1->visible());
690+
691+
builder2.run();
692+
693+
Monitor *monitor2 = list2->monitor();
694+
695+
ASSERT_TRUE(monitor2);
696+
ASSERT_TRUE(monitor1->visible());
697+
ASSERT_TRUE(monitor2->visible());
698+
}
699+
657700
TEST_F(ListBlocksTest, ShowList_Local_Existent)
658701
{
659702
auto stage = std::make_shared<Stage>();

0 commit comments

Comments
 (0)