Callback for a signal's value chaged subscription not being executed

Hi guys, I’m having an issue with some code for my filter, and seems the simplified snippet below has the same issue, so I’ll base it on that. Basically, I need to subscribe to the changes of a ScalarSignal, which is changed inside an onTap() callback, but it doesn’t seem to work. Any ideas?

let id = Reactive.val(0);

TouchGestures.onTap().subscribe(() => {
    id = id.add(1);
id.monitor().subscribe((event) => Diagnostics.log("Why this never runs after a screen tap?"));

It’s not firing because you are reassigning the signal value. You are listening to the old signal and not the new one (id.add() returns a new signal, not the original). I don’t think there’s any way to monitor a constant signal value, but you could trigger a function to handle your logic every time you change the signal value. It’s a little tedious, but it will work.

Thanks for answering Josh! The copy things make a lot of sense. It’s a pity the internal state of a signal can’t be changed through scripting so as to be able to monitor value changes from there (which I guess makes monitor() effectively useless for script-only signals?).

Handling the signal in the Patch Editor and importing it into the script works fine as usual, so the Editor is definitely handling signals quite differently (probably modifying the signal instance’s internal state instead of making copies each time a new value is assigned) than the scripting engine / API.

The solution you mentioned was in fact the workaround I found, but as you said it was more tedious and it just felt more natural to encapsulate the logic inside the signal change event on its own. But good luck with that for now it seems :sweat_smile:

Thanks again for your kind help!

Oh, actually… this reminded me of writable signals! There are a few types of signals that have “source” variants that can be used to set values while maintaining the original signal. I think you might need to enable the writable signal source capability before you use the “source” signal types.

  // Create a scalar signal source
  var scalarSource = Reactive.scalarSignalSource("ScalarSource");

  // Get the scalar signal from the source
  const scalarSignal = scalarSource.signal;

  // Set an initial value for the scalar signal

  // Add the signal to the Watch view"Scalar value: ", scalarSignal);

  // Create a variable to keep track of the tap count
  var count = 0;

  // Update the scalar value each time a tap event is fired
  TouchGestures.onTap().subscribe(() => {

    // Increment 'count' value
    count += 1;

    // Set the value of the scalar signal to the value of 'count'

Nice, didn’t know about signal sources and that’s exactly what I was looking for :grin:
It’s working great with sources now.

Thanks for the tip Josh!