Help Needed! Dynamic Table for Reputation Tracking

Hello, everyone. I am still encountering an issue with my Reputation Tracking system. In the actor sheet, there is a tab to track the character’s reputation with other characters using a table. Each row in the table represents a relationship. The row has a column for an input text field (to store the character’s name) and a second column of radio buttons which represent a numerical scale of the relationship level. There should be a button to add a new row, which creates a new object in the array of relationships this character has. Each row will also have a third column with a delete button that not only removes the row, but deletes the object from the array. For some reason, this isn't working as intended (see attached video for example of problem and console warnings). Rows and new objects get added, but information for only 1 object will save. When trying to add a new object after inputting info, the array will reset itself. Is there anyone out there who can help me identify my problem and guide me towards correction? Thank you! from the template.json:
{
"Actor": {
"types": ["character", "monster", "npc"],
...
"npc": {
"templates": ["base"],
"attributes": {
"level": {
"value": 1
},
"npcType": {
"value": ""
},
"reputation": {
"entries": [
{
"characterName": "Character Name",
"value": 0
}
]
}
}
}
}
}
{
"Actor": {
"types": ["character", "monster", "npc"],
...
"npc": {
"templates": ["base"],
"attributes": {
"level": {
"value": 1
},
"npcType": {
"value": ""
},
"reputation": {
"entries": [
{
"characterName": "Character Name",
"value": 0
}
]
}
}
}
}
}
from getData():
// Character Reputation Tracker
console.log("Initial reputation entries:", actorData.system.attributes.reputation.entries);
context.reputationValueOptions = {
0: -5,
1: -4,
2: -3,
3: -2,
4: -1,
5: 0,
6: 1,
7: 2,
8: 3,
9: 4,
10: 5
};

context.reputationEntries = actorData.system.attributes.reputation.entries || [];
console.log("Reputation Value Options:", context.reputationValueOptions);
console.log("Reputation Entries:", context.reputationEntries);
}
// Character Reputation Tracker
console.log("Initial reputation entries:", actorData.system.attributes.reputation.entries);
context.reputationValueOptions = {
0: -5,
1: -4,
2: -3,
3: -2,
4: -1,
5: 0,
6: 1,
7: 2,
8: 3,
9: 4,
10: 5
};

context.reputationEntries = actorData.system.attributes.reputation.entries || [];
console.log("Reputation Value Options:", context.reputationValueOptions);
console.log("Reputation Entries:", context.reputationEntries);
}
from activeListeners():
// Bind the add reputation row function
html.find('.add-reputation').click(this._onAddReputationRow.bind(this));

// Listener for reputation name input changes
html.find('.reputation-name-input').change(event => this._onReputationNameChange(event));

// Listener for reputation value radio button changes
html.find('.reputation-value-radio').change(event => this._onReputationValueChange(event));
// Bind the add reputation row function
html.find('.add-reputation').click(this._onAddReputationRow.bind(this));

// Listener for reputation name input changes
html.find('.reputation-name-input').change(event => this._onReputationNameChange(event));

// Listener for reputation value radio button changes
html.find('.reputation-value-radio').change(event => this._onReputationValueChange(event));
async functions in actor-sheet.mjs:
async _onReputationNameChange(event) {
const inputElement = $(event.currentTarget);
const entryIndex = inputElement.data('entry-index'); // assuming each row has a data attribute like data-entry-index
const newName = inputElement.val();

let updateData = {};
updateData[`system.attributes.reputation.entries.${entryIndex}.characterName`] = newName;

await this.actor.update(updateData);
}

async _onReputationValueChange(event) {
const radioButton = $(event.currentTarget);
const entryIndex = radioButton.data('entry-index');
const newValue = radioButton.val();

let updateData = {};
updateData[`system.attributes.reputation.entries.${entryIndex}.value`] = newValue;

await this.actor.update(updateData);
}

async _onAddReputationRow(event) {
event.preventDefault();

// Retrieve current entries and check if it's an array
let currentEntries = this.actor.system.attributes.reputation.entries;
if (!Array.isArray(currentEntries)) {
console.warn("currentEntries is not an array. Initializing as an empty array.");
currentEntries = [];
}

// Add a new entry
let newEntry = { characterName: 'New Character', value: 5 };
currentEntries.push(newEntry);

// Update the actor
try {
await this.actor.update({ 'system.attributes.reputation.entries': currentEntries });
this.render(false);
} catch (e) {
console.error("Error updating actor:", e);
}
}
async _onReputationNameChange(event) {
const inputElement = $(event.currentTarget);
const entryIndex = inputElement.data('entry-index'); // assuming each row has a data attribute like data-entry-index
const newName = inputElement.val();

let updateData = {};
updateData[`system.attributes.reputation.entries.${entryIndex}.characterName`] = newName;

await this.actor.update(updateData);
}

async _onReputationValueChange(event) {
const radioButton = $(event.currentTarget);
const entryIndex = radioButton.data('entry-index');
const newValue = radioButton.val();

let updateData = {};
updateData[`system.attributes.reputation.entries.${entryIndex}.value`] = newValue;

await this.actor.update(updateData);
}

async _onAddReputationRow(event) {
event.preventDefault();

// Retrieve current entries and check if it's an array
let currentEntries = this.actor.system.attributes.reputation.entries;
if (!Array.isArray(currentEntries)) {
console.warn("currentEntries is not an array. Initializing as an empty array.");
currentEntries = [];
}

// Add a new entry
let newEntry = { characterName: 'New Character', value: 5 };
currentEntries.push(newEntry);

// Update the actor
try {
await this.actor.update({ 'system.attributes.reputation.entries': currentEntries });
this.render(false);
} catch (e) {
console.error("Error updating actor:", e);
}
}
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server