Advertisement Create a free account

Attempting to class script following template, and failing

1560337275

Edited 1560338824
Aku
Pro
Hello everyone, I found GiGs template for creating a class, and have tried to follow it, but after filling in my script, i get an error "unexpected token , " Reading through some JS info, it doesn't really look the like comma needs to be used here, but if i remove them, I start getting a cascading list of other errors as i whack-a-character. Edit: the follow up error, after removing the commas is "Unexpected token )" Edit2: got it to compile after moving some brackets around, but now is not responding to the chat input Edit3: attempted to embed, and failed. Corrected back to a regular link The code I have so far is : <a href="https://gist.github.com/Dherman3607/bf2d8f47069db5a54e19f828fc41299b" rel="nofollow">https://gist.github.com/Dherman3607/bf2d8f47069db5a54e19f828fc41299b</a>
1560349094

Edited 1560349397
GiGs
Pro
Sheet Author
Wow, people are actually using that post. Great :) There are a couple of issues. I should probably post an updated version of that template using simpler syntax, but as it is you are missing a bunch of brackets in various places. Also you have no semi-colons to end lines anywhere, but that doesnt appear to be causing the problem. I havent checked that the code does what you want it to, but here's the corrected syntax for the format var DetermineEncounter = DetermineEncounter || (function () { // note the open bracket before the word function here 'use strict'; var version = '1.0', handleInput = function (msg) { log(msg); //rolling and sending the encounter table to the dm, eventually? if (msg.content.split(' ')[0] == "!encounter") { var dangerLevel = msg.content.split(" ")[1]; log('Danger level is ' + dangerLevel); var DieRolls = []; var i; var outMessage = ""; for (i = 0; i &lt; 6; i++) { DieRolls += randomInteger(6); } CompareToTarget(dangerLevel, DieRolls); // also you have a function call with no function - i added an empty function below } // THIS WAS THE MAIN ERROR: you didnt close the if statement }, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CompareToTarget = function(dangerLevel, DieRolls) { }, DetermineEncounter = function (dangerLevel, DieRolls) { for (var i = 0; i &lt; DieRolls.length; i++) { log('Die roll is ' + DieRolls[i]) switch (i) { case 0: Time = 'Morning' if (DieRolls[i] &lt;= dangerLevel) { encounterType = DetermineEncounter() } else { encounterType = 'No encounter' } log(encounterType) } //log(DieRolls[i]) // if(DieRolls[i] &lt;= dangerLevel){ //log('there should be an encounter here') } }, // make sure every function before the final one before RETURN ends with }, - that comma is vital. registerEventHandlers = function () { on('chat:message', handleInput); // if you have any other on events, you can enter that here and create a function above, like handleInput, to intercept it. }; // this is the last fucntion before the return, so it gets a semi-colon to end it. return { RegisterEventHandlers: registerEventHandlers }; }()); // note the weird number of brackets here, you have to get this right. on("ready", function () { 'use strict'; DetermineEncounter.RegisterEventHandlers(); }); // also you'd got the wrong brackets ending this section too. The purpose of this template is so you can use the outer structure without changes, you just need to create inner functions.&nbsp;
1560349562
GiGs
Pro
Sheet Author
I suspect that compareToTarget probably isnt going to work exactly how you want it to. If you want it to return a value, you need a variable to store the return in: var comparison = CompareToTarget(dangerLevel, DieRolls); And then in the function, you perform our calculation, and have&nbsp; final line which has return result; // with result being the variable you assigned the calculation result to.
1560349620
Aku
Pro
Thanks GiGs,Just tested it, and it got me back to where i was, thanks. I guess I should've (re)started with your template in the first place, instead of trying to copy and paste into it, I think that's where i introduced the error to begin with.
1560351486
GiGs
Pro
Sheet Author
You're welcome. I noticed another issue: DieRolls += randomInteger(6); This should be DieRolls.push( randomInteger(6)); With an array, you usually add extra items to it using the push command.&nbsp; I'd also suggest (not essential, but its better), if you are using the same value multiple times, like this: msg.content.split(' ') put it in a variable. Like so var args = msg.content.split(' '); if (args[0] == "!encounter") { Also, you want to protect against accidentally typing multiple spaces. You can do the split with a regular expression like this: var args = msg.content.split(/\s/); It looks confusing, but you dont have to understand it to use it. It will treat any number of spaces as a single unit, so if you type "!encounter&nbsp; &nbsp; &nbsp;oops" all those extra spaces wont cause an issue.
1560352366
Aku
Pro
Thanks for all the replies. I havent entirely decided how I'm going to use CompareToTarget(). My first thought was to just chain to the next function, that's going to actually format a sendChat to the GM with the results, but that's going to turn into spaghetti I think. So you're probably right, I'll probably end up returning something and then moving on to the next function. I've got so many partial languages floating in my head I can't keep things straight as to best practices (ala array += vs array.push() ). As far as the chat parser, I appreciate the tip but I THINK in the end I'm going to turn this into a macro listener instead of a chat (just so players don't see the danger level of the zone they're going to/in)