# Allow text globals to reevaluate instead of caching them per-formula

**URL:** <https://forum.kustom.rocks/t/allow-text-globals-to-reevaluate-instead-of-caching-them-per-formula/9086>\
**Category:** Ideas\
**Tags:** globals, kode\
**Created:** [October 23, 2024, 7:28am UTC](https://forum.kustom.rocks/t/allow-text-globals-to-reevaluate-instead-of-caching-them-per-formula/9086 "2024-10-23T07:28:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thetored](https://forum.kustom.rocks/user_avatar/forum.kustom.rocks/thetored/32/5316_2.png) [@thetored](https://forum.kustom.rocks/u/thetored)\
**Post date:** [October 23, 2024, 7:28am UTC](https://forum.kustom.rocks/t/allow-text-globals-to-reevaluate-instead-of-caching-them-per-formula/9086/1 "2024-10-23T07:28:19Z")

</div>

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$`

```auto
$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()`.

---

<div class="post-metadata">

**Author:** ![Baju\_Santiko](https://forum.kustom.rocks/user_avatar/forum.kustom.rocks/baju_santiko/32/6471_2.png) [@Baju\_Santiko](https://forum.kustom.rocks/u/Baju_Santiko)\
**Post date:** [September 20, 2025, 1:29am UTC](https://forum.kustom.rocks/t/allow-text-globals-to-reevaluate-instead-of-caching-them-per-formula/9086/2 "2025-09-20T01:29:50Z")

</div>

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

![Local and Global Variables](https://forumcdn.kustom.rocks/original/2X/1/17f0444f4226fe0537a93f6a1482d83a4ce4d1ad.gif)

NOTE  
You cannot set a local variable while using it in one process.

I hope this would help.  
☺👍

---

<div class="post-metadata">

**Author:** ![thetored](https://forum.kustom.rocks/user_avatar/forum.kustom.rocks/thetored/32/5316_2.png) [@thetored](https://forum.kustom.rocks/u/thetored)\
**Post date:** [September 24, 2025, 10:56am UTC](https://forum.kustom.rocks/t/allow-text-globals-to-reevaluate-instead-of-caching-them-per-formula/9086/3 "2025-09-24T10:56:24Z")

</div>

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$`.

```plaintext
$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.

---

<div class="post-metadata">

**Author:** ![Baju\_Santiko](https://forum.kustom.rocks/user_avatar/forum.kustom.rocks/baju_santiko/32/6471_2.png) [@Baju\_Santiko](https://forum.kustom.rocks/u/Baju_Santiko)\
**Post date:** [September 25, 2025, 1:37am UTC](https://forum.kustom.rocks/t/allow-text-globals-to-reevaluate-instead-of-caching-them-per-formula/9086/4 "2025-09-25T01:37:56Z")

</div>

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.  
☺👍

---

<div class="post-metadata">

**Author:** ![thetored](https://forum.kustom.rocks/user_avatar/forum.kustom.rocks/thetored/32/5316_2.png) [@thetored](https://forum.kustom.rocks/u/thetored)\
**Post date:** [September 25, 2025, 8:22am UTC](https://forum.kustom.rocks/t/allow-text-globals-to-reevaluate-instead-of-caching-them-per-formula/9086/5 "2025-09-25T08:22:23Z")

</div>

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.
