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 .
×
Create a free account

Substrings in macros?

March 21 (3 years ago)

Is it possible to substring an attribute in a macro? so for example on a character sheet, I have an attribute called "character_height_detail" on a specific token this height attribute is set to [6'2"] I would like to break down this attribute into a 6 and a 2. This would require substring or something I'm not sure but I'm not sure what is available to variable manipulation in macros or if there is an API that produces a command that could substring.

March 21 (3 years ago)
Oosh
Sheet Author
API Scripter

That's a negative with vanilla macros - although you can coax the first integer out, assuming those squackets [ ] aren't actually part of the value:

[[6'2"]]

This works pretty much like JS's parseInt() and will give you the 6.


It would be easy to split apart properly with an API script, but.... what do you want to do with the numbers? A generic script that returns feet and inches is easy enough, but spitting the numbers into chat doesn't seem like it's getting you very far, when you can just read them off the screen with a normal @{attribute} call.

March 21 (3 years ago)
David M.
Pro
API Scripter

Scriptcards has some built-in string functions that could do it.

March 21 (3 years ago)

trying to create a macro using the token-mod command that automagically sets size of the token based on the rp height of a character. i already have a macro where I can manually plug in the values but I'd also like to be able to do it based on this attribute if possible.

March 21 (3 years ago)

Edited March 22 (3 years ago)
Oosh
Sheet Author
API Scripter

The easiest option is to just write 6.2 instead of 6"2' on the sheet. 6.2 is a float and can go straight into the parser. You could do "6.2 feet" or something - sure, the metric crowd will get angry if you use a decimal point with an imperial measurement, but it works - [[6.2 feet]] just gets parsed down to 6.2.

Next easiest is to just do [[6"2']] - but this means the tokens will only size on feet, not inches, as you lose everything after the first non-digit.

But the best option would be David's suggestion of using scriptcards. That's out of my wheelhouse, as I've never used it. You'll doubtless find other uses for it once you learn its ways, it's rather versatile, but will take a bit of learning.


March 22 (3 years ago)

Edited March 22 (3 years ago)
David M.
Pro
API Scripter

Here's an example scriptcard macro that takes the value of the character_height_detail attribute for a single selected token, parses the string value into decimal feet, and normalizes to a unit height using token-mod (6ft=1u in this case). I've added comments to explain what is happening, as the syntax may seem funky. See the scriptcards wiki for more explanation of the functions. 

Required scripts: Scriptcards, SelectManager, token-mod

!script {{
  --#whisper|gm
  --#title|Set Token Height Example
  --&myStr|@{selected|character_height_detail}
  --#leftsub|height attr val: [&myStr]

  --/Assign a roll variable with the height to normalize selected token height to
  --=unitHgt|6  

  --/Splits attr value into multiple "hgt" string variables, appended with the index (1-based)
  --~hgt|string;split;';[&myStr]

  --/Trim the leading and trailing spaces
  --~hgt1|string;trim;[&hgt1]
  --~hgt2|string;trim;[&hgt2]

  --/Remove the double quote for inches from the second term
  --~hgt2|string;replaceall;";;[&hgt2]

  --/Assign roll variable for decimal feet based on string vals for later math (note scriptcards math reads left to right and does not use PEMDAS)
  --=hgtFeet|[&hgt2]/12 + [&hgt1]

  --/Normalize to unit height (rounded to two decimal places)
  --=newHgtUnits|[$hgtFeet] / [$unitHgt] {ROUND:2}

  --/Set new token height with token-mod
  --@forselected|token-mod _set height|[$newHgtUnits]u

  --/Whisper results to the GM
  --+Token height set to|[$newHgtUnits] units
}}