Initialize variables once in Javascript

implict's picture

How to initialize variable once in Javascript? I need to memorize and use old variables data. For example, in another programming environment, whenever executed some function, it can saves input arguments to array variable ; array.push(arg). So it makes possible to get some old data in array variable. But in qc, variables initialized newly at each frame. It is ok, not a way using Javascript. Does anybody help?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

gtoledo3's picture
Re: Initialize variables once in Javascript

If I'm understanding you correctly, I'm pretty sure that before your function you can just list something like:

__number value=1.; __structure vector=[0.,0.,0.,0.];

Then refer to "value" or "vector" in your code. If this isn't what you mean, maybe give a shot at explaining it again. I'm not 100% sure what you mean by "get some old data in array variable". If you need to get data from a past frame, you can make a feedback loop, or maybe handle it some other way(?).'s picture
Re: Initialize variables once in Javascript

Sounds like you want persistant variables across QC frames.

Declare them outside the 'main' function (using "var" command). That way they become globals accessible in any function and not initialised when first declared (auto-declaration) in any function (including the 'main' function). Functions have access to all the variables declared in the function that called them as well as anything passed in the function arguments.

Global Array.qtz38.42 KB
Var scope.qtz35.37 KB's picture
Re: Initialize variables once in Javascript

I put Log statements in JS to help explain and I find them invaluable as the only debugging tool available. Really slows down execution if you Log structures every frame. So here it is commented out.

Global Array no Log statements.qtz38.43 KB