dice in text string, easy way to roll?

Thoughts, Help, Feature Requests, Bug Reports, Developing code for...

Moderators: dorpond, trevor, Azhrei

Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)
Post Reply
User avatar
celestian
Dragon
Posts: 276
Joined: Mon May 17, 2010 3:29 pm

dice in text string, easy way to roll?

Post by celestian »

Okay, lets stay I have a property that looks like this:

"1d10 sp, 1d4 gp" called "Treasure".

When the npc dies I'd like to roll those values up and put that info somewhere. My problem is I can't seem to come up with a way to "roll" it.

Code: Select all

[reward = eval(getProperty("Treasure"))]
That fails because of the "sp," and "gp" I am guessing. I've tried other things but can't seem to get it to cope with it.

Is there a way to do this w/o writing a parser to pull out all the dice rolls and replace their text with the rolled values?

I am hoping I am just missing a way to make a function that exists do this.

User avatar
JML
Dragon
Posts: 515
Joined: Mon May 31, 2010 7:03 am
Location: Blagnac, France

Re: dice in text string, easy way to roll?

Post by JML »

Take a look at Wiki: strformat() if you didn't already did. I think it should be what you're looking for.

User avatar
celestian
Dragon
Posts: 276
Joined: Mon May 17, 2010 3:29 pm

Re: dice in text string, easy way to roll?

Post by celestian »

JML wrote:Take a look at Wiki: strformat() if you didn't already did. I think it should be what you're looking for.
Unfortunately strformat presumes that the 1dX and "gp" are not already in one string. The text and the "dice roll" amount are in the same string "1d20 sp, 1d4 gp".

If 1d20 and 1d4 were separate I could use strformat but I'm trying to avoid having properties for each denomination and just use a simple treasure value that is displayed at a specific time for the party.

Right now my only thought is to use regex and pull the dice rolls out, roll them and reinsert. I am just trying to avoid that hoping I missed something that exists.

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

Re: dice in text string, easy way to roll?

Post by aliasmask »

Depends. Do you have control over how you enter the strings. For example, can you enter the string as "{1d10} sp, {1d4} gp"? If not, the you'll have to use strfind, some regex and a loop. I can help with that if you want.

Code: Select all

[evalMacro("{1d10} sp, {1d4} gp")]

User avatar
celestian
Dragon
Posts: 276
Joined: Mon May 17, 2010 3:29 pm

Re: dice in text string, easy way to roll?

Post by celestian »

aliasmask wrote:Depends. Do you have control over how you enter the strings. For example, can you enter the string as "{1d10} sp, {1d4} gp"? If not, the you'll have to use strfind, some regex and a loop. I can help with that if you want.

Code: Select all

[evalMacro("{1d10} sp, {1d4} gp")]
Yeah, I might go with that. I was hoping that I could avoid making the "users" (other than me) of the framework remember such things when editing a creature token. I'll play around with it and see if it feels good. Might just be enough that I add a "enclose dice rolls in {}" example.

User avatar
celestian
Dragon
Posts: 276
Joined: Mon May 17, 2010 3:29 pm

Re: dice in text string, easy way to roll?

Post by celestian »

celestian wrote:
aliasmask wrote:Depends. Do you have control over how you enter the strings. For example, can you enter the string as "{1d10} sp, {1d4} gp"? If not, the you'll have to use strfind, some regex and a loop. I can help with that if you want.

Code: Select all

[evalMacro("{1d10} sp, {1d4} gp")]
Yeah, I might go with that. I was hoping that I could avoid making the "users" (other than me) of the framework remember such things when editing a creature token. I'll play around with it and see if it feels good. Might just be enough that I add a "enclose dice rolls in {}" example.
So, I did a test with this and unfortunately when you load the token up with the edit macro the property "treasure" (using input()) it processes the dice roll. Which kinda makes it hard for the token to have the "{1d20} sp, {1d4} gp" as a property...

Is there a command I can wrap around "Treasure" var in this:

Code: Select all

"myTreasure|"+Treasure+"|Treasure found",


that will keep it from processing the rolls?

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

Re: dice in text string, easy way to roll?

Post by aliasmask »

Is Treasure a token property? When you use getProperty it will run execute what's in the {}'s. You can avoid that by using getRawProperty. getProperty will essentially make the roll for you. So, you can get the rolled value like this:

Code: Select all

[H: TreasureResult = getProperty("Treasure")]
[H: TreasureString = getRawProperty("Treasure")]
[R: strformat("%{TreasureString} = %{TreasureResult}")]

User avatar
celestian
Dragon
Posts: 276
Joined: Mon May 17, 2010 3:29 pm

Re: dice in text string, easy way to roll?

Post by celestian »

GetRawProperties() allowed me to get it working with the {}'s. Took a bit of sorting out but got it working in the end.

Thanks for the tip!

I still might work out the regex someday tho as I can think of a lot of uses for it. I just remember it being a little difficult to deal with in macros more so than say my perl scripts ;)

Post Reply

Return to “MapTool”