Current Page: NeXtMidas Training Primitives Primitive Restarts Preserving State
- - - | Preserving State: On occasion it may be necessary to preserve some state across a restart. The standard way to do this is as follows: All variables where state is NOT preserved should be initialized in `open()`. All variables where state is preserved should be initialized in `open()` if they are `null` (or `NaN`, etc.), and be left unaltered otherwise. Example:
          public class myprim extends Primitive {
            private double frameRate; // Default value set in open()
            private double frequency = 1024.0;
            private double amplitude = -1.0;

            public int open() {
              frameRate = MA.getD("RATE", 16.0);       // Always reset frameRate
              frequency = MA.getD("FREQ", frequency);  // Reset frequency if given on command line
              if (amplitude <= 0) {
                amplitude = MA.getD("AMP", 1.0);       // Always preserve amplitude
              }
              ...
            }

            public int process() { ... }
            public int close() { ... }
          }
In the above example, following a restart... `frameRate` is always reset back to its initial value (as given on the command line or `16.0`). `frequency` is reset back to its initial value ONLY if `FREQ=` was specified on the command line. `amplitude` is always preserved.