Merge pull request #6326 from imgntn/entity_params

Add Ability to Pass Parameters When Calling Methods on Entities
This commit is contained in:
Eric Levin 2015-11-09 12:17:58 -08:00
commit 7700e67734
7 changed files with 99 additions and 9 deletions

View file

@ -0,0 +1,42 @@
//
// createParamsEntity.js
//
// Created by James B. Pollack @imgntn on 11/6/2015
// Copyright 2015 High Fidelity, Inc.
//
// This script demonstrates creating an entity and sending it a method call with parameters.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
var PARAMS_SCRIPT_URL = Script.resolvePath('paramsEntity.js');
var testEntity = Entities.addEntity({
name: 'paramsTestEntity',
dimensions: {
x: 1,
y: 1,
z: 1
},
type: 'Box',
position: {
x: 0,
y: 0,
z: 0
},
visible: false,
script: PARAMS_SCRIPT_URL
});
var subData1 = ['apple', 'banana', 'orange'];
var subData2 = {
thing: 1,
otherThing: 2
};
var data = [subData1, JSON.stringify(subData2), 'third'];
Script.setTimeout(function() {
print('sending data to entity')
Entities.callEntityMethod(testEntity, 'testParams', data);
}, 1500)

View file

@ -0,0 +1,45 @@
//
// paramsEntity.js
//
// Script Type: Entity
//
// Created by James B. Pollack @imgntn on 11/6/2015
// Copyright 2015 High Fidelity, Inc.
//
// This script demonstrates how to recieve parameters from a Entities.callEntityMethod call
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
(function() {
function ParamsEntity() {
return;
}
ParamsEntity.prototype = {
preload: function(entityID) {
print('entity loaded')
this.entityID = entityID;
},
testParams: function(myID, paramsArray) {
paramsArray.forEach(function(param) {
var p;
try {
p = JSON.parse(param);
print("it's a json param")
print('json param property:' + p.thing);
} catch (err) {
print('not a json param')
p = param;
print('param is:' + p);
}
});
}
}
return new ParamsEntity();
});

View file

@ -19,7 +19,7 @@
class EntitiesScriptEngineProvider { class EntitiesScriptEngineProvider {
public: public:
virtual void callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName) = 0; virtual void callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName, const QStringList& params = QStringList()) = 0;
}; };
#endif // hifi_EntitiesScriptEngineProvider_h #endif // hifi_EntitiesScriptEngineProvider_h

View file

@ -217,14 +217,13 @@ void EntityScriptingInterface::deleteEntity(QUuid id) {
} }
} }
void EntityScriptingInterface::callEntityMethod(QUuid id, const QString& method) { void EntityScriptingInterface::callEntityMethod(QUuid id, const QString& method, const QStringList& params) {
if (_entitiesScriptEngine) { if (_entitiesScriptEngine) {
EntityItemID entityID{ id }; EntityItemID entityID{ id };
_entitiesScriptEngine->callEntityScriptMethod(entityID, method); _entitiesScriptEngine->callEntityScriptMethod(entityID, method, params);
} }
} }
QUuid EntityScriptingInterface::findClosestEntity(const glm::vec3& center, float radius) const { QUuid EntityScriptingInterface::findClosestEntity(const glm::vec3& center, float radius) const {
EntityItemID result; EntityItemID result;
if (_entityTree) { if (_entityTree) {

View file

@ -15,6 +15,7 @@
#define hifi_EntityScriptingInterface_h #define hifi_EntityScriptingInterface_h
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtCore/QStringList>
#include <DependencyManager.h> #include <DependencyManager.h>
#include <Octree.h> #include <Octree.h>
@ -93,7 +94,7 @@ public slots:
/// Allows a script to call a method on an entity's script. The method will execute in the entity script /// Allows a script to call a method on an entity's script. The method will execute in the entity script
/// engine. If the entity does not have an entity script or the method does not exist, this call will have /// engine. If the entity does not have an entity script or the method does not exist, this call will have
/// no effect. /// no effect.
Q_INVOKABLE void callEntityMethod(QUuid entityID, const QString& method); Q_INVOKABLE void callEntityMethod(QUuid entityID, const QString& method, const QStringList& params = QStringList());
/// finds the closest model to the center point, within the radius /// finds the closest model to the center point, within the radius
/// will return a EntityItemID.isKnownID = false if no models are in the radius /// will return a EntityItemID.isKnownID = false if no models are in the radius

View file

@ -18,6 +18,7 @@
#include <QtNetwork/QNetworkReply> #include <QtNetwork/QNetworkReply>
#include <QtScript/QScriptEngine> #include <QtScript/QScriptEngine>
#include <QtScript/QScriptValue> #include <QtScript/QScriptValue>
#include <QtCore/QStringList>
#include <AudioConstants.h> #include <AudioConstants.h>
#include <AudioEffectOptions.h> #include <AudioEffectOptions.h>
@ -1170,8 +1171,7 @@ void ScriptEngine::refreshFileScript(const EntityItemID& entityID) {
recurseGuard = false; recurseGuard = false;
} }
void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName, const QStringList& params) {
void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName) {
if (QThread::currentThread() != thread()) { if (QThread::currentThread() != thread()) {
#ifdef THREAD_DEBUGGING #ifdef THREAD_DEBUGGING
qDebug() << "*** WARNING *** ScriptEngine::callEntityScriptMethod() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " qDebug() << "*** WARNING *** ScriptEngine::callEntityScriptMethod() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "
@ -1180,7 +1180,8 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS
QMetaObject::invokeMethod(this, "callEntityScriptMethod", QMetaObject::invokeMethod(this, "callEntityScriptMethod",
Q_ARG(const EntityItemID&, entityID), Q_ARG(const EntityItemID&, entityID),
Q_ARG(const QString&, methodName)); Q_ARG(const QString&, methodName),
Q_ARG(const QStringList&, params));
return; return;
} }
#ifdef THREAD_DEBUGGING #ifdef THREAD_DEBUGGING
@ -1195,6 +1196,7 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS
if (entityScript.property(methodName).isFunction()) { if (entityScript.property(methodName).isFunction()) {
QScriptValueList args; QScriptValueList args;
args << entityID.toScriptValue(this); args << entityID.toScriptValue(this);
args << qScriptValueFromSequence(this, params);
entityScript.property(methodName).call(entityScript, args); entityScript.property(methodName).call(entityScript, args);
} }

View file

@ -19,6 +19,7 @@
#include <QtCore/QSet> #include <QtCore/QSet>
#include <QtCore/QWaitCondition> #include <QtCore/QWaitCondition>
#include <QtScript/QScriptEngine> #include <QtScript/QScriptEngine>
#include <QtCore/QStringList>
#include <AnimationCache.h> #include <AnimationCache.h>
#include <AnimVariant.h> #include <AnimVariant.h>
@ -115,7 +116,7 @@ public:
Q_INVOKABLE void loadEntityScript(const EntityItemID& entityID, const QString& entityScript, bool forceRedownload = false); // will call the preload method once loaded Q_INVOKABLE void loadEntityScript(const EntityItemID& entityID, const QString& entityScript, bool forceRedownload = false); // will call the preload method once loaded
Q_INVOKABLE void unloadEntityScript(const EntityItemID& entityID); // will call unload method Q_INVOKABLE void unloadEntityScript(const EntityItemID& entityID); // will call unload method
Q_INVOKABLE void unloadAllEntityScripts(); Q_INVOKABLE void unloadAllEntityScripts();
Q_INVOKABLE void callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName); Q_INVOKABLE void callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName, const QStringList& params = QStringList());
Q_INVOKABLE void callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName, const MouseEvent& event); Q_INVOKABLE void callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName, const MouseEvent& event);
Q_INVOKABLE void callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName, const EntityItemID& otherID, const Collision& collision); Q_INVOKABLE void callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName, const EntityItemID& otherID, const Collision& collision);