0

I'm would say I know only a little bit on Javascript but enough to add simple interactivity to apps / websites. However, recently I came across an app that uses reactive javascript instead of the normal javascript that I'm used to. (not sure what the 'Normal' javascript is called.)

Conventional If-Else statements do not seem to work in reactive javascript so I was wondering if anyone can help me figure out what this line would be in reactive javascript? It would be really helpful if anyone can teach me how you do it / direct me to a source where I can learn more.

const car = Scene.root.find('Collider');
const carMaterial = Materials.get('ColliderMat');
const carMaterial2 = Materials.get('ColliderMat2');
var loopCountNum = Patches.getScalarValue('loopCount');

if (loopCountNum > = 0)
{
  car.material = carMaterial;
}

else if(loopCountNum >= 10)
{
  car.material = carMaterial2;
}

The idea here is that when the loopCount reaches a certain range, the car material changes. I don't know how it works in Reactive Javascript and has been struggling with this for awhile now. Any help would be greatly appreciated.

(Loop Count is derived from another function and it is working properly)

Levin
  • 65
  • 1
  • 2
  • 10
  • 2
    Even in "normal" JavaSCript, how could you reach the `else-if` condition? –  Dec 04 '19 at 10:11
  • 4
    The `else if` statement will never be reached because `loopCount >= 0` is always `true` when `loopCount >= 10`. – Titus Dec 04 '19 at 10:11
  • The loopCountNum >= 0 condition does not execute even when the second condition is omitted. – Levin Dec 04 '19 at 10:12
  • may be your first if statement should be something like `if (loopCountNum > = 0 && loopCountNum < 10)` – Punith Jain Dec 04 '19 at 10:17
  • Your condition could be written as a single line ternary `car.material = (loopCountNum >= 10) ? carMaterial2 : carMaterial;` but Im not sure if this solves your issue? What is your issue? – Craicerjack Dec 04 '19 at 10:17
  • The issue is that the car material is not showing when the condition seems to make sense. – Levin Dec 04 '19 at 10:19
  • In RxJs (Reactive Javascript) you have subjects which emits values to their subscribers. So basically you don't have simple if/else statements. More like you are using pipe functions like map/filter to modify or react to new emitted values. In your problem, the context is totally missing. It is not clear which is a object containing a value and which are observables. – Ling Vu Dec 04 '19 at 10:21
  • 1
    The issue is that `loopCountNum` is not a number, it's a signal as described here: https://sparkar.facebook.com/ar-studio/learn/documentation/reference/classes/patchesmodule/ – Alexey Lebedev Dec 04 '19 at 10:22
  • Oh? I thought that scalar value is an Integer. Is there a way to convert it to an Integer then? I tried using ParseInt() but it doesn't seem to work too. – Levin Dec 04 '19 at 10:26
  • 1
    Signal doesn't correspond to a single integer value, it's more like a stream of integer values that you have to connect to something that receives a stream. Sorry can't offer more specific advice, Spark AR documentation is pretty bad. – Alexey Lebedev Dec 04 '19 at 10:32
  • Okay! Thank you guys so much! I'll fish around for more information and I'll ask in the facebook forum group. Thanks again! – Levin Dec 04 '19 at 10:37
  • I think the solution might look something like `car.material = loopCountNum.lt(10). ifThenElse(carMaterial, carMaterial2)`. Based on https://sparkar.facebook.com/ar-studio/learn/documentation/reference/classes/reactivemodule/ and https://sparkar.facebook.com/ar-studio/learn/documentation/reference/classes/reactivemodule.boolsignal/ – Alexey Lebedev Dec 04 '19 at 10:40

0 Answers0