Local variables from the parent formula transfer into text globals, but it seems that the result of the text global is cached and not reevaluated.
Example:
Text global sqr: $#n * #n$
$lv(n, 2) + gv(sqr)$ $lv(n, 4) + gv(sqr)$ -> 4 4
I’d like the example to return 4 16, which would effectively enable global functions by combining lv() and gv().
1 Like
Hi there,
I tried your code snippet and I think it’s incorrect.
I think the issue is not about the global variable that is cached or not.
Here’s my suggestion:
Assuming that the global variable sqr has been created with its value set to 4 …
$lv(n, 2)$ ➞ define a new local variable n and set its value to 2
$lv(n) + gv(sqr)$ ➞ calculate: the local variable + the global variable
$lv(n, 4)$ ➞ Override the local variable n with the value of 4
$lv(n) + gv(sqr)$ ➞ calculate: the local variable + the global variable

NOTE
You cannot set a local variable while using it in one process.
I hope this would help.


1 Like
I don’t think you understand what I mean here. I’m putting a formula that reads a local variable in the text global, and then setting the local variable outside and recomputing the global with a new local variable value.
When gv(sqr) is $#n * #n$.
$lv(n, 2)$ - set local variable n to 2
$gv(sqr)$ - evaluate text global, $#n * #n$ -> $2 * 2$ -> 4
$lv(n, 4)$ - set local variable to 4
$gv(sqr)$ - try to evaluate text global again
- what happens: gv(sqr) was already accessed before, so 4 is returned
- what I want to happen: evaluate text global again, $#n * #n$ -> $4 * 4$ -> 16
This is an intentionally simplified example, but you could run much more complicated logic inside the global variable and access it from anywhere by just doing lv(n, 5) + gv(sqr) instead of having to duplicate the formula every time you need to use it.
Hi there,
As far as I know, global variables are evaluated during the loading of a preset, and it won’t be reevaluated until its modified (ie. by a Flow).
So if a global variable contains a local variable, it won’t be reevaluated even if the local variable has an updated value.
IMHO, the concept of global variable is to store a value so that it can be accessed globally across the elements in a preset. It doesn’t act like a function/procedure (as in an application programming).
CMIIW
I hope this would help.


Text globals are reevaluated in the context in which they are accessed, but only once per formula. You can easily confirm this for yourself by creating a text global like gv(test) set to $si(mindex)$, and then adding 3 text objects all set to $gv(test)$ - each text object will get a different value based on its si(mindex) despite them all using the same global. This is also why $lv(n, 2) + gv(sqr)$ returns 4 the first time, if the local wasn’t set before gv(sqr) is evaluated you’d simply get #n*#n as the result - if a local variable doesn’t exist, #name is used as a string instead of as a local variable name, and * will concatenate those strings together with the operator symbol in the middle.
Almost everything needed for text globals to work as functions is already here, they are already evaluated using the context from the formula that uses them, it’s just that after the first evaluation of a global in a given formula the result is cached and thus any subsequent calls return the same value even if the context changes. This was fine before lv(), because the only surrounding context that was relevant was the module index, which couldn’t change between text global evaluations, but now with lv() we can purposefully influence the context to create global functions which would be incredibly useful.