Skip to content

Foundry V13 - various Marco fixes inside #64

Description

@warwon

Not sure how to send this in but anyways,

V13 code for the hex marco

//Generate Hex - Corrected for Foundry V13

const version = '1.0';
// NOTE: Change 'Tables' below to the exact title of your Compendium pack.
const compendium_label = 'Tables';

(async () => {

let hexTypeList = ['Random', 'Countryside', 'Forest', 'River', 'Human Town'];
let selectList = "";

hexTypeList.forEach(option => selectList += "<option value='" + option + "'>" + option + "</option>")

let d = new Dialog({
    title: "Select Type",
    content: "<h2> Select or Roll Hex Type </h2> <select style='margin-bottom:10px;'name='stat' id='stat'> " + selectList + "</select> <br/>",
    buttons: {
        roll: {
            icon: '<i class="fas fa-check"></i>',
            label: "Roll",
            callback: (html) => hexType(html.find('[id=\"stat\"]')[0].value)
        },
        cancel: {
            icon: '<i class="fas fa-times"></i>',
            label: "Cancel",
            callback: () => { }
        }
    },
    default: "roll",
    close: () => { }
});
d.render(true);

})()

async function hexType(hexType) {
let hexContents = "";
let hexDetails = "";
let table = null;
let roll = null;
let randomHex = ['Countryside', 'Forest', 'River', 'Human Town'];

if(hexType == "Random"){
    hexType = randomHex[Math.floor(Math.random() * randomHex.length)];
}

// --- First Table Roll ---
table = await drawFromTable("Hex - " + hexType);
if (!table) return; // Exit if table not found

roll = await table.roll(); // Roll the table (no await on roll() needed typically, but keeping it async-safe)
hexContents = roll.results[0].text; // CORRECTED: Access text directly

// --- Second Table Roll ---
table = await drawFromTable("Hex - Landmark Details");
if (!table) return; // Exit if table not found

roll = await table.roll();
hexDetails = roll.results[0].text; // CORRECTED: Access text directly

let message = "\
<h2> " + hexType + ": </h2>\
<b> Landmark: </b>" + hexContents + "</br>\
<b> Details: </b><i>" + hexDetails + "</i></br>";

let chatData = {
    content: message,
    whisper : ChatMessage.getWhisperRecipients("GM")
};  

ChatMessage.create(chatData);

}

/* Functions */
async function drawFromTable(tableName) {
// CORRECTED: Find the compendium pack by name (label) and document type
const pack = game.packs.filter(p => p.documentName === 'RollTable').find(p => p.metadata.label === compendium_label);

if (!pack) {
    ui.notifications.warn(`Compendium pack with label ${compendium_label} not found.`, {});
    return;
}

// CORRECTED: Use getDocuments() to get the content and filter by name
const inside = await pack.getDocuments(); 
const table = inside.find(p => p.name === tableName);

if (!table) {
    ui.notifications.warn(`Table ${tableName} not found in pack ${compendium_label}.`, {});
    return;
}
return table;

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions