biomusor
biomusor

Reputation: 17

Spark AR Why if-else if construction doesn't work?

I don't understand why if-else construction doesn't work.

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


var num = 5;
var firstCondition= false;
var secondCondition = false;

function func(){
    firstCondition = Reactive.ge(num, 10);
    secondCondition = Reactive.lt(num, 10);
    if(firstCondition){             //false
      num = 0;
    } else if(secondCondition){     //true
      num = 1;
    }
}
func();

Diagnostics.watch("num - ", num);
Diagnostics.watch("firstCondition ", firstCondition);
Diagnostics.watch("secondCondition ", secondCondition);

num shows 0:( What am I doing wrong? Is it specific of reactive programming?

Upvotes: 0

Views: 1259

Answers (3)

lisichka ggg
lisichka ggg

Reputation: 660

You need to use different mindset here:

const conditionedSignal = inputSignal.ge(10).ifThenElse(0, 1);

This is a scalarSignal class, not a JS Number!

Upvotes: 1

Nithin NS
Nithin NS

Reputation: 51

Spark AR Studio uses reactive programming, a declarative programming model that uses asynchronous data streams. This guide will cover the benefits and use of reactive programming within Spark AR Studio.

https://sparkar.facebook.com/ar-studio/learn/documentation/scripting/reactive/

Upvotes: 0

Mathieu Hackz
Mathieu Hackz

Reputation: 71

There are two issues in your example:

  • signals can be confusing and return true just because the variable is there, so you need to check: if (firstCondition.pinLastValue()) to get the value of the boolean signal.
  • secondly, I guess your function is not updated, you need something like this:

    const Time = require('Time'); const interval = Time.setInterval(func(), 500);
    

Upvotes: 2

Related Questions