Generic debounce logic (or, how do I pass variables by reference?)

Using this code as a guide, I’m trying to implement a generic button debounce method:

var nextBtn = {
  state: false,
  timer: 0.0,
  action: () => sequencerNext(),
};

var prevBtn = {
  state: false,
  timer: 0.0,
  action: () => sequencerPrevious(),
};

export function beforeRender(delta) {
  debounce(delta, analogInputs[2], nextBtn)
  debounce(delta, analogInputs[1], prevBtn)
}

export function debounce(delta, value, btn) {
  var newState = value > 0.5;
  if (newState == btn.state) {
    btn.timer = 0;
  } else {
    btn.timer += delta;
  }
  if (btn.timer > 20) {
    btn.state = newState;
    if (btn.state) {
      btn.action();
    }
  }
}

I’m getting “Unsupported type ObjectExpression” as an error on the line I declare nextBtn. I need to do something like this though, as I have to pass the state and timer variables by reference into debounce(). Is what I’m trying to do possible or am I just messing up the syntax slightly?

Oh, wait a minute, reading comprehension fail (from the docs):

These are language features you’d expect to work writing JavaScript that won’t run on Pixelblaze.

  • Objects, named properties, classes, etc.

So, is it possible to pass by reference at all? Or is the simplest thing just to duplicate the debounce logic (which is nonetheless a little bit painful without includes of some kind.)

Arrays and functions are passed by reference.

If you search the forum for “debounce” there’s a few different examples, but I think this one is probably close to what you’re looking for:

Oh, ok, so I probably could have done it with arrays instead of objects. I’ve gone and duplicated the whole logic in the mean time, much of a muchness now I guess.

1 Like

Is it possible to call this out somewhere in the docs?

Hmm we could, but in an effort to keep the docs succinct, it seems we generally haven’t been calling out much that specifically works just like ECMAScript does (PB language is a subset of ECMAScript).

I believe C works similarly, though it passes the pointer itself by value.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.