From 7f4e3f82165fb5afa122f110a0a3393ddfcd6588 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 22 Jun 2015 13:12:34 -0700 Subject: [PATCH] when deserializing action, remove any existing actions that weren't part of the serialized data --- libraries/entities/src/EntityItem.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 53d5e11339..5ac3665c75 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -1413,12 +1413,16 @@ void EntityItem::setActionData(QByteArray actionData) { QDataStream ds(actionData); ds >> serializedActions; + // Keep track of which actions got added or updated by the new actionData + QSet updated; + foreach(QByteArray serializedAction, serializedActions) { QDataStream dsForAction(serializedAction); EntityActionType actionType; QUuid actionID; dsForAction >> actionType; dsForAction >> actionID; + updated << actionID; if (_objectActions.contains(actionID)) { EntityActionPointer action = _objectActions[actionID]; @@ -1428,7 +1432,7 @@ void EntityItem::setActionData(QByteArray actionData) { auto actionFactory = DependencyManager::get(); EntityTree* entityTree = _element ? _element->getTree() : nullptr; - EntitySimulation* simulation = entityTree? entityTree->getSimulation() : nullptr; + EntitySimulation* simulation = entityTree ? entityTree->getSimulation() : nullptr; if (entityTree) { EntityItemPointer entity = entityTree->findEntityByEntityItemID(_id); @@ -1438,6 +1442,24 @@ void EntityItem::setActionData(QByteArray actionData) { } } } + + // remove any actions that weren't included in the new data. + QHash::iterator i = _objectActions.begin(); + while (i != _objectActions.end()) { + const QUuid id = i.key(); + if (updated.contains(id)) { + i++; + continue; + } + EntityActionPointer action = _objectActions[id]; + i = _objectActions.erase(i); + action->setOwnerEntity(nullptr); + EntityTree* entityTree = _element ? _element->getTree() : nullptr; + EntitySimulation* simulation = entityTree ? entityTree->getSimulation() : nullptr; + if (simulation) { + action->removeFromSimulation(simulation); + } + } }