Skip to content

Item buttons on actor sheets unable to reference actor roll data #63

@asacolips

Description

@asacolips

While helping a user sort out roll formulas in the SWB discord server topic, I noticed that the buttons generated by items on an actor are unable to reference their parent actor's data for rolls. I'm not sure at what point in the process this is failing exactly, but the rough issue seems to be that their button formulas are written as @items.someItemAttrName and if someItemAttrName is a formula attribute referencing actor attributes, they're unable to drill deep enough in the roll data to work.

Steps to reproduce

  1. Create a new actor
  2. Create a Number attribute named foo with the value set to 1
  3. Create a Formula attribute named bar with the value set to @foo + 1
  4. Create an item on the actor
  5. Create a Formula attribute on the item named fooRoll with the value set to d20 + @foo
  6. Create a Formula attribute on the item named barRoll with the value set to d20 + @bar
  7. Click the buttons for each roll on the actor's items tab.

Expected

  • fooRoll should generate a roll with the formula d20 + 1
  • barRoll should generate a roll with the formula d20 + 1 + 1

Actual

  • fooRoll works as expected
  • barRoll causes a fatal error due to no prop being found for @foo in the @foo + 1 formula.
  • If rolling directly from the attributes tab of the items sheet, both formulas work as expected.

Workarounds

This appears to be happening due to either how getRollData() is returning for the actor, or possibly due to differences in how rolls are evaluated now versus when this feature was originally written for SWB. I was able to work around it by modifying SimpleActorSheet#_onItemRoll() to use the following logic:

/**
   * Listen for roll buttons on items.
   * @param {MouseEvent} event    The originating left click event
   */
  _onItemRoll(event) {
    let button = $(event.currentTarget);
    const li = button.parents(".item");
    const item = this.actor.items.get(li.data("itemId"));
    const actorRollData = this.actor.getRollData();
    const rollData = Object.assign(
      actorRollData,
      actorRollData.items[item.name.slugify({strict: true})],
    );
    const rollFormula = Roll.replaceFormulaData(button.data('roll'), rollData);
    let r = new Roll(rollFormula, rollData);
    return r.toMessage({
      user: game.user.id,
      speaker: ChatMessage.getSpeaker({ actor: this.actor }),
      flavor: `<h2>${item.name}</h2><h3>${button.text()}</h3>`
    });
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions