I need help evaluating only part of a string!

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
Vintermute
Kobold
Posts: 2
Joined: Sun Sep 03, 2017 6:36 pm

I need help evaluating only part of a string!

Post by Vintermute »

Alright, first of all, I'll explain what you need to know about the Framework i've been building for D&D 5e.

Each Token with the Token Type "PC" or "NPC" has these properties:

Code: Select all

Dexterity:8
DEX:{floor(Dexterity / 2) - 5}
PB (Proficiency Bonus):2
Weapon0:wpnName=dagger; attackMod={DEX+PB}; critTarget=20; fumbRange=1; effect=;
To edit these properties, I made a library that has a macro called editValues, to modify most of the values a character would have in its sheet. The part that modifies the Weapon0 property is basically as follows:

Code: Select all

[H: input(

"tab1 | Weapons || TAB", 
    "Weapon0|
      wpnName="+getStrProp(Weapon0,"wpnName")+";
      attackMod="+getStrProp(Weapon0,"attackMod")+";
      critTarget="+getStrProp(Weapon0,"critTarget")+";
      FumbRange="+getStrProp(Weapon0,"fumbRange")+";
      effect="+getStrProp(Weapon0,"effect")+"
    ||PROPS|SPAN=TRUE SETVARS=UNSUFFIXED"
)]
This is so that every time the Input window is opened, the previous values are kept and are not always replaced by 0 every time the macro is run to its end.

Now, the problem:

The effect variable inside the Weapon0 property is supposed to be flexible, to allow outputs like these:
deal (1d8+DEX) piercing damage
deal (2d6) piercing damage, or (1d6) if swarm has half HP or lower.
Leaving any attack rolls aside, my objective is to get these strings to roll the dice within them, but allow them to be editable easily. I tried changing the parentheses with [brackets], and although this initially works (the damage has a different result each time, which means the rolling works correctly), once you try to open the Input window again, the previous values in the variable are kept (as intended) but replaced by their already evaluated version (as not intended). Using {curly braces} has the same problem, but the output isn't as messy.

Image
Editing the value for the first time.
Image
Editing the value a second time.

The final display is straightforward:

Code: Select all

[h: wpnName = getStrProp(Weapon0, "wpnName")]
[h: attackMod = getStrProp(Weapon0, "attackMod")]
[h: critTarget = getStrProp(Weapon0, "critTarget")]
[h: fumbRange = getStrProp(Weapon0, "fumbRange")]
[h: effect = getStrProp(Weapon0, "effect")]

[h: attackRoll1 = 1d20]
[h: attackRoll2 = 1d20]

<b>Attack</b>: [wpnName] (+[attackMod])<br>
<b>Effect</b>: [effect]

<!-- Check if attack roll is 1 or 20, do some colorful stuff on either result, output is basically: -->
...
[attackroll1 + attackMod] || [attackroll2 + attackMod]
I've struggled with this issue for almost a week, and I've come to the conclusion that my macro scripting skills aren't up to the task. So I'm hoping someone else can shed some light on this, I'd be very grateful.

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: I need help evaluating only part of a string!

Post by wolph42 »

I think you encountered a bug in strProp. I've informed the devs concerning this. A workaround in the mean time is this:

Code: Select all

[h:varsFromStrProp(Weapon0)]
[h:effect = decode(effect)]
[H:abort(input(
    "tab1 | Weapons || TAB", 
        "Weapon0|
          wpnName="+wpnName+";
          attackMod="+attackMod+";
          fumbRange="+fumbRange+";
          critTarget="+critTarget+";
          wpnName="+wpnName+";
        ||PROPS|SPAN=TRUE SETVARS=UNSUFFIXED",
        "effect|"+effect+"|give effect use brackets"
         
))]
[h:varsFromStrProp(Weapon0)]
[h:Weapon0 = setStrProp(Weapon0,"effect", encode(effect))]
[h: attackRoll1 = 1d20]
[h: attackRoll2 = 1d20]

<b>Attack</b>: [wpnName] (+[attackMod])<br>
<b>Effect</b>: [effect] 
in order to use 'effect' you first need to decode it:

Code: Select all

[r:decode(getStrProp(Weapon0,"effect"))] 
and in order to evaluate it you use Wiki: evalMacro():

Code: Select all

[r:evalMacro(decode(getStrProp(Weapon0,"effect")))] 
tested in 1.4.0.5

Vintermute
Kobold
Posts: 2
Joined: Sun Sep 03, 2017 6:36 pm

Re: I need help evaluating only part of a string!

Post by Vintermute »

Sorry for the late answer, but life got in the way. Fortunately, your solution works perfectly! Thank you a lot.
I've also modified it so that it can use up to four weapons, in case anyone wants to see.

Properties:

Code: Select all

Dexterity:8
DEX:{floor(Dexterity / 2) - 5}
PB (Proficiency Bonus):2
Weapon0:wpnName=longbow (150/600) ; attackMod=7 ; critTarget=20 ; fumbRange=1 ; effect=deal+%5B1d8%2BDEX%5D+piercing+damage. 
Weapon1:wpnName=rapier ; attackMod=5 ; critTarget=20 ; fumbRange=1 ; effect=deal+%5B1d8%2BDEX%5D+piercing+damage. 
Inside the editValues library macro:

Code: Select all

"tab1 | Weapons || TAB", 
        "Weapon0|
         wpnName="+getStrProp(Weapon0,"wpnName")+";
         attackMod="+getStrProp(Weapon0,"attackMod")+";
         critTarget="+getStrProp(Weapon0,"critTarget")+";
         fumbRange="+getStrProp(Weapon0,"fumbRange")+";
        ||PROPS|SPAN=TRUE SETVARS=UNSUFFIXED",

        "effect0|"+effect0+"|Effect for Weapon 0",

        "Weapon1|
         wpnName="+getStrProp(Weapon1,"wpnName")+";
         attackMod="+getStrProp(Weapon1,"attackMod")+";
         critTarget="+getStrProp(Weapon1,"critTarget")+";
         fumbRange="+getStrProp(Weapon1,"fumbRange")+";
        ||PROPS|SPAN=TRUE SETVARS=UNSUFFIXED",

        "effect1|"+effect1+"|Effect for Weapon 1", 
Inside the weaponAttack library macro

Code: Select all

[h:status = input("
       weaponID|
       "+getStrProp(Weapon0, "wpnName")+", "+getStrProp(Weapon1, "wpnName")+"|
       Choose weapon to attack with|
       LIST|
       SELECT=0 VALUE=NUMBER
")]

[if(weaponID == 0), CODE:
            {
                [h: wpnName = getStrProp(Weapon0, "wpnName")]
                [h: attackMod = getStrProp(Weapon0, "attackMod")]
                [h: critTarget = getStrProp(Weapon0, "critTarget")]
                [h: fumbRange = getStrProp(Weapon0, "fumbRange")]
                [h: effect = decode(getStrProp(Weapon0,"effect"))] 
            };
            {
                [h: wpnName = getStrProp(Weapon1, "wpnName")]
                [h: attackMod = getStrProp(Weapon1, "attackMod")]
                [h: critTarget = getStrProp(Weapon1, "critTarget")]
                [h: fumbRange = getStrProp(Weapon1, "fumbRange")]
                [h: effect = decode(getStrProp(Weapon1,"effect"))] 
             };]


[h: attackRoll1 = 1d20]
[h: attackRoll2 = 1d20]

<b>Attack</b>: [wpnName] (+[attackMod])<br>
<b>Effect</b>: [r:evalMacro(effect)]
<center><font size="4">[if(attackroll1 == 20),CODE:
    {
        <font color="green">[attackroll1 + attackMod]</font>
    };
    {
        [if(attackroll1 == 1), CODE:
            {
                <font color="maroon">[attackroll1 + attackMod]</font>
            };
            {
                <font color="black">[attackroll1 + attackMod]</font>
            };]
    };] || [if(attackroll2 == 20),CODE:
    {
        <font color="green">[attackroll2 + attackMod]</font>
    };
    {
        [if(attackroll2 == 1), CODE:
            {
                <font color="maroon">[attackroll2 + attackMod]</font>
            };
            {
                <font color="black">[attackroll2 + attackMod]</font>
            };]
    };] </font></center>
 

Post Reply

Return to “MapTool”