Script API
Contents
Overview
// TODO
| Properties | Type | Description |
|---|---|---|
| Script.context | string | |
| Script.objectName | string |
| Methods |
|---|
| Script.addEventHandler(EntityItemID,QString,QScriptValue) |
| Script.callAnimationStateHandler(QScriptValue,AnimVariantMap,QStringList,bool,AnimVariantResultHandler) |
| Script.callEntityScriptMethod(EntityItemID,QString) |
| Script.callEntityScriptMethod(EntityItemID,QString,EntityItemID,Collision) |
| Script.callEntityScriptMethod(EntityItemID,QString,PointerEvent) |
| Script.callEntityScriptMethod(EntityItemID,QString,QStringList) |
| Script.cleanupMenuItem(QString) |
| Script.clearInterval(QObject*) |
| Script.clearTimeout(QObject*) |
| Script.doneRunning() |
| Script.errorMessage(QString,QString) |
| Script.evaluate(QString,QString) |
| Script.evaluate(QString,QString,int) |
| Script.evaluateInClosure(QScriptValue,QScriptProgram) |
| Script.executeOnScriptThread(std::function<void()>) |
| Script.executeOnScriptThread(std::function<void()>,Qt::ConnectionType) |
| Script.finished(QString,ScriptEngine*) |
| Script.formatException(QScriptValue,bool) |
| Script.generateUUID() |
| Script.getContext() |
| Script.include(QString) |
| Script.include(QString,QScriptValue) |
| Script.include(QStringList) |
| Script.include(QStringList,QScriptValue) |
| Script.infoMessage(QString,QString) |
| Script.isAgentScript() |
| Script.isClientScript() |
| Script.isEntityClientScript() |
| Script.isEntityScriptRunning(EntityItemID) |
| Script.isEntityServerScript() |
| Script.lintScript(QString,QString) |
| Script.lintScript(QString,QString,int) |
| Script.load(QString) |
| Script.loadEntityScript(EntityItemID,QString,bool) |
| Script.loadScript(QString,bool) |
| Script.makeError() |
| Script.makeError(QScriptValue) |
| Script.makeError(QScriptValue,QString) |
| Script.print(QString) |
| Script.registerFunction(QString,QScriptEngine::FunctionSignature) |
| Script.registerFunction(QString,QScriptEngine::FunctionSignature,int) |
| Script.registerFunction(QString,QString,QScriptEngine::FunctionSignature) |
| Script.registerFunction(QString,QString,QScriptEngine::FunctionSignature,int) |
| Script.registerGetterSetter(QString,QScriptEngine::FunctionSignature,QScriptEngine::FunctionSignature) |
| Script.registerGetterSetter(QString,QScriptEngine::FunctionSignature,QScriptEngine::FunctionSignature,QString) |
| Script.registerGlobalObject(QString,QObject*) |
| Script.registerValue(QString,QScriptValue) |
| Script.reloadScript(QString,bool) |
| Script.removeEventHandler(EntityItemID,QString,QScriptValue) |
| Script.requestGarbageCollection() |
| Script.require(QString) |
| Script.resetModuleCache() |
| Script.resetModuleCache(bool) |
| Script.resolvePath(QString) |
| Script.resourcesPath() |
| Script.setInterval(QScriptValue,int) |
| Script.setTimeout(QScriptValue,int) |
| Script.signalHandlerException(QScriptValue) |
| Script.stop() |
| Script.stop(bool) |
| Script.unhandledException(QScriptValue) |
| Script.unloadAllEntityScripts() |
| Script.unloadEntityScript(EntityItemID) |
| Script.unloadEntityScript(EntityItemID,bool) |
| Script.update(float) |
| Script.updateMemoryCost(qlonglong) |
| Script.warningMessage(QString,QString) |
| Events |
|---|
| Script.entityScriptDetailsUpdated() |
| Script.entityScriptContentAvailable(EntityItemID,QString,QString,bool,bool,QString) |
| Script.errorLoadingScript(QString) |
| Script.objectNameChanged(QString) |
| Script.printedMessage(QString,QString) |
| Script.runningStateChanged() |
| Script.scriptEnding() |
| Script.scriptLoaded(QString) |
clearInterval()
setInterval() is used to run a specified callback function at a specified interval.
clearInterval() is used to stop calls scheduled using setInterval().
Function
clearInterval(valueReturnedBySetInterval) // Return value
Arguments
valueReturnedBySetInterval: valueReturnedBySetInterval: The value returned by setInterval()
Examples
In this example, setInterval() is used to print a line to the debug log once every 1000 milliseconds. The repeating call is stopped by using clearInterval().
<code>var multiple_timer = Script.setInterval(function() {
print("Repeating timer fired!");
}, 1000);
// Stops the repeating timer
Script.clearInterval(multiple_timer);</code>
You should not see any output from this example as the call is stopped.
clearTimeout()
setTimeout() is used to run a specified callback function once after a specified delay.
clearTimeout() is used to stop a call scheduled by setTimeout().
Function
clearTimeout(valueReturnedBySetTimeout)
Arguments
valueReturnedBySetTimeout: valueReturnedBySetTimeout: The value returned by setTimeout()
Examples
In this example setTimeout() is used to print a line to the debug log once after a delay of 1000 milliseconds. clearTimeout() is used to stop the scheduled call.
<code>var one_timer = Script.setTimeout(function() {
print("One time timer fired!");
}, 1000);
// this stops the scheduled function
Script.clearTimeout(one_timer);</code>
You should not see any output from this example as the scheduled call was stopped.
include()
include() is used to include javascript from another script (or scripts) in the current script regardless of whether the current script is an interface script, entity script, or assignment client script.
For more information about script types, see the Scripts Overview.
Function
include(StringOrArrayOfStrings, optionalCallback)
Arguments
scripts: String or Strings[]: A script or an array of scripts to include. Scripts can be specified as a full path or URL or can be relative to the current script.
optionalCallback: function: (Optional) A callback function to run after the script(s) have been loaded. If a callback function is specified, the specified script(s) will load asynchronously. If no callback function is specified, the specified script(s) will load synchronously (blocks).
Examples
This example contains two scripts both located in the same directory. The first script (ScriptA.js) prints a line to the debug log.
<code>// ScriptA.js
print ("This is script A.")</code>
The second script (ScriptB.js) uses include() to include ScriptA.js. include() contains a callback function to be run when complete.
<code>//ScriptB.js
Script.include('ScriptA.js', function() {
print("Script A included.");
});</code>
When you run ScriptB.js, you should see the following output:
<code>This is script A. Script A included.</code>
load()
load() is used to start a new interface script. As load() is used to start interface scripts, it is only meaningful in interface and entity scripts.
For more information about script types, see the Scripts Overview.
Function
load(name)
Arguments
name: String: The name, including the full path, of the script to be loaded. The path can be a full path or URL or it can be relative to the calling script.
Examples
This example uses two scripts in the same directory path. The first script (sample1.js) simply prints a line to the debug log.
<code>// sample1.js
print ("This is a sample script.");</code>
The second script (loadSample.js), loads and runs the sample1.js script.
<code>// loadSample.js
Script.load("sample1.js");</code>
You should see the following output.
<code>This is a sample script.</code>
resolvePath()
resolvePath() is used to resolve a path segment into an absolute path.
Function
resolvePath(pathSegment) // Returns a string
Arguments
pathSegment:string: The path segment to be resolved
Returns
absouletPath:string: The absolute path
scriptEnding()
scriptEnding() is used to run a callback function when the calling script is ending.
Function
scriptEnding(callback)
Arguments
callback:function: The callback function to run when the script is ending
Examples
This example waits for the script to end and prints a line to the debug log.
<code>Print("This script just prints this line and then ends.");
function scriptEnding() {
print("SCRIPT ENDING!!!");
}
// register our scriptEnding callback
Script.scriptEnding.connect(scriptEnding);</code>
You should see the following output:
<code>This script just prints this line and then ends. SCRIPT ENDING!!!</code>
setInterval()
setInterval() is used to run a specified callback function at a specified interval.
Function
setInterval(callback, milliseconds) // Returns a valueReturnedBySetInterval
Arguments
callback: function: The function to be run at each interval
milliseconds: number: The number of milliseconds between each interval
Returns
valueReturnedBySetInterval: valueReturnedBySetInterval: A value that can be used by clearInterval()
Examples
This example uses setInterval() to print a message to the debug log once every 1000 milliseconds.
<code>var multiple_timer = Script.setInterval(function() {
print("Repeating timer fired!");
}, 1000);</code>
You should see output similar to this, with a new line printed every 1000 milliseconds:
<code>Repeating timer fired! Repeating timer fired! Repeating timer fired!</code>
setTimeout()
setTimeout() is used to run a specified callback function once after a specified delay.
Function
setTimeout(callback, milliseconds) // Return valueReturnedBySetTimeout
Arguments
callback: function: The function to run after the specified delay
milliseconds:number: The number of milliseconds to wait before running the specified callback function
Returns
valueReturnedBySetTimeout: valueReturnedBySetTimeout: A value that can be used by clearTimeout()
Examples
In this example, setTimeout() is used to specify a callback function that prints a single line to the debug log after a delay of 10000 milliseconds.
<code>var one_timer = Script.setTimeout(function() {
print("One time timer fired!");
}, 10000);</code>
After a delay of 10000 milliseconds, you should see the following output:
<code>One time timer fired!</code>
stop()
stop() is used to stop the current script.
Function
stop()
Arguments
This function has no parameters.
Examples
You can stop the current script simply by calling stop().
<code>Script.stop();</code>
update()
update() is used to run a specified callback function at a system-determined interval (currently 60Hz but may change going forward).
Note: update() gives you the number of milliseconds since the last call as an argument. If you need that and are using setInterval(), you would have to track the timing yourself.
Function
update(float) // Return value
Arguments
name:class: description
Returns
name:class: description
Examples
Stand-alone (or close as possible) examples showcasing the function
<code>var count = 0;
function displayCounter(deltaTime) {
print("count =" + count + " deltaTime=" + deltaTime);
count++;
}
// register the call back so it fires before each data send
Script.update.connect(displayCounter);</code>