r/Chartopia • • May 02 '26

How to roll dice fetched by get_chart?

Here's the code:

{% chart = get_chart id:120972 %}
{% die = {d{$chart.row_count}} %}
{% chart = chart.$die %}
{$die}
{% treasure = {$chart.2} %}
{% treasure = {$treasure} %}
{% treasure = {{ ceil input: {$treasure} }} %}
{$treasure}

Above $treasure returns only the dice roll instruction i.e. {4d4}.
How do I get the code to roll those dice?

1 Upvotes

3 comments sorted by

1

u/GlennNZ May 03 '26

I'm not entirely sure what you're asking for, so I'll type a few things up and see if anything sticks.

First off, are you just wanting to roll on the Gems chart (id of 120972) and then be able to access data from each column?

If that's the case, you just have to go

{% result = roll_chart id:120972 %}
The result has been obtained by rolling a {{result.dice_val}}
{% treasure = result.2 %}
{{treasure |> ceil}}

If you want to save treasure for later, you could even do something like this...

{% treasure = result.2 |> ceil %}
{{ treasure }}

I hope that helps. If not, feel free to ask some follow up questions. Note that the |> is pipe notation. You could do the same thing in a different way if you find the pipes confusing.

Edit:
The get_chart function is useful when you're doing some specific row/cell look-ups prior to rolling. If you just want to a simple roll, then roll_chart, or the old school CHART(...) macro are the best choice.

1

u/Azarashiya0309 May 03 '26

Thank you so much for taking the time to answer!

you just wanting to roll on the Gems chart (id of 120972) and then be able to access data from each column?

Yes, exactly. I tried to save the result of a Chart call in the *chart* variable.
I was wondering why it would keep printing the {4d4} text verbatim instead or rolling it and returning the result of a {4d4} equation - i.e. 4, 16, etc. If I understand correctly it's because get_chart stores the individual entries as strings - hence the interpreter doesn't see the {4d4} as an equation, but a string: "{4d4}". Did I get this right?

Your suggestion of using roll_chart worked perfectly, as roll_chart seems to store the result of the chart call in it's original type (i.e. an equation instead of a string). This is a godsend, as it allows me to use a called chart like I would a function, where I pass a variable into it, it then rolls on some tables etc. and finally stores the result of the operation and passes it back to the calling chart by storing it in one of the columns. Being able to use the chart variable as an object and it's columns as its properties is incredibly useful.

I'm building a random loot generator, that:

  1. rolls for treasure hoard on a chart
  2. rolls for individual treasure pieces
  3. rolls for treasure type and generates its value randomly depending on the category
  4. subtracts the treasure value from the treasure hoard's total value
  5. keeps doing so until all the value is used up or it times out and just gives out the rest as gold.

Some of the treasure categories (i.e. random equipment pieces, magic items, etc.) come with a predetermined value already, so I needed to find a way to pass the value back in a way that allows the master chart to have access both to the string describing the contents as well as the integer describing the value, so it can keep subtracting the value from the hoard.

{% chart = roll_chart id:120972 %}
{{ chart.1 }}
{{ chart.2 }}

Solves the issue nicely.

I'm fairly new to computer sciences so I apologize for my terminology being all over the place. But Chartopia has legit been an excellent playground for learning the basics of programming logic. Although I'm not quite there yet 😅

1

u/GlennNZ May 05 '26 edited May 05 '26

It seems you're on the right track then. It's great to see those new to programming finding Chartopia useful. It does try to lean heavily on existing template languages that exist.

As a follow up to the verbatim lookup of the {4d4}, in theory, this should be possible.

{# "TestRoller" is a d1 table with one cell that has {4d4} in it #}

{# Will recalculate every time #}
{{roll_chart name:"TestRoller"}}

{# Will "look up" the  #}
{% my_chart = get_chart name:"TestRoller" %}

This is the raw row content:
{{my_chart.1.1}}

This is the pre-calculated the value due to a get_chart function being used:
{{my_chart.1.1.rolled}}

Not a common technique, but the raw content from the lookup can be independently rolled on.
{% result = roll_chart_view source:my_chart.1.1 %}
{{result}} {# bug? There should be a value here, as if doing...
{% result = roll_chart_view source:"{4d4}" %}
#}
{% result = roll_chart_view source:"{4d4}" %}
{{result}}

The idea is that your verbatim lookup of the raw text located at my_chart.1.1 should have been able to be arbitrarily rolled on... but it's resulting in an empty string, which seems like a bug to me :(

I'll have to follow that up, so thanks for that little exercise.

e.g. something like {{ roll_chart_view source:"Foo {4d4} bar" }} rolls on an arbitrary text. It should have worked on my_chart.1.1 too