diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index 3c34a6f6c7..8113168655 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -31,9 +31,6 @@ EntityItemID EntityScriptingInterface::addEntity(const EntityItemProperties& pro EntityItemID id(NEW_ENTITY, creatorTokenID, false ); - // queue the packet - queueEntityMessage(PacketTypeEntityAddOrEdit, id, properties); - // If we have a local entity tree set, then also update it. if (_entityTree) { _entityTree->lockForWrite(); @@ -41,6 +38,9 @@ EntityItemID EntityScriptingInterface::addEntity(const EntityItemProperties& pro _entityTree->unlock(); } + // queue the packet + queueEntityMessage(PacketTypeEntityAddOrEdit, id, properties); + return id; } @@ -97,13 +97,10 @@ EntityItemID EntityScriptingInterface::editEntity(EntityItemID entityID, const E // if the entity is unknown, attempt to look it up if (!entityID.isKnownID) { actualID = EntityItemID::getIDfromCreatorTokenID(entityID.creatorTokenID); - } - - // if at this point, we know the id, send the update to the entity server - if (actualID.id != UNKNOWN_ENTITY_ID) { - entityID.id = actualID.id; - entityID.isKnownID = true; - queueEntityMessage(PacketTypeEntityAddOrEdit, entityID, properties); + if (actualID.id != UNKNOWN_ENTITY_ID) { + entityID.id = actualID.id; + entityID.isKnownID = true; + } } // If we have a local entity tree set, then also update it. We can do this even if we don't know @@ -113,6 +110,12 @@ EntityItemID EntityScriptingInterface::editEntity(EntityItemID entityID, const E _entityTree->updateEntity(entityID, properties); _entityTree->unlock(); } + + // if at this point, we know the id, send the update to the entity server + if (entityID.isKnownID) { + queueEntityMessage(PacketTypeEntityAddOrEdit, entityID, properties); + } + return entityID; } @@ -123,13 +126,10 @@ void EntityScriptingInterface::deleteEntity(EntityItemID entityID) { // if the entity is unknown, attempt to look it up if (!entityID.isKnownID) { actualID = EntityItemID::getIDfromCreatorTokenID(entityID.creatorTokenID); - } - - // if at this point, we know the id, send the update to the entity server - if (actualID.id != UNKNOWN_ENTITY_ID) { - entityID.id = actualID.id; - entityID.isKnownID = true; - getEntityPacketSender()->queueEraseEntityMessage(entityID); + if (actualID.id != UNKNOWN_ENTITY_ID) { + entityID.id = actualID.id; + entityID.isKnownID = true; + } } // If we have a local entity tree set, then also update it. @@ -138,6 +138,11 @@ void EntityScriptingInterface::deleteEntity(EntityItemID entityID) { _entityTree->deleteEntity(entityID); _entityTree->unlock(); } + + // if at this point, we know the id, send the update to the entity server + if (entityID.isKnownID) { + getEntityPacketSender()->queueEraseEntityMessage(entityID); + } } EntityItemID EntityScriptingInterface::findClosestEntity(const glm::vec3& center, float radius) const {