Help using checkboxes and collecting their 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
Jewish_Monk
Kobold
Posts: 3
Joined: Sun Apr 23, 2017 7:57 pm

Help using checkboxes and collecting their values

Post by Jewish_Monk »

Okay, hopefully this is a simple thing that I'm just not getting.

I'm attempting to make some macros for Shadowrun Anarchy. All that needs to happen is the players tick one box for a skill, one for an attribute, and the macro should retrieve their numbers, add them together, and roll xd6. Simple.

First, this is a bit of dummy code to hold the skill and attribute ranks until I can make it prettier.

Code: Select all

[H: countChecked = 0]
[H: dialogName = getName() + "'s Dice Pool" ]
[dialog(dialogName): {
<html>
  <head>
    <title>[R: dialogName]</title>
  </head>
  <body>
  <table>
    <tr>
      <td align="right">Strength</td>
      <td>
        <input type="checkbox" name="strength" value="0" />
      </td>
      <td align="right">Hacking</td>
      <td>
        <input type="checkbox" name="hacking" value="0" />
      </td>
    </tr>
    <tr>
      <td align="right">Agility</td>
      <td>
        <input type="checkbox" name="agility" value="0" />
      </td>
      <td align="right">Driving</td>
      <td>
        <input type="checkbox" name="driving" value="0" />
      </td>
    </tr>
    <tr>
      <td align="right">Willpower</td>
      <td>
        <input type="checkbox" name="willpower" value="0" />
      </td>
      <td align="right">Sorcery</td>
      <td>
        <input type="checkbox" name="sorcery" value="0" />
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center">
      <input tabindex="2" name="Button" type="submit" value="Roll" /> 
      <input tabindex="3" name="Button" type="submit" value="Cancel" /></td>
      <td align="right">Checked?</td>
      <td>
        [R: countChecked]
      </td>
      <td></td>
    </tr>
  </table>
  </body>
</html>
}]
That gives me a dialog box that looks like this:
Image

That's great, but I can't quite figure out how to detect if any checkboxes have been ticked. The wiki says that "The variable is assigned either 0 (unchecked) or 1 (checked)."

The Forms tutorial also mentions checkboxes. Says to "test if a box is checked by using json.contains on the field name."

So I tried a simple test to see if the Strength box was ticked.

Code: Select all

*snip*
  </table>
  </body>
</html>
}]

[H: countChecked = if(json.contains(strength, 1), countChecked + 1, countChecked)]
and the macro runs, but I get the message
Unknown JSON type "" in function "json.contains".
None of those parameters are strings. Doesn't make any sense. I get that this test doesn't check if the box has been unticked, but I'll worry about that later. I feel like if I could just determine which boxes are ticked, I could send that info to another macro to retrieve the property values and be done with it.

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

Re: Help using checkboxes and collecting their values

Post by wolph42 »

you've apparently mist a vital part while reading the tutorial: http://lmwcs.com/rptools/wiki/Forms_tutorial

Now lets make my little form work:

[h: processorLink = macroLinkText("processForm@Lib:token", "all")]
<form action="[r:processorLink]" method="json">
Character name: <input type="text" name="charName"><br>
Strength: <input type="text" name="str"><br>
<input type="submit" name="myForm_btn" value="Okay">
</form>
And create the processForm-macro.

<pre>
[r: json.indent(macro.args,2)]
</pre>
With this setup we can very easily find out how a specific form packs the data entered and how we could work with that. For this tutorial this processForm-macro will do.

The output we receive from this example is

{
"charName": "the fishy dude",
"str": "7",
"myForm_btn": "Okay"
}
Now its pretty easy to access the name and strength using json.get().
point is you require the

Code: Select all

[h: processorLink = macroLinkText("processForm@Lib:token", "all")]
<form action="[r:processorLink]" method="json">
line which calls the macro 'processForm' on the library token "lib:token" (which you can change to any other token) in processForm you can check the json with:

Code: Select all

[r: json.indent(macro.args,2)]

Jewish_Monk
Kobold
Posts: 3
Joined: Sun Apr 23, 2017 7:57 pm

Re: Help using checkboxes and collecting their values

Post by Jewish_Monk »

wolph42 wrote:you've apparently mist a vital part while reading the tutorial: http://lmwcs.com/rptools/wiki/Forms_tutorial
You are correct. That's exactly the thing I needed to get back on track. It took a bit of swearing and drinking, but I finally got a working tool.

Image


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

Re: Help using checkboxes and collecting their values

Post by Full Bleed »

Jewish_Monk wrote: You are correct. That's exactly the thing I needed to get back on track. It took a bit of swearing and drinking, but I finally got a working tool.
You should post the working code for others to scavenge.
Maptool is the Millennium Falcon of VTT's -- "She may not look like much, but she's got it where it counts."

Jewish_Monk
Kobold
Posts: 3
Joined: Sun Apr 23, 2017 7:57 pm

Re: Help using checkboxes and collecting their values

Post by Jewish_Monk »

Well, if you insist. It's not perfect and it's certainly not pretty, but it works.

Be Aware:
  • - You'll need a token named Lib:Anarchy to hold the "rollTwoStats" macro
    - A custom set of properties should be set for each character (Strength, Agility, Hacking, etc). This is a side effect of Anarchy being so free-form with its skills. Every character could have 4-6 skills unique to them.
    - Each individual character will need to have the HTML portion of the "Dice Pool" macro tailored to their individual skills. I would have had this generate automatically, but I called it quits at 4am. That may be the next step for me.
    - If the token is not actively selected when the Roll button is pushed, there will be a 0 dice pool. Trying to figure out a way around this
    - The dialog does not actually enforce only two checkboxes. I considered implementing that, but I think it would be better to output the player's choices and police things manually.
"Dice Pool" macro - add to token

Code: Select all

[h: processorLink = macroLinkText("rollTwoStats@Lib:Anarchy", "all")]
[h: id = getSelected()]

[H: dialogName = getName() + "'s Dice Pool" ]
[frame(dialogName): {
<html>
  <head>
    <title>[R: dialogName]</title>
    <link rel="stylesheet" type="text/css" href="css@Lib:Anarchy"></link>
  </head>
  <body>
  <form action="[R: processorLink]" method="json">
  <table>
    <th colspan = 4>Please select two</th>
    <tr class="odd">
      <td align="right">Strength: [r: getProperty("Strength", id)]</td>
      <td>
        <input type="checkbox" name="strength" value="1" />
      </td>
      <td align="right">Escape Artist: [r: getProperty("Escape Artist", id)]</td>
      <td>
        <input type="checkbox" name="escape artist" value="1" />
      </td>
    </tr>
    <tr class="even">
      <td align="right">Agility: [r: getProperty("Agility", id)]</td>
      <td>
        <input type="checkbox" name="agility" value="1" />
      </td>
      <td align="right">Sorcery: [r: getProperty("Sorcery", id)]</td>
      <td>
        <input type="checkbox" name="sorcery" value="1" />
      </td>
    </tr>
    <tr class="odd">
      <td align="right">Willpower: [r: getProperty("Willpower", id)]</td>
      <td>
        <input type="checkbox" name="willpower" value="1" />
      </td>
      <td align="right">Astral Combat: [r: getProperty("Astral Combat", id)]</td>
      <td>
        <input type="checkbox" name="astral combat" value="1" />
      </td>
    </tr>
    <tr class="even">
      <td align="right">Logic: [r: getProperty("Logic", id)]</td>
      <td>
        <input type="checkbox" name="logic" value="1" />
      </td>
      <td align="right">Survival: [r: getProperty("Survival", id)]</td>
      <td>
        <input type="checkbox" name="survival" value="1" />
      </td>
    </tr>
    <tr class="odd">
      <td align="right">Charisma: [r: getProperty("Charisma", id)]</td>
      <td>
        <input type="checkbox" name="charisma" value="1" />
      </td>
      <td align="right">Engineering: [r: getProperty("Engineering", id)]</td>
      <td>
        <input type="checkbox" name="engineering" value="1" />
      </td>
    </tr>
    <tr>
      <td align="right" class="even">Edge: [r: getProperty("Edge", id)]</td>
      <td class="even">
        <input type="checkbox" name="edge" value="1" />
      </td>
      <td align="right"></td>
      <td></td>
    </tr>
    <tr>
      <td colspan = 2 align = right class="odd">x2<input type="checkbox" name="xx" value="1" />
      x3<input type="checkbox" name="xxx" value="1" /></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td colspan="4" align="center">
      <input tabindex="2" name="Button" type="submit" value="      Roll!      " /> 
      </td>
    </tr>
  </table>
  </form>
  </body>
</html>
}]
"rollTwoStats" macro - add to Lib:Anarchy

Code: Select all

<!-- Shadowrun UltraAnarchy macro -->
<!-- Initialize variables -->
[h: statBlock = json.indent(macro.args,2)]
[h: id = getSelected()]
[h: dicePool = 0]

[r, foreach(var, statBlock, ""), code: {
  [h: newDie = getProperty(var, id)]

  [r, if(var != "Button" && var != "xx" && var != "xxx"), code:{
    [h: dicePool = dicePool + newDie]
  };{}] <!--IF-->

}] <!--FOREACH-->

[r, foreach(var, statBlock, ""), code: {

  [r, if(var == "xx"), code:{
    [h: dicePool = dicePool * 2]
  };{}] <!--IF-->

}] <!--FOREACH-->

[r, foreach(var, statBlock, ""), code: {

  [r, if(var == "xxx"), code:{
    [h: dicePool = dicePool * 3]
  };{}] <!--IF-->

}] <!--FOREACH-->

[r: "<br>Dice Pool = "][r: dicePool]
[r: sr4(dicePool)]

Post Reply

Return to “Macros”