It would be great to have the option to create or define functions (as in other programming languages), to which we can enter values (such as numbers, colors, etc.) and automatically pass them through some predefined formula. For example, being able to create a function to edit a color, and automatically pass it through a complex formula that changes its saturation and luminosity. I’m sure it would be very useful, and would make many tasks that require retyping a formula several times easier.
Haven’t used Flows myself, but I think you should be able to use that as a function(?)
Fun fact: text globals almost fulfil the need for functions, because a text global is evaluated in the context of the formula accessing it and not once per preset as you might expect. This means that if you put $si(mindex)$
(gets the index of the current object like shape/text/icon/… in its parent group/root) in a text global test
, the result of setting something to $gv(test)$
will depend on the object you set it on, not on the index of the global in the global list.
This shared context includes local variables. This means you can set a local variable in a formula and then evaluate a text global to do the equivalent of a function call. For example:
text object: $lv(n, 5) + gv(sqr)$
gv(sqr): $#n * #n$
This will result in your text object displaying 25
. lv(n, 5)
sets the value of local variable n
to 5
, #n
reads that value in the text global.
The catch? Globals are only evaluated once per formula and then cached. That means if you tried to call gv(sqr)
in the above example twice, you’d get 25
both times:
> $lv(n, 5) + gv(sqr)$ $lv(n, 3) + gv(sqr)$
< 25 25 (instead of expected "25 9")
TL;DR:
A good, hopefully easy to implement solution to this request would be to either make text globals that use lv()
to reevaluate each time they are called, add a checkbox to global options that would enable this behaviour, or even simpler make the rule apply when the text global name starts with a special character like _
or :
, so we also have an easy way to distinguish between global functions and regular text globals.
No, you can’t call flows from a formula. What is being requested here is the ability to define functions to call later. It could look something like:
- Declare global function with name
sqr
and one argument calledn
function body being$#n * #n$
- Use that function in some other formula:
$gf(sqr, 4)$
Basically, you’d have a reusable block of code that could be as complex as you want that transforms some arguments into a result value.
Yes, that’s exactly what I’m requesting, thank you!
I had previously tried the solution you propose, obtaining the same results That’s why I came to propose this idea.
I understand that the solution you propose is the one that requires the least development work, even so, I think an optimal solution would be to have a functions section, where you can create and define at least the name, the input parameters, and the body of the function. This would simplify the invocation formula, which could be, for example: “fn(parameter1,parameter2…)”.
I’m not sure, but I think it could be counterproductive to create the functions in the same formula, because we would usually use them everywhere, not in a single global or object.
A fully-featured functions section would obviously be the best, with the ability to specify function parameters, maybe examples and helper text to appear in the editor as you try to use the function. It would also be great if you could also search and add community-made functions to your presets, then there’s also the question of if functions would be stored for every Kustom app on the phone or separately for every preset.
Since even designing a feature like this is a lot of work, not to mention implementing it, I’m proposing a change that will get us most of the effect with very little effort on the part of the developer. Text globals already exist and evaluate using local variables when called, we can already make global folders so functions can be tucked away in a folder called func
or whatever (yeah you’d have to call them through gv(func/:name)
, admittedly a bit inconvenient), so (depending on how the code is written) implementing this might be a matter of adding one if(globalName.startsWith(":"))
statement where the app decides whether to recall a value from cache or evaluate the text global.
Hi there,
Have you guys ever tried Fave Formulas? You can save any custom formula (your code snippets), so that you can reuse it whenever you need it.
I hope this would help.
Not sure if it helps, but I make “global functions” where I create a text global with related local variables i need in various parts of my widget, for example:
text global named “function”, formula is $lv(output1, (gv(var) + 4 ))$
object formula mode: $gv(function)$ The output is $#output1$
this can be scaled up significantly, for example I use this to read specific key values in JSON.
OMG IT WORKS! thanks!
I created a function that transforms the incoming color depending on other variables. I created a global variable, which references another local variable called #col (it does not exist within the same formula):
$if(gv(dark),ce(ce(#col,lum,100),sat,30),ce(ce(#col,lum,40),sat,50)))$
So in the color settings of an object, I wrote the formula:
$lv(col,#FFFF0000)$$gv(fncc)$
inserting the local variable definition before calling the global variable. and it worked! Yes, it’s a bit cumbersome, haha, but it works. For complex formulas, it can be a huge time saver. Thank you so much!
This is the approach I mentioned in my post above, it works as long as you don’t use the function twice in the same formula. Try this:
$lv(col,#FFFF0000)$$gv(fncc)$
$lv(col,#FF00FF00)$$gv(fncc)$
It’ll return the same thing twice, despite #col
changing between evaluations of gv(fncc)
- the return value of gv(fncc)
gets cached and subsequent calls just recall the value from cache, so local variable changes don’t change the output.