diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index 0eec5cf04b..7c16214a78 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -766,6 +766,36 @@ QVector EntityScriptingInterface::findEntitiesByType(const QString entity return result; } +QVector EntityScriptingInterface::findEntitiesByName(const QString entityName, const glm::vec3& center, float radius, bool caseSensitiveSearch) const { + + QVector result; + if (_entityTree) { + QVector entities; + _entityTree->withReadLock([&] { + _entityTree->findEntities(center, radius, entities); + }); + + if (caseSensitiveSearch) { + foreach(EntityItemPointer entity, entities) { + if (entity->getName() == entityName) { + result << entity->getEntityItemID(); + } + } + + } else { + QString entityNameLowerCase = entityName.toLower(); + + foreach(EntityItemPointer entity, entities) { + QString entityItemLowerCase = entity->getName().toLower(); + if (entityItemLowerCase == entityNameLowerCase) { + result << entity->getEntityItemID(); + } + } + } + } + return result; +} + RayToEntityIntersectionResult EntityScriptingInterface::findRayIntersection(const PickRay& ray, bool precisionPicking, const QScriptValue& entityIdsToInclude, const QScriptValue& entityIdsToDiscard, bool visibleOnly, bool collidableOnly) { QVector entitiesToInclude = qVectorEntityItemIDFromScriptValue(entityIdsToInclude); diff --git a/libraries/entities/src/EntityScriptingInterface.h b/libraries/entities/src/EntityScriptingInterface.h index 73422efefa..c9fa50932a 100644 --- a/libraries/entities/src/EntityScriptingInterface.h +++ b/libraries/entities/src/EntityScriptingInterface.h @@ -387,6 +387,21 @@ public slots: /// this function will not find any entities in script engine contexts which don't have access to entities Q_INVOKABLE QVector findEntitiesByType(const QString entityType, const glm::vec3& center, float radius) const; + /**jsdoc + * Find all entities of a particular name that intersect a sphere defined by a center point and radius. + * @function Entities.findEntitiesByName + * @param {Entities.EntityType} entityName - The name of the entity to search for. + * @param {Vec3} center - The point about which to search. + * @param {number} radius - The radius within which to search. + * @param {bool} caseSensitiveSearch - Choose whether to to return case sensitive results back. + * @returns {Uuid[]} An array of entity IDs of the specified type that intersect the search sphere. The array is empty if + * no entities could be found. + * @example Get back a list of entities + * var entityIDs = Entities.findEntitiesByName("Light-Target", MyAvatar.position, 10, false); + * print("Number of Entities with the name Light-Target " + entityIDs.length); + */ + Q_INVOKABLE QVector findEntitiesByName(const QString entityName, const glm::vec3& center, float radius, bool caseSensitiveSearch = false ) const; + /**jsdoc * Find the first entity intersected by a {@link PickRay}. Light and Zone entities are not * intersected unless they've been configured as pickable using {@link Entities.setLightsArePickable|setLightsArePickable}