switch for numerical values?

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
gmg
Kobold
Posts: 5
Joined: Mon Aug 17, 2020 8:15 am

switch for numerical values?

Post by gmg »

I want to have a macro that prints something depending on 1d100 result. For example let's say that I have a table of the form:

01-20: A
21-50: B
51-60: C
61-90: D
91-00: E

What is the most efficient way to program this? First I guessed that SWITCH could be used, but my understanding is that it only works with strings. Can I avoid nested ifs?

User avatar
Full Bleed
Demigod
Posts: 4736
Joined: Sun Feb 25, 2007 11:53 am
Location: FL

Re: switch for numerical values?

Post by Full Bleed »

gmg wrote:
Thu Aug 20, 2020 1:46 pm
I want to have a macro that prints something depending on 1d100 result. For example let's say that I have a table of the form:

01-20: A
21-50: B
51-60: C
61-90: D
91-00: E

What is the most efficient way to program this? First I guessed that SWITCH could be used, but my understanding is that it only works with strings. Can I avoid nested ifs?
Why not use an actual table to store the data?

http://lmwcs.com/rptools/wiki/Introduction_to_Tables


Then you have a whole host of functions to work with: http://lmwcs.com/rptools/wiki/Category:Table_Function
Maptool is the Millennium Falcon of VTT's -- "She may not look like much, but she's got it where it counts."

gmg
Kobold
Posts: 5
Joined: Mon Aug 17, 2020 8:15 am

Re: switch for numerical values?

Post by gmg »

Oh, I didn't know about tables :/

Thanks for that :)

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: switch for numerical values?

Post by aliasmask »

Here's another non-table method.

Code: Select all

<!-- use highest to demark breaks. -->
[H: indexes = "20,50,60,90,100"]
[H: values = "A,B,C,D,E"]

[H: roll = 1d100]
[H: result = 0]

[H, foreach(index,indexes), code: {
   [H, if(roll > index): result = result + 1]
}]

[H: finalValue = listGet(values,result)]

User avatar
Full Bleed
Demigod
Posts: 4736
Joined: Sun Feb 25, 2007 11:53 am
Location: FL

Re: switch for numerical values?

Post by Full Bleed »

gmg wrote:
Thu Aug 20, 2020 9:00 pm
Oh, I didn't know about tables :/

Thanks for that :)
NP.

Glad you jumped in to get the info. The stuff MT can do and people don't know about is staggering.
Maptool is the Millennium Falcon of VTT's -- "She may not look like much, but she's got it where it counts."

Post Reply

Return to “Macros”