klaas1979 / tablesmith-like

Use Tablesmith-like tables in FoundryVTT and create complex nested tables.
MIT License
3 stars 5 forks source link

Capturing output in a variable in a macro #19

Closed Lyinggod closed 1 year ago

Lyinggod commented 1 year ago

The ability to capture table output in a variable within a macro

Possible output Let output = game.modules.get('tablesmith-like').api.evaluateTable(groupCall, options)

Describe alternatives you've considered I have considered creating temporary chat or journals but the temporary output would then need to be deleted Both options require timers or a method to check creation of table output before the output can be retrieved.

The usage that spawned this my desire to create items based on random output where the output is captured, processed, and then turned into an item or NPC.

klaas1979 commented 1 year ago

The result is returned, as of now the result object is quite complicated and async. You can get the result by using code like this:

let result = await game.modules.get('tablesmith-like').api.evaluateTable(groupCall, options)
console.log('tablesmith', result.asString())

The result can consist of many resuls, as a single call can be used to generate multiple roles an a table. The result objects contains the call and context, so the results are nested within the object.

The asString() method is provided to get the results all merged into a single string. For your purpose this should work. Could you try?

klaas1979 commented 1 year ago

This is the complete example that should provide you with a result to use in a macro.

// change the Call to the correct Table and name
// Table name without the '.tab' extension
let table = 'test';
let group = 'Start'; // default group for Tables
let parameter = ''; // comma separated list of parameters, leave empty for most tables
let rollCount = '1';
// options for displaying results in chat or ading to journal
let options = {
  toChat: false, // print result to chat
  toJournal: false, // add result to a journal entry
  lenient: false, // try to fix some common errors in calls
  journal: undefined, // leave undefined for using Journal in settings or use:
  // journal: { folder: 'Tablesmith', name: 'Roll Results' },
  journalOptions: { includeTimestamp: true, notify: true },
};
// do not change below this line
if (parameter && parameter.length > 0) parameter = `(${parameter})`
let groupCall = `[${table}.${group}${parameter}]:${rollCount}`;
let result = await game.modules.get('tablesmith-like').api.evaluateTable(groupCall, options)
console.log('tablesmith', result.asString())