Untitled

📰Getting Started | **Updates |** 📝 Guides | 🔢 API | ❓FAQ

Guides Overview

The Voice API provides access to registering callbacks when certain keywords are recognized.

const voice = require('voice');

Registration

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');
}

Handing dynamic data

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
};

Module Methods

register(command, callback, [playAudioFeedback])

Registers a voice command.

registerProtected(command, callback, [playAudioFeedback])

Registers a protected voice command. Protected voice commands must be spoken after the word "debug".

unregister(command)

Removes registration of a voice command.

Next: Assets Web API

Sidebar Table of Contents


Untitled

Copyright © 2021 Enklu, Inc.