String Array from Script

Hi, I’m trying to pass a reactive number from patch editor as a driver to call a string from an array. That string is for a 2dtext text input.

So in patch editor I can plug in the number, and then the script will throw out the text accordingly, and it is also going to be updated if the input number changes without having to restart the effect.

This is my current script lines:

const P = require('Patches');
const S = require('Scene');
const R = require('Reactive');
const D = require('Diagnostics');

// Use export keyword to make a symbol available in scripting debug console
export const Diagnostics = require('Diagnostics');

let x = P.outputs.getScalar('Nomer');

let quotes = [
  'abcde.',
  'qwerty',
  'xyzw',
  'hello',
  'bye',
  ];

 let chosenQuotes = quotes[2];

 P.inputs.setString('Quotes', chosenQuotes);

That works, but if I change the 2 from line 19, with an x from line 9, it doesn’t work anymore. what am I missing here?

When you get an output, it returns a promise, so step 1 is to await the promise. That will give you an event source, I think.
let x = await P.outputs.getScalar('Nomer');

Next step is to monitor the event source.

x.monitor({fireOnInitialValue: true}).subscribe((snapshot) => {
  Diagnostics.log(snapshot.newValue)
})

I haven’t run this code but I think it’ll work. You can use the snapshot’s newValue to select an item in the array

1 Like

WOW! Thanks Josh! It works!

const P = require('Patches');
const S = require('Scene');
const R = require('Reactive');
const D = require('Diagnostics');

(async function () { 

  let x = await P.outputs.getScalar('Nomer');

  x.monitor({fireOnInitialValue: true}).subscribe((snapshot) => {

    let quotes = [
      'abcde.',
      'qwerty',
      'xyzw',
      'hello',
      'bye',
      ];

    P.inputs.setString('Quotes', quotes[snapshot.newValue]);
    D.log(snapshot.newValue)
  })

})();
1 Like