Previous topicNext topic

Writable(!) variables

Questions, comments, feedback, etc.
Post Reply
TKS
Posts: 139
Joined: Mon May 17, 2021 10:40 am

Writable(!) variables

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

Re: Writable(!) variables

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

Re: Writable(!) variables

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

Re: Writable(!) variables

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

Re: Writable(!) variables

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

Re: Writable(!) variables

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

Re: Writable(!) variables

Post 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:
Magic
Site Admin
Posts: 3440
Joined: Wed Apr 09, 2014 9:28 pm

Re: Writable(!) variables

Post 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.
Post Reply