overload a single function and cleanup, add examples

This commit is contained in:
James B. Pollack 2015-11-06 11:48:51 -08:00
parent 3cee569bae
commit e928c8278b
7 changed files with 92 additions and 44 deletions

View file

@ -0,0 +1,43 @@
//
// createParamsEntity.js
//
//
// Additions by James B. Pollack @imgntn on 11/6/2015
// Copyright 2015 High Fidelity, Inc.
//
// This script demonstrates creates an entity and sends 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
//
// Additions 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,8 +19,7 @@
class EntitiesScriptEngineProvider {
public:
virtual void callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName) = 0;
virtual void callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName, const QStringList& params) = 0;
virtual void callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName, const QStringList& params = QStringList()) = 0;
};
#endif // hifi_EntitiesScriptEngineProvider_h

View file

@ -217,13 +217,6 @@ void EntityScriptingInterface::deleteEntity(QUuid id) {
}
}
void EntityScriptingInterface::callEntityMethod(QUuid id, const QString& method) {
if (_entitiesScriptEngine) {
EntityItemID entityID{ id };
_entitiesScriptEngine->callEntityScriptMethod(entityID, method);
}
}
void EntityScriptingInterface::callEntityMethod(QUuid id, const QString& method, const QStringList& params) {
if (_entitiesScriptEngine) {
EntityItemID entityID{ id };

View file

@ -94,8 +94,8 @@ public slots:
/// 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
/// no effect.
Q_INVOKABLE void callEntityMethod(QUuid entityID, const QString& method);
Q_INVOKABLE void callEntityMethod(QUuid entityID, const QString& method, const QStringList& params);
Q_INVOKABLE void callEntityMethod(QUuid entityID, const QString& method, const QStringList& params = QStringList());
/// finds the closest model to the center point, within the radius
/// will return a EntityItemID.isKnownID = false if no models are in the radius
/// this function will not find any models in script engine contexts which don't have access to models

View file

@ -1170,37 +1170,6 @@ void ScriptEngine::refreshFileScript(const EntityItemID& entityID) {
recurseGuard = false;
}
void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName) {
if (QThread::currentThread() != thread()) {
#ifdef THREAD_DEBUGGING
qDebug() << "*** WARNING *** ScriptEngine::callEntityScriptMethod() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] "
"entityID:" << entityID << "methodName:" << methodName;
#endif
QMetaObject::invokeMethod(this, "callEntityScriptMethod",
Q_ARG(const EntityItemID&, entityID),
Q_ARG(const QString&, methodName));
return;
}
#ifdef THREAD_DEBUGGING
qDebug() << "ScriptEngine::callEntityScriptMethod() called on correct thread [" << thread() << "] "
"entityID:" << entityID << "methodName:" << methodName;
#endif
refreshFileScript(entityID);
if (_entityScripts.contains(entityID)) {
EntityScriptDetails details = _entityScripts[entityID];
QScriptValue entityScript = details.scriptObject; // previously loaded
if (entityScript.property(methodName).isFunction()) {
QScriptValueList args;
args << entityID.toScriptValue(this);
entityScript.property(methodName).call(entityScript, args);
}
}
}
void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName, const QStringList& params) {
if (QThread::currentThread() != thread()) {
#ifdef THREAD_DEBUGGING

View file

@ -116,8 +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 unloadEntityScript(const EntityItemID& entityID); // will call unload method
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);
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 EntityItemID& otherID, const Collision& collision);