📰Getting Started | ⭐**Updates |** 📝 Guides | 🔢 API | ❓FAQ
Web Editor Basics
HoloLens
Mobile
Meta Quest
Chapter Series Documentation
Assets
Scripting
Enklu Embedded
API Reference
Release Notes
FAQ
Contact
The Voice API provides access to registering callbacks when certain keywords are recognized.
const voice = require('voice');
A keyword can be registered with register
or registerProtected
. Protected keywords require the word "debug" to be spoken before being recognized.
function enter() {
voice.register('explode',function() {
myExplosionAsset.visible = true;
});
voice.registerProtected('cleanup',function() {
myExplosionAsset.visible = false;
});
}
function exit() {
voice.unregister('explode');
voice.unregister('cleanup');
}
When working with large groups of data you may find entering info into a callback time consuming like so.
voice.register("Show item 0", function () {visibility("Show", 0);});
You can use closures if you need to handle dynamic data like in the use case of an array. This example below creates an array of voice commands and each has a unique input.
The voice commands would be “Hide item” + some number (0-3 by default) so “hide item 1” should trigger a debug message to appear, this can be expanded to hide an array of items, or reveal an array of items.
const voice = require('voice');
const messages = require('messages');
var hideCommands = [];
const DISPATCH_MSG = '{[Dispatch message string:string]}';
const MAX_COMMANDS = {[Max voice commands:int=3]}; //just setting a default
const DEBUG_LOGS = {[Use debug log:bool]};
/**
* Called when the script is initialized.
*/
//You can use objects to send out a lot of data at once
var visibilityData = {
itemNumber: 0,
itemName: "",
}
function enter() {
for(let j=0; j<=MAX_COMMANDS+1; j++) {
hideCommands[j] = "Hide item " + j;
let myJ = j; //let keyword helps with scoping issues
//Voice commands use callbacks which means they don't normally
//accept parameters, you can get around that with closures
voice.register(hideCommands[j], helpHide(myJ));
}
}
//Closures - each function has access to it's parent's scope
function helpHide(number){
return function() {
HideItem(number);
}
}
function HideItem(number){
if(DEBUG_LOGS){
log.info("hide item " + number);
}
//just an example of how to send out a variety of data
visibilityData.itemNumber = number;
visibilityData.itemName = "food";
//could just use self if you don't want to use an object
messages.dispatch(DISPATCH_MSG, visibilityData);
}
/**
* Called before the script is removed or rebuilt.
*/
function exit() {
//to prevent meomry leaks destroy when exiting
for(let i=0; i<hideCommands.length; i++){
if(hideCommands[i] !=null)
voice.unregister(hideCommands[i]);
}
}
module.exports = {
enter: enter,
exit: exit
};
register(command, callback, [playAudioFeedback])
command <string>
The voice command to register.callback <function>
The function that will be invoked when the command is recognized. The command is passed as a parameter.playAudioFeedback <bool>
[Optional] audio toggle.
false
to disable audio feedback.true
to enable audio feedback. If no bool is supplied, defaults to true
.Registers a voice command.
registerProtected(command, callback, [playAudioFeedback])
command <string>
The voice command to register.callback <function>
The function that will be invoked when the command is recognized. The command is passed as a parameter.playAudioFeedback <bool>
[Optional] audio toggle.
false
to disable audio feedback.true
to enable audio feedback. If no bool is supplied, defaults to true
.Registers a protected voice command. Protected voice commands must be spoken after the word "debug".
unregister(command)
command <string>
The command that should no longer be recognized.Removes registration of a voice command.
Next: Assets Web API
Sidebar Table of Contents
Copyright © 2021 Enklu, Inc.