Writable(!) variables

Sat Aug 07, 2021 12:17 pm

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...
TKS
 
Posts: 42
Joined: Mon May 17, 2021 10:40 am

Re: Writable(!) variables

Sat Aug 07, 2021 2:19 pm

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.
Sadler
 
Posts: 890
Joined: Sat Aug 02, 2014 7:10 pm
Location: London, UK

Re: Writable(!) variables

Sat Aug 07, 2021 4:08 pm

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...
TKS
 
Posts: 42
Joined: Mon May 17, 2021 10:40 am

Re: Writable(!) variables

Sat Aug 07, 2021 4:53 pm

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.
Eric
Site Admin
 
Posts: 3170
Joined: Wed Apr 09, 2014 9:28 pm

Re: Writable(!) variables

Sat Aug 07, 2021 5:43 pm

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.
TKS
 
Posts: 42
Joined: Mon May 17, 2021 10:40 am

Re: Writable(!) variables

Sat Aug 07, 2021 6:30 pm

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.
Eric
Site Admin
 
Posts: 3170
Joined: Wed Apr 09, 2014 9:28 pm

Re: Writable(!) variables

Sat Aug 07, 2021 8:31 pm

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:
TKS
 
Posts: 42
Joined: Mon May 17, 2021 10:40 am

Re: Writable(!) variables

Sat Aug 07, 2021 10:07 pm

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.
Eric
Site Admin
 
Posts: 3170
Joined: Wed Apr 09, 2014 9:28 pm

Return to General Discussion

© 2021 Color & Music, LLC • This web site is mobile-friendly