Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
May your rolls be chill this holiday season!
Create a free account

Recursive function

Hi. My group has been doing fine playing a narrative system with Roll20, but I have another more involved system that uses more math. I'd like to have a function that receives an input and gives an output. In some cases a function needs to be somewhat recursive, i.e., using a value in more than one place. For example, if I need to determine a value for a sum, over the board I would use (x^2+x)/2. Is this doable with a macro, or do I need an api script? Thanks for having such a good support community.
1597260647
The Aaron
Roll20 Production Team
API Scripter
If you are querying for x, then yes: [[ (?{x}**2+?{x})/2 ]] If x is a roll, it's probably possible but more complicated.
Just a matter of technical accuracy, (x^2+x)/2 is a polynomial and not a recursive function. Mathematically, recursive functions are typically defined in terms of sequence where the function evaluated at certain point in the sequence depends on the function evaluated at some set of previous points in the sequence with initial values defined.  For example,  f(n) = g(f(n-1),f(n-2),...,f(n-k)) where g is another function.  To give a more concrete example, if g(x,y) = x+y. with f(0) = f(1) = 1, then f(n) would define the Fibonacci sequence. EDIT: some notational clean up.
1597265466
GiGs
Pro
Sheet Author
API Scripter
Narrator said: For example, if I need to determine a value for a sum, over the board I would use (x^2+x)/2. Is this doable with a macro, or do I need an api script?  Terminology aside: if x is a query, as Aaron says, or its an attribute on a character sheet, then yes you can do this in a macro. If its a roll, you will almost certainly need to use the API.
I used the wrong terminology, but I think I got the info. If I can re-use a variable in a macro then I can do it. Thanks.
1597270135
GiGs
Pro
Sheet Author
API Scripter
Narrator said: I used the wrong terminology, but I think I got the info. If I can re-use a variable in a macro then I can do it. Thanks. Roll20 macros dont have a concept of a variable - you can only reuse certain kinds of values - attribute values, and queries. There's also the recently discovered Reusing Rolls trick (see Stupid Tricks thread) which allows reusing rolls in certain situations - but that wont work for the situation described in your formula. If you can describe exactly what you need to do, in full detail, we can tell you one way or another if it can be done.
1597296040

Edited 1597335775
I've read more of the forums now, and I understand a bit more about the limitations. What I had hoped was to use macros as functions I could refer to for a variety of game mechanics, so that I didn't have to type in the formula each time, using them like subroutines. For example, in the game an attribute's point cost is based on its rank, where each cost is equal to all of the ranks up to that one added together (the sum), so that a rank of 4 costs 1+2+3+4, or 10 points. If I had a function that would let me give a rank and return the sum, it would be re-usable. The way it is I have to type in a formula for each instance, which is fine. But I also have other uses for that function, as well as for a reverse version (provide cost and get rank, a more involved formula). What I can do is just copy and paste for each case, then insert the relevant constants. I'm trying to digest the api script protocols for Roll20, which would seem to enable the creations of callable functions, but am I right that even then the result would post to chat, not provide input for attributes or macros?
1597335973
GiGs
Pro
Sheet Author
API Scripter
The API can modify attributes on sheets, it doesnt have to post to chat (it can also do both in the same script). To be clear though: are you creating a custom character sheet, or are these macros that you want to add to an existing sheet? If you are creating a custom character sheet, this is much easier-  you just use sheet workers (which are a cutdown version of javascript, with character sheet-specific functions).
Creating a custom sheet. Thanks for being so helpful.
1597423222
GiGs
Pro
Sheet Author
API Scripter
In that case, you're solutions are likely pretty simple. If you can post specific formulas you want to use in context (e.g. list the input attributes by name, the operation that is performed on them, and the output attribute, we can easily show you how to do it. Looking on the wiki in the sheet workers section is probably helpful too.
Ok, Here's an example, and I can extrapolate to the others. The idea is to have the point cost automated during character creation, so for strength,  ST is the input, STCST is the output.   STCST = (ST^2 + ST)/2  I'll try to learn more about this and contribute when I can, and look into the wiki. Thanks