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

[Question] Check for none existence of var

January 23 (11 years ago)

Edited January 23 (11 years ago)
DXWarlock
Sheet Author
API Scripter
I got an easy question hopefully someone can help out with.

Im working on doing catching rolls from chat, such as [[1d20+10]], which works fine.
My problem is how to check to see if part of that roll is 'undefined' before processing that part..to stop Cannot Read errors.

For example I got a part called
var mod = (r.results.rolls[1].expr);
which returns the +10 to display their modifier..works great.

problem is if they roll [[1d20]] I get a 'Cannot read property 'expr' as undefined.'


How do I check if it exists, and if none make var mod = '0', Anything I tried of doing 'if == undefined', 'if == null' ect.. brings me back to it saying it doesnt know what it is, to check it in the first place.


January 23 (11 years ago)
if (variable != undefined) {

}

works for me.
January 23 (11 years ago)

Edited January 23 (11 years ago)
DXWarlock
Sheet Author
API Scripter
I tried that I had:

if (r.results.rolls[1].expr != undefined) {
..stuff to do
}
else {
..other stuff to do
}
but still get TypeError: Cannot read property 'expr' of undefined

even tried just a straight:
if (r.results.rolls[1].expr == undefined) return;
to cancel even trying to run the script after that

same result, like it needs to know what that is, before it can tell me if it's undefined.
January 23 (11 years ago)
Drop the .expr. This works for me. You don't need to go further into the array by putting .expr onto [1]

if (slice[2] != undefined){
if (slice[2].toLowerCase() == "augment") {
augmented = 1;
}}
January 23 (11 years ago)

Edited January 23 (11 years ago)
DXWarlock
Sheet Author
API Scripter
Not sure what you mean,
I need the expr its being returned from the msg.inlinerolls. as such:
{"expression":"1d20+0","results":{"resultType":"sum","rolls":[{"dice":1,"results":[{"v":6}],"sides":20,"type":"R"},{"expr":"+2","type":"M"}],"total":6,"type":"V"}}

and I need to see if "expr":"+2" exists first, so I don't get the error, and if its doesn't make it 0.

maybe I'm being dense on what you are suggesting and not understanding.
January 23 (11 years ago)

Edited January 23 (11 years ago)
You're calling to check if a particular part of an array is undefined, specifically rolls[1].

Unless there's something I'm not aware of that's specific to inline rolls, "rolls" is an array consisting of:

rolls[0] being a sub-array of ...


Oh...


Try rolls[1][0] == undefined
or rolls[1].expr[0] == undefined

Damned sub arrays. I think one of those may work.
January 23 (11 years ago)

Edited January 23 (11 years ago)
DXWarlock
Sheet Author
API Scripter
nadda
both:
if (r.results.rolls[1].expr[0] == undefined) return;
and
if (r.results.rolls[1][0] == undefined) return;

throw undefined errors.

AH, while typing this I had a lightbulb on what you said, you was right taking expr off the check and do
if (r.results.rolls[1] == undefined) return;
seems to work, I was stuck on thinking you mean take it out of what the modifier equals.

so this does it right now, Ive been fighting it for hours and stuck in a pigeonhole on what I was looking at, not realizing what it was..lol.
Thanks for helping me out of the rut I I was stuck in for what I was trying to do.
if (r.results.rolls[1] == undefined) {
    mod1 = 0;
} else {
    mod1 = (r.results.rolls[1].expr);
}

January 23 (11 years ago)
Now, since you're working on it. See if your script can pick up 1d20+12+5. Or any arbitrary number of modifiers on the d20. :P
January 23 (11 years ago)
DXWarlock
Sheet Author
API Scripter
It only took me 12 hours to do that, Ill whip that right up for you..lol
January 23 (11 years ago)
That is probably going to be where the subarray comes into play, but don't quote me on that. And have fun!
January 23 (11 years ago)
DXWarlock
Sheet Author
API Scripter
For now they can total the bonuses in their head and make it one number, until I get into that cobweb of stuff :)
January 23 (11 years ago)
Riley D.
Roll20 Team
A very common pattern in Javascript is to do something like this:

if(r.results.rolls && r.results.rolls.length > 0 && r.results.rolls[0].expr) {
//Do something with r.results.rolls[0].expr here
}

The way Javascript works is that it will "bail out" the first time it reaches a "false" statement (from left to right). So if you get to "r.results.rolls" and that is undefined, it will just skip the rest and not try to do anything. So you can string together several checks that go "down" through the structure right in a row to make sure you know what you're dealing with and avoid any undefined errors.
January 23 (11 years ago)

Edited January 23 (11 years ago)
DXWarlock
Sheet Author
API Scripter
Ah thanks Riley, the .length would help me too. Was driving me mad with it erroring and saying it was undefined..and I was thinking "I know it is, stop complaining its undefined, so you cant check if its undefined!..":)