Page 1 of 1

Writable(!) variables

Posted: Sat Aug 07, 2021 12:17 pm
by TKS
I want to save previous values of modules for later use/comparisons, but so far without success:

- globals seem to be "read-only" from within a 'normal' module.
- the only internal variables I know of are "x" and "y". Abusing y to save a x-value fails for obvious reasons.
- trying to define a new variable inside the Expression modifier (like described in https://github.com/ArashPartow/exprtk/b ... readme.txt) results in a syntax error.

Am I missing something? Coding expressions would be way more intuitive if I either could define own variables, or even better create globals which are writable from within a module's expression. And from other globals of course...

Re: Writable(!) variables

Posted: Sat Aug 07, 2021 2:19 pm
by Sadler
Variables may be practically unwritable, but you can assign values.

For example, this will 'compile' in an expression:

Code: Select all

var z1 ;
z1 := 5;
if (x > 3, z1, x);
This code creates a variable and separately assigns a value and does return 5 when x > 3. You can't write to globals, but I've found that being able to declare read-only variables within the scope of expressions to be useful.

Local globals have been asked for before though I can't remember if making them writable has been discussed. What application do you have in mind? There is probably a workaround.

Re: Writable(!) variables

Posted: Sat Aug 07, 2021 4:08 pm
by TKS
Sadler wrote:Variables may be practically unwritable, but you can assign values.
For example, this will 'compile' in an expression:[...]
Aaah...I've tried float, double and whatnot...that does the job. Thanks a lot
Sadler wrote: What application do you have in mind? There is probably a workaround.
A dynamic "Hold" function for modules which can be adjusted by the actual beat of a song. Right now "Hold" only accepts fixed values, and no matter how precisely I calculate this value it drifts away after some time...

Re: Writable(!) variables

Posted: Sat Aug 07, 2021 4:53 pm
by Magic
It doesn't make sense for there to be variables that last more than one frame, because every frame is its own context and would keep overwriting anything you assigned more than one frame ago.

The thing that might be helpful for you to be aware of is the "xp" variable, which means "x previous" and is the x value from the previous frame. Thus you can compare the current value with the previous one.

Re: Writable(!) variables

Posted: Sat Aug 07, 2021 5:43 pm
by TKS
Eric wrote:It doesn't make sense for there to be variables that last more than one frame, because every frame is its own context and would keep overwriting anything you assigned more than one frame ago.
That's where writable globals would help, I could save context-information as long as I need it.

Re: Writable(!) variables

Posted: Sat Aug 07, 2021 6:30 pm
by Magic
Doesn't matter whether it's global or not, the same logic applies.

If I'm understanding correctly, what you want to do is have a global, and in that global, you do something like this:

Code: Select all

if ([some condition], f(x), y)
Which means that if the condition is true, the global gets a new value (based on some function applied to the input, or perhaps just the input itself); otherwise, it gets its previous value, which means it stays the same.

Re: Writable(!) variables

Posted: Sat Aug 07, 2021 8:31 pm
by TKS
Some pseudo-code for my "Hold" function:

Code: Select all

if (Current_Time - Hold_Time > Old_Time) {
    Old_Time := Current_Time;
    x_old := x;
} else {
   x := x_old;
}
Hold_Time could be dynamically computed in a global, but I see no way to remember Old_Time.
Of course this wouldn't be necessary if the Hold parameter of a module could be changed via a global and/or an expression :mrgreen:

Re: Writable(!) variables

Posted: Sat Aug 07, 2021 10:07 pm
by Magic
Another way of saying what I said above is that you don’t assign a value to a global; rather, a global assigns a value to itself. Thus, if you want to save values for later, all your code should be inside one or more globals.