Coding Ideas and Discussion for Character Sheet Action Building (was: Have testing syntax recognize characters and strings, not just numbers)

So, I am working on a character sheet where an attack is categorized as either a melee attack, a ranged attack or a threaten attack. I would like to have a stat Attack_Type that could be defined as “M”, “R” or “T”, and then have the action take that stat and apply the appropriate bonuses. However, when I try to do a test like “|Bonus| !({Attack_Type==“T” ? Threaten_Bonus : Attack_Type==“R”? Range_Bonus : Melee_Bonus}d{CD}) |” I get an error of “Error: Cannot convert “R” to a number”. So, either I’m doing something wrong, or characters can’t be used in testing syntax.

(I have a working version of the sheet which uses 1, 2, 3 for values of the Attack_Type stat with a text block next to the stat that says “1=Melee, 2=Ranged, 3=Threaten”. Clumsy, but it works, in the absence of a real solution.)

Try wrapping the Stat name in string(), for example string(Attack_Type).

Well, I tried that. Here’s the code that I wrote:

|Bonus| !({String(Attack_Type)==“T” ? Threaten_Bonus : String(Attack_Type)==“R”? Range_Bonus : Melee_Bonus}d{CD}) |

And here’s the error that I got:

TypeError: (intermediate value)(intermediate value)(intermediate value) is not a function

Did I get something wrong in coding that?

Ok, I’m trying a bunch of different variations on this, just to see if there’s a way to get it to work. My latest variation is this:

|Bonus| !({String(Attack_Type)==String(Threaten) ? Threaten_Bonus : String(Attack_Type)==String(Ranged)? Range_Bonus : Melee_Bonus}d{CD}) |

Where the “Threaten”, “Ranged”, and “Melee” are stats with the values “T”, “R”, and “M”, respectively. Doing this, I got the same error as before (the “intermediate value” error).

I’ll keep thinking about this to see if I can come up with a way to do this, but I welcome any further input.

Edit: So, I’ve tried just about every combination that I can think of, without understanding the programming syntax a bit deeper. Nothing that I’ve tried has given me any successes, getting either the “cannot convert” error or the “intermediate value” error. So, if anyone has any more suggestions, I’d love to hear them.

Failing that, my feature request stands to have a character or string comparison capability.

Ahh! I believe you need to use compareText(Stat1, "Value")

This is a function in MathJS, which is what Astral uses for all its internals.

1 Like

And now it’s time for me to say: Ah ha!

As it turns out, I don’t want compareText, but I want equalText! The function compareText returns too much nuance, I only want a True/False regarding whether or not it’s equal. Thus, the final, and functional, version of the code is:

|Bonus| !({equalText(Attack_Type,“T”)? Threaten_Bonus : equalText(Attack_Type,“R”)? Range_Bonus : Melee_Bonus}d{CD}) |

And now it works!

Thank you for helping me to find the right solution. I have moved this out of the feature request category, but I’d be happy to leave it as an open thread for discussing the deeper functionality of the MathJS implementation in Astral…

Leaving open for discussion. Thanks @SnowyRiver for bringing this point up. I have used string comparison in one of my sheets but it is undocumented on Astral.

For anyone interested in digging deeper into MathJS and what it can do: math.js | an extensive math library for JavaScript and Node.js


It would be nice to see more documentation on this. That said just knowing the MathJS point is enough for me to figure out what I can do in Astral.

I agree on both counts. Knowing that we’re working with MathJS as a starting point is great. It would still be nice to have some Astral specific documentation of the capabilities of using the MathJS calls,

The only thing specific to Astral is no Vector functions, no array functions, no PI.

Other than that, I can’t think of anything specific to using MathJS in Astral.

Hi @LaMorte,

Thanks for those details. I was actually meaning something more along the lines of having some examples. My case above would be one such example. It could be something along the lines of saying:

You can have a Stat that is a character or string and have that control aspects of an action. For example, if you have a Stat for an attack that defines it as a Melee attack, a Ranged attack or an attack that is a Threat. For the Stat, this could be handled with the first letter: “M”, “R” or “T”. Then, in the attack action, the test for how many bonus d6’s to apply would be handled with the equalText function:
Bonus: !({equalText(Attack_Type,“T”)? Threaten_Bonus : equalText(Attack_Type,“R”)? Range_Bonus : Melee_Bonus}d6) |

This just from the standpoint of saying that handing people ideas like this is a good way to help inspire new and better ideas by giving them more of a starting point, rather than just the catalog of MathJS functions.

The problem is that with the hundreds of options available through MathJS you would need thousands of pages of documentation for even a sliver of the possible combinations in which you could use them.

The Astral team would never get any actual work done! :grin:

Oh, I don’t think that this is a job for the Dev team. They have MUCH more important things to do. This is where WE need to come in and make sure that we share what we’re doing. Maybe having a whole sub-forum that could focused on ideas for programming, so those of us who are good at that kind of thing can help each other, and help those who are just learning…

Now, maybe that should be a new request…

Some of the Archmagi are working on a Wiki. It will still be a while before it becomes official, but that would better suit that kind of endeavor.

1 Like

Ok, here’s another one that I’ve been trying to puzzle out. I don’t know if there is a way to do this at all.

I want to have a check box control whether one die or two dice are rolled by an action. Now, it’s important to understand that these are two separate dice, not the difference between “1d6 and 2d6”, but more the difference between “1d6” and “1d6, 1d8”. I’ve been able to make it so that the action checks the checkbox stat and will leave the second die as a blank if the box is not checked, but that still puts the box of “there is a calculated value here” in the result in the chat, the box is just empty.


It occurred to me that something like a For-Next Loop could potentially solve this problem, but I don’t think that’s available, or at least my delving into MathJS hasn’t revealed it to me.

Any thoughts?

Astral does not support loops as it only allows for a single statement executiion per line.

The box was a design decision that RedMega and I came to after solving the issue of the parser crashing when Astral encountered an output of “” (empty string) from a ternary.

1 Like

So, what you’re saying is that there is no real way to do what I want to do with the current implementation of the scripting language. That’s disappointing.

Oh well. I have an alternative, it’s just not as . . . elegant as I’d like it to be.

Yeah, the thing is: it’s not really a scripting language.

It’s more like a very fancy calculator. :wink:

Yeah. I totally get that. I’m just one who finds the limits of things like this and then I try to push them. There are spreadsheets that I’ve built where I’ve had people say that they are astonished because they didn’t know that it was possible to make a spreadsheet do those things (and that’s not using programming plug-ins, just the built in calculating capability of the sheets).

So, just chalk this up to me trying to figure out what the limits are and pushing them around. And, whatever I say here, I’m happy as a clam to have this and whatever capabilities come along with it. I’m having a blast problem solving and working around what I can do to get the result that I want.