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

[HELP] with arrays and indexof

1391835444
Konrad J.
Pro
API Scripter
I need a bit of guidance. Just missing something here. Here's what I want to do. Its not everything just a code snippet to show the problem I have. I want to create an array of GMIDS inside of my fwCONSTANTS variable so that later I can use indexof on the GMIDS to test if an ID exists in the array. var fwCONSTANTS = { FWCOMMAND : "!fw", GMIDS : ["A", "B", "C"] // this is what I have wrong, but I just can't figure out how to do an array here? I could just create GMID1, GMID2, etc., but I wanted // to do it as an array }; . . if (fwCONSTANTS.GMIDS.indexof(playerid) !== -1 { . . } . . Please be gentle, I'm not an expert although I can get some good stuff done, but I'm blinded by something here that you experts will know right away! :) Thanks for your help, Konrad
1391845672
Lithl
Pro
Sheet Author
API Scripter
Your array creation looks just fine. Are you getting an error on that line, or is your logic just not working? Worth pointing out, you're missing a closing parentheses in your if conditional. Here's what I'd write: var fwCONSTANTS = { FWCOMMAND: "!fw", GMIDS: [77736, 235259] // 77736 is Konrad J., 235259 is Brian }; on('chat:message', function(msg) { var player = getObj('player', msg.playerid); var d20userid = player.get('d20userid'); var isGM = false; if (fwCONSTANTS.GMIDS.indexOf(d20userid) >= 0) { isGM = true; } ... }); ">= 0" over "!= -1" is just a preference, really. I find it easier to match my conditionals to what I'm really looking for. I want to see if the array contains the given id, not if the array does not not contain the id. If you want to check against the playerid instead of the d20userid, the process is basically the same, except your GMIDS array will contain long, cryptic strings of letters and numbers.
1391848349
Konrad J.
Pro
API Scripter
Thanks Brian, I guess it just took a few hours away from the computer to see the problem. the missing bracket was just a typo I made when putting the code into the forum, but you will notice I didn't capitalize the O in indexOf!!! :) I was so sure I was just doing something wrong with the array. If d20userid is just as good as playerid then I'll use it, I know everyone else is. Its easy to get the playerid with a chat command, but I guess its even easier for a user to get their d20userid. Thanks again!