Search code examples
javascriptspark-ar-studio

How do I monitor a pulse value in a Spark AR script?


As part of a Spark AR Studio project there is a script that is used in Patch.

This script has two inputs (one bool, one trigger) and two outputs (both triggers).

Monitoring the bool input works as expected but monitoring the trigger input has suddenly started throwing an error:

JavaScript error: TypeError: triggerIn.monitor is not a function. (In 'triggerIn.monitor()', 'triggerIn.monitor' is undefined)

Here is all of the code from the file:

const Diagnostics = require('Diagnostics');
var Patches = require('Patches');
var Reactive = require('Reactive');

var input1 = Patches.getBooleanValue('input1');
var triggerIn = Patches.getPulseValue('triggerIn');
var onHead = true;

triggerIn.monitor().subscribe( function(e) {
    if (input1) {
        if (onHead) {
            Diagnostics.log('Play-Trigger');
            Patches.setPulseValue("play", Reactive.once());
        } else {
            Diagnostics.log('Reset-Trigger');
            Patches.setPulseValue("reset", Reactive.once());
        }
        onHead = !onHead;
    }
});

input1.monitor().subscribe( function(e) {
  if (e.newValue == true && !onHead) {
    Diagnostics.log('Reset-1');
    Patches.setPulseValue("reset", Reactive.once());
    onHead = true;
  }
});

triggerIn is connected to the output of a Head Nod node while input1 is connected to the output of an Exactly Equals node. The outputs connect to the play and reset inputs of an Animation node.

Previously I had different guts in the monitor function and things were working but now even if I strip all the code out and put in a log message it still doesn't work.

Is it not possible to monitor trigger signals? Is Spark AR a bit like Xcode where you sometimes need to clean the build folder? If so, what is Spark AR's equivalent? Or, maybe I've screwed up the syntax somehow and am blind to it?

I can make the effect work with just Patch but the scripting creates a better user experience.


Solution

  • After bouncing arounds in the docs (they really need cross links like Apple's docs) I finally found the answer…

    For values obtained with Patches.getPulseValue() you leave out the monitor() call going straight to subscribe().

    triggerIn.subscribe( function(e) {
        …
    });