Shoutbox

Problem with Script - ; required? Where? - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Problem with Script - ; required? Where? (/showthread.php?tid=76466)

Problem with Script - ; required? Where? by Toneo on 08-01-2007 at 07:22 PM

I'm making an AutoMessage script, and it uses random messages. But it stops the script and the debugger has an error:

code:
Error: Expected ';' (code: -2146827284)
       File: AutoMessage.js. Line: 16.


Here is where it finds fault with my script.

code:
int randomNum1 = Math.round(Math.random() * 5 + 1);

switch (randomNum1)
{
case 1 :
randomMsg = "Hello!";
break;

case 2 :
randomMsg = "Hi!";
break;

case 3 :
randomMsg = "Yo";
break;

case 4 :
randomMsg = "Allo";
break;

case 5 :
randomMsg = "Hiya";
break;

case 6 :
randomMsg = "How are you?";
break;
}


Help needed, can't find where to put this ;, or what's wrong with it...
RE: Problem with Script - ; required? Where? by Mnjul on 08-01-2007 at 08:02 PM

quote:
Originally posted by Toneo
int randomNum1
Use var. (or use nothing at all)

JScript's variable declaration does not use data type as declarer (or whatever it's called)
RE: Problem with Script - ; required? Where? by matty on 08-01-2007 at 08:10 PM

blah why not do this

code:
var aWordArray = new Array ('Hello!', 'Hi!', 'Yo', 'Allo', 'Hiya', 'How are you?');
Debug.Trace(aWordArray[Math.round(Math.rand() * aWordArray.length + 1)]);

RE: Problem with Script - ; required? Where? by markee on 08-01-2007 at 11:45 PM

quote:
Originally posted by matty
blah why not do this

code:
var aWordArray = new Array ('Hello!', 'Hi!', 'Yo', 'Allo', 'Hiya', 'How are you?');
Debug.Trace(aWordArray[Math.round(Math.rand() * aWordArray.length + 1)]);


I think you actually mean this:
code:
var aWordArray = new Array ('Hello!', 'Hi!', 'Yo', 'Allo', 'Hiya', 'How are you?');
Debug.Trace(aWordArray[Math.floor(Math.rand() * aWordArray.length + 1)]);

And you should use Math.floor() rather than Math.round() anyway, this is because the use of rounding means that the first and last variable have only half of the probability of getting chosen as the others do.  And with matty's code anywa, the addition of 1 would have meant that the array elements would have to start from 1 rather than zero.
RE: Problem with Script - ; required? Where? by Toneo on 08-02-2007 at 08:41 AM

Thanks :) Forgot about arrays and floor(). Didn't know whether to use int or var.

EDIT:
It's still not working. The debugger now says "Error: ; expected" on line 16. I don't understand, there doesn't seem to be a place to put ;. And I don't seem to find anything wrong with my script.

The whole function ChatWnd_ReceiveMessage function.

code:
function OnEvent_ChatWndReceiveMessage(ChatWnd,Origin,Message,MessageType)
{

if (Origin != Messenger.MyName)
{

if (Messenger.MyStatus != 3)
{
ChatWnd.SendMessage("AutoMessage: I'm busy right now.");
}

var wordArray = new Array ('Hello!', 'Hi!', 'Yo', 'Allo', 'Hiya', 'Why, Hello!');
var randomMsg = wordArray[Math.floor(Math.rand() * wordArray.length)];

switch (Message.toUpperCase())
{
case "HI" :
ChatWnd.SendMessage(randomMsg);
break;

case "HI!" :
ChatWnd.SendMessage(randomMsg);
break;

case "LO" :
ChatWnd.SendMessage(randomMsg);
break;

case "\'LO" :
ChatWnd.SendMessage(randomMsg);
break;

case "HIYA" :
ChatWnd.SendMessage(randomMsg);
break;

case "HIYA!" :
ChatWnd.SendMessage(randomMsg);
break;

case "HELLO!" :
ChatWnd.SendMessage(randomMsg);
break;

case "HELLO" :
ChatWnd.SendMessage(randomMsg);
break;

case "YO" :
ChatWnd.SendMessage(randomMsg);
break;

case "YO." :
ChatWnd.SendMessage(randomMsg);
break;

case "YO!" :
ChatWnd.SendMessage(randomMsg);
break;

case "ALLO" :
ChatWnd.SendMessage(randomMsg);
break;

case "ALLO!" :
ChatWnd.SendMessage(randomMsg);
break;
}

}

}


RE: Problem with Script - ; required? Where? by LifelesS on 08-02-2007 at 12:00 PM

I try to run the code myself, it just gave me an error on the:
var randomMsg = wordArray[Math.floor(Math.rand() * wordArray.length)];

Says object doesn't support this property or method, anyway, shouldn't it be easier to turn the array into an enumerator and use a for loop?

something like this:

code:
        var e = new Enumerator(wordArray)
        for(; !e.atEnd(); e.moveNext())
        {
            var word = e.item();
            if(Message.toUpperCase() == word)
            {
                ChatWnd.SendMessage(randomMsg);
                break;
            }
        }

- you had this to toUppercase() instead of toUpperCase() also

That should work, at least works for me:)
You just need to add all other things to the array and your set to go ;)
And if you're going to use many words, you can put them on a .txt file and load it to an array using split() after readall()
RE: Problem with Script - ; required? Where? by matty on 08-02-2007 at 12:40 PM

Untested but should work

code:
var wordArray = new Array ('Hello!', 'Hi!', 'Yo', 'Allo', 'Hiya', 'Why, Hello!');
var receivedWordArray = new Array ('HI', 'HI!', 'LO', '\'LO', 'HIYA', 'HELLO!', 'HELLO', 'YO.', 'YO!', 'YO', 'ALLO!', 'ALLO');

function OnEvent_ChatWndReceiveMessage(ChatWnd,Origin,Message,MessageType) {
    if (Origin != Messenger.MyName) {
        if (Messenger.MyStatus != 3) {
            ChatWnd.SendMessage('AutoMessage: I\'m busy right now.');
            return false;
        }
        var randomMsg = wordArray[Math.floor(Math.rand() * wordArray.length)];
        for (var i in receivedWordArray) {
            if (Message.toUperCase() === receivedWordArray[i]) {
                ChatWnd.SendMessage(randomMsg);
            }
        }
    }
}

Also please note that this can cause infinite loops and you should take precautions when sending messages.

Best way is to store the Message you send in an object then compare the message if it is the same then disregard it.
RE: Problem with Script - ; required? Where? by LifelesS on 08-02-2007 at 12:43 PM

matty way is simpler I think, and instead of using all that things in the array ('YO', 'YO!') you could use a Regular Expression.


RE: Problem with Script - ; required? Where? by Ezra on 08-02-2007 at 01:03 PM

I think this will work even better

code:
var wordArray = new Array ('Hello!', 'Hi!', 'Yo', 'Allo', 'Hiya', 'Why, Hello!');
var receivedWordArray = new Array ('HI', 'HI!', 'LO', '\'LO', 'HIYA', 'HELLO!', 'HELLO', 'YO.', 'YO!', 'YO', 'ALLO!', 'ALLO');
var re = RegExp("\b"+receivedWordArray.join("\b|\b")+"\b","i");

function OnEvent_ChatWndReceiveMessage(ChatWnd,Origin,Message,MessageType) {
    if (Origin != Messenger.MyName) {
        if (Messenger.MyStatus != 3) {
          ChatWnd.SendMessage('AutoMessage: I\'m busy right now.');
          return false;
        }
        var randomMsg = wordArray[Math.floor(Math.rand() * wordArray.length)];
        if (re.test(Message)){
          ChatWnd.SendMessage(randomMsg);
        }
    }
}

Credits to markee for the regex
RE: RE: Problem with Script - ; required? Where? by LifelesS on 08-02-2007 at 01:15 PM

quote:
Originally posted by matty

Also please note that this can cause infinite loops and you should take precautions when sending messages.

Best way is to store the Message you send in an object then compare the message if it is the same then disregard it.


That's y I used the break; inside the if, so when it founds the word quits for loop.