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

Having logging issues TypeError: Cannot convert object to primitive value

November 04 (6 years ago)

Edited November 04 (6 years ago)

Let me preface with this that I am new to the API, javascript, and web development in general. (My background is in python and java for scientific and engineering purposes) I am trying to implement a system for a DM friend (DoubleCross if anyone is curious). I am starting by getting the basic dice mechanic functions. However as I work I have been getting issues trying to just spit out my function returns so I can just check that the basic math is falling through before I start working on implementing other parts of the system. Here's my code:

https://gist.github.com/lord-xaphan/9fc2674372a7143631e97d988b987a95

If I run this I get or similar if I try to log my function outputs:

TypeError: Cannot convert object to primitive value at diceAction (apiscript.js:26:32) at apiscript.js:2:23 at eval (eval at <anonymous> (/home/node/d20-api-server/api.js:151:1), <anonymous>:65:16) at Object.publish (eval at <anonymous> (/home/node/d20-api-server/api.js:151:1), <anonymous>:70:8) at checkForReady (/home/node/d20-api-server/api.js:1438:12) at /home/node/d20-api-server/api.js:1518:9 at c (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:14:64) at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:560 at hc (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:39:147) at Kd (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:546)

It seems to come up when I am trying to log but is inconsistent as it seems to work sometimes. I am not sure as to what I am doing wrong. Sorry if it is something obvious or easily Googlable.



November 04 (6 years ago)
GiGs
Pro
Sheet Author
API Scripter

The first thing i notice in your code is that you dont end any lines with semi-colons, so there's likely syntax issues. 

I'd run your code through an online validator like this one https://validatejavascript.com/

and see if the adjusted code works. Also check out the errors it provides. Some can be ignored, others might be informative.

November 04 (6 years ago)

Edited November 04 (6 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator

GG's suggestion is good, but I think your problem is actually a function call issue. The error gives you where the problem is (Bolded/Italicized below):

TypeError: Cannot convert object to primitive value at diceAction (apiscript.js:26:32) at apiscript.js:2:23 at eval (eval at <anonymous> (/home/node/d20-api-server/api.js:151:1), <anonymous>:65:16) at Object.publish (eval at <anonymous> (/home/node/d20-api-server/api.js:151:1), <anonymous>:70:8) at checkForReady (/home/node/d20-api-server/api.js:1438:12) at /home/node/d20-api-server/api.js:1518:9 at c (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:14:64) at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:560 at hc (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:39:147) at Kd (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:546)

Your problem is that in your diceAction function, you attempt to call the randomInteger function, but you don't call it as a function:

if (damageFlag){
    var damageDice = Math.floor(actionTotal/10 %10)
    damageResult = 0
    var i
    for (i=0; i < damageDice; i++){
        damageResult += randomInteger //The functions (even ones that don't take arguments) need to be called as functions. This should be randomInteger()
    }
}

Additionally, if you are trying to replicate the VTT's dice rolling, I would recommend against randomInteger(). The wiki is not as clear as it could be on this matter, but the fact is that randomInteger (and Math.random()) in the Roll20 environment use the back up RNG rather than quantumRoll. I'd recommend switching to an async/await and/or promise pattern and using sendChat() to actually roll the dice to get your random numbers. There will be a slight speed delay as the script will have to wait for an asynchronous operation, but it will actually replicate the results you would get rolling using the chat commands and the speed delay won't be any worse than a normal dice roll.

NOTE: While I think that your error is actually caused by the above, I would strongly suggest following GG's suggestion as well. In addition I would also strongly suggest putting your script into strict mode by adding the following to the first line after your on('ready'):

'use strict';
November 04 (6 years ago)
GiGs
Pro
Sheet Author
API Scripter

That does seem likely to be the cause, well spotted, Scott.

I think the advice on abandoning randomInteger might be overkill though. Yes, it's less random than the quantum engine, and for roll20, who have to deal with millions of dice rolls, it's good to have the most robust random generator they can have. But for any individual gm, using a script for a handful of games, it's really not important to be as random as humanly possible. Dice at a gaming table are often not as random as they appear, they have manufacturing issues and wear-and-tear flaws, but they are good enough for gaming nights. Likewise randomInteger is fine for a few hundred gaming nights.

So, it's really a trade-off. If you are willing to give up the ease of using randomInteger and go for a more complex programming solution, more power to you. But if it' feels like a lot of trouble, it'll be fine to stick with randomInteger.

November 04 (6 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator

True, it probably is overkill, but if this script is being developed for use by many, then using the same dice engine that would be used for normal dice rolls would probably be preferred so that the user experience is the same.

"If you are happy and know it, syntax error." That seems to have worked. Thanks for the suggestions. I'll have to wrestle with async functions and the dice roller's underlying structure as it looks like that might simplify a couple things.