Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Cycle Through PCs to Send Dreams

I'm trying to create a script that will cycle through the PCs and whisper an item from a table to them. This is what I have pieced together from bits in the forum that is supposed to identified the character names of the selected tokens, but I get a function not defined error. // ==ClosureCompiler== // @output_file_name default.js // @compilation_level SIMPLE_OPTIMIZATIONS // @formatting pretty_print // ==/ClosureCompiler== /** * Generates Nightmares for WWC * * Syntax: !dreams option * * option TBD */ var dreams = dreams || { output: [], listen: function () { on('chat:message', function (msg) { // Exit if not an api command if (msg.type != "api") { return; } // if (msg.content.indexOf("!dreams ") != -1) { // var input = msg.content.split(" "); // if (input[1] == "help") { // dreams.showHelp(); // } else { dreams.generate(msg, dreams.printSheet); // } // } else if (msg.content.indexOf("!dreams") != -1) { // dreams.showHelp(); // } }); }, // showHelp: function () { // sendChat("API", "/direct <table style='background: #DCD9D5; border-radius: 20px; font-size: 10px;'>" + // "<thead><tr><th>Help</th></tr></thead>" + // "<tbody>" + // "<tr><td><strong>!dreams</strong><br><strong>!dreams help</strong><br>Show this help screen.</td></tr>" + // "<tr><td> </td></tr>" + // "</td></tr></tbody></table>"); // }, getNamesFromTokens: function (msg, outputCallback, saveCallback) { return (selected || []).map(obj => getObj("graphic", obj._id)) .filter(x => !!x) .map(token => token.get("represents")) .filter(name => getObj("character", name || "")); }, generate: function (msg, outputCallback, saveCallback) { dreams.id = msg.playerid; dreams.player = msg.who; let charid = getNamesFromTokens(option)[0]; char = findObjs({ _type: 'character', _id: charid })[0]; // dreams.name = msg.who + " #" + (findObjs({ // type: "character", // controlledby: msg.playerid // }).length + 1) dreams.rollDream(); if (typeof outputCallback === "function") { setTimeout(outputCallback, 2500, msg, saveCallback); } }, printSheet: function (msg, saveCallback) { var styleLabel = "style='font-weight: bold; padding: 5px;'"; var styleVal = "style='padding: 5px;'"; if (typeof dreams.Type != 'undefined') { dreams.rollNPC(); } dreams.output['Title'] = "<thead><tr><th colspan='2' style='background: #8C8173; padding: 5px;'>" + dreams.Title + "</th></tr></thead>"; dreams.output['Text'] = "<tr><td style='padding: 5px;'>" + dreams.Text + "</td></tr>"; sendChat(msg.who, "/w gm <table style='background: #DCD9D5; border-radius: 20px; font-size: 10px;'>" + dreams.output['name'] + "<tbody>" + dreams.output['Title'] + dreams.output['Text'] + "</tbody></table>"); if (typeof saveCallback === "function") { saveCallback(); } } } on("ready", function () { dreams.listen(); }); Any suggestions?
1694522833

Edited 1694532017
timmaugh
Pro
API Scripter
Hey, Joab... I think you're getting that error because you have references to functions that don't exist. For instance, your dreams.listen() function calls dreams.generate(), which calls dreams.rollDream()... which... doesn't exist. Similarly your printSheet() function (which you currently don't have anything calling, so, OK for now) would want to call dreams.rollNPC()... which also doesn't exist. I'm seeing other issues, though, Joab. You assign data to properties right on your dreams object, and those are going to stick around from chat message to chat message; in fact, if you get multiple chat messages in close succession, they can step on each other writing those properties. I think you'd benefit from giving the main body of your script a closure that will let you write data to that space, then generating a new data object for each message your script would handle. If you don't need it to stick around after the initial message has been processed (that is, you'll never need to refer to it again after the first message), you can just create it in your chat handler and pass it along to the other functions. When the code finishes running the object will go out of scope automatically. If you do need to refer to it again later (after the initial message), you can create it in the body of your script closure, store it, and then perform cleanup yourself after it is no longer needed. If you need help with setting either of those up, I can show an example. Lastly, if you implement Aaron's API_Meta trick , you can disambiguate the line numbers involved in the error and narrow down where the problem is more quickly. The trick is discussed again in the ScriptInfo thread. ScriptInfo is a script that reads all of the scripts that use the API_Meta trick and quickly does the math for you to disambiguate the line number. For instance, when  you get an error like the "function not defined" you mention, it will come with a line number... maybe line 10254. You plug that into ScriptInfo like this: !scriptinfo 10254 ...and you get a report that line 10254 corresponds to line 51 in Dreams... which is the dreams.rollDream() function call. I keep a button in my development game for just this case, with the command line reading: !scriptinfo ?{Line number of error} ...that way I can quickly run it and input the number of the error I'm trying to track down.