Shoutbox

global variable not working - 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: global variable not working (/showthread.php?tid=76285)

global variable not working by scutterman on 07-23-2007 at 09:30 PM

I cant touch type so if someone sends me a msg while I am sending one to them I somethimes don't see it until I have sent my msg.

I am trying to create a script to play a sound whenever another person in the coversation sends a message

The code is:

var name; // used to store the name of the user

function OnEvent_Initialize(MessengerStart)
{

    Debug.Trace("Script Started");

}


function OnEvent_SigninReady(Email) // this should assign the name to the variable
{
    name = Messenger.MyName;
    name = MsgPlus.RemoveFormatCodes(name);
    MsgPlus.DisplayToast("",name) // i used this to confirm that this stage was working. It is
}



function OnEvent_ChatWndReceiveMessage(ChatWnd,Origin,Message,MessageKind) // this should play the sound
{                                                                  //if origin is not the same as name

MsgPlus.DisplayToast("",name) // this is used to check that the name is still stored. It is just blank.

Origin = MsgPlus.RemoveFormatCodes(Origin);

    if (Origin != name)
    {
    MsgPlus.PlaySound("i likse the way you moo.mp3",1500) //this sound will be replaced with "type" when i have converted it to mp3
    }
}


function OnEvent_Uninitialize(MessengerExit)
{

    Debug.Trace("Script Ended");

}


The problem is that "name" seems to loose its value when the SigninReady function ends, which it shouldn't do because i've declared it as a global variable.

I'm pretty new to jscript and mpl scripting so any help is appreciated.

Thanks
~~Scutterman~~

RE: global variable not working by roflmao456 on 07-23-2007 at 10:33 PM

maybe if you try local variables ? :tongue:


RE: global variable not working by albert on 07-23-2007 at 10:56 PM

quote:
Originally posted by roflmao456
maybe if you try local variables ? :tongue:

I don't see how this would solve the issue. =P

quote:
Originally posted by roflmao456
The problem is that "name" seems to loose its value when the SigninReady function ends, which it shouldn't do because i've declared it as a global variable.

Pretty simple, the name variable should be declared on top.
Eg :

code:

name = Messenger.MyName;

function OnEvent_Initialize(MessengerStart)
{

Debug.Trace("Script Started");

}


function OnEvent_SigninReady(Email) // this should assign the name to the variable
{
name = MsgPlus.RemoveFormatCodes(name);
MsgPlus.DisplayToast("",name) // i used this to confirm that this stage was working. It is
}



function OnEvent_ChatWndReceiveMessage(ChatWnd,Origin,Message,MessageKind) // this should play the sound
{   //if origin is not the same as name

MsgPlus.DisplayToast("",name) // this is used to check that the name is still stored. It is just blank.

Origin = MsgPlus.RemoveFormatCodes(Origin);

if (Origin != name)
{
MsgPlus.PlaySound("i likse the way you moo.mp3",1500) //this sound will be replaced with "type" when i have converted it to mp3
}
}



RE: global variable not working by scutterman on 07-23-2007 at 11:07 PM

ok it seems to be working now. Thanks for the help

~~Scutterman~~

RE: global variable not working by Spunky on 07-23-2007 at 11:39 PM

There is a big bug in this script if you want to change your name...

It could all be done in one function and would solve this problem:

code:

function OnEvent_ChatWndReceiveMessage(ChatWnd,Origin,Message,MessageKind) {
     MsgPlus.DisplayToast("",name);
     if (MsgPlus.RemoveFormatCodes(Origin) != MsgPlus.RemoveFormatCodes(Messenger.MyName)){
          MsgPlus.PlaySound("i likse the way you moo.mp3",1500);}
}

RE: global variable not working by matty on 07-24-2007 at 02:10 AM

quote:
Originally posted by albert
Pretty simple, the name variable should be declared on top.
That is wrong and will only work if the user is already signed in.

code:
var name = new String('');

function OnEvent_Initialize(MessengerStart){
    try {
        name = MsgPlus.RemoveFormatCodes(Messenger.MyName);
        MsgPlus.DisplayToast('', name)
        Debug.Trace('Script Started');
    } catch (err) {}
}

function OnEvent_SigninReady(eMail) {
    OnEvent_Initialize(true);
}

function OnEvent_ChatWndReceiveMessage(oChatWnd, sOrigin, sMessage, nMessageKind) {
    MsgPlus.DisplayToast('', name)
    if (MsgPlus.RemoveFormatCodes(Origin) != name) {
        MsgPlus.PlaySound('i likse the way you moo.mp3' ,1500)
    }
}

function OnEvent_Uninitialize(MessengerExit){
    Debug.Trace('Script Ended');
}


RE: global variable not working by albert on 07-24-2007 at 02:21 AM

quote:
Originally posted by Matty
That is wrong and will only work if the user is already signed in.

Isn't it how you create a global variable though?

Because as I understood by the title and thread, that's what he wanted to know.. I didn't look at the code any further.
RE: global variable not working by matty on 07-24-2007 at 02:28 AM

You create a global variable by creating it outside the scope of functions hence the top of the script. However if Messenger isn't signed in then the object Messenger.MyName doesn't exist and will cause the script to stop and never start when they sign back in.

That is why my code I create the global variable outside of the scope but declare it as a string instead of Messenger.MyName.

Also the script wont run on Initialize because it catches an error if the user isn't signed in. And when they are signed in it will recall Initialize function.

This is a much better practise. Or instead of try() you can use if (Messenger.MyStatus > 2)


RE: global variable not working by scutterman on 07-24-2007 at 09:07 AM

Ol, Matty's code seems good but  i did notice that the:

if (MsgPlus.RemoveFormatCodes(Origin) != name) {

inside the ChatWndReceiveMessage function should replace "Origin" with "sOrigin" since that's what it is declared as in the function, is that right?

Thanks again for all of the help
~~Scutterman~~

RE: global variable not working by matty on 07-24-2007 at 01:16 PM

quote:
Originally posted by scutterman
Ol, Matty's code seems good but  i did notice that the:

if (MsgPlus.RemoveFormatCodes(Origin) != name) {

inside the ChatWndReceiveMessage function should replace "Origin" with "sOrigin" since that's what it is declared as in the function, is that right?

Thanks again for all of the help
~~Scutterman~~

That would be correct!

Ummm 5000 posts weee
RE: global variable not working by markee on 07-24-2007 at 01:54 PM

quote:
Originally posted by scutterman
Ol, Matty's code seems good but  i did notice that the:

if (MsgPlus.RemoveFormatCodes(Origin) != name) {

inside the ChatWndReceiveMessage function should replace "Origin" with "sOrigin" since that's what it is declared as in the function, is that right?

Thanks again for all of the help
~~Scutterman~~

That is true but there are problems with your code if you end up using some features of StuffPlug or something like that however.... I suggest using the following code instead....
code:
var aSent = new Array();

function OnEvent_ChatWndSendMessage(ChatWnd, Message) {
    aSent[ChatWnd.Handle] = Message;
}

function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, MessageKind) {
    if (aSent[ChatWnd.Handle] === Message) {
        delete aSent[ChatWnd.Handle];
    } else {
        MsgPlus.PlaySound("i likse the way you moo.mp3",1500) //this sound will be replaced with "type" when i have converted it to mp3
    }
}

Please note that this isn't my code, it is CookieRevised's code and any thanks should go to him.  It is a more failsafe way of going about what you are trying and I HIGHLY recommend that you use it instead.

I also would like to point everyone to the code at the bottom of CookieRevised's reply to [Fix] Problem with OnEvent_ChatWndRecieveMessage's Origin for any issue involving triggering events only when a contact sends a message.
RE: global variable not working by scutterman on 07-24-2007 at 03:17 PM

The code looks good but as I said I'm new so I would be realy grateful if someone could talk me through it.

P.S. why are there 3 "=" signs in this part:
if (aSent[ChatWnd.Handle] === Message) {

Thanks alot
~~Scutterman~~

RE: global variable not working by matty on 07-24-2007 at 03:55 PM

quote:
Originally posted by scutterman
The code looks good but as I said I'm new so I would be realy grateful if someone could talk me through it.

P.S. why are there 3 "=" signs in this part:
if (aSent[ChatWnd.Handle] === Message) {

Thanks alot
~~Scutterman~~

It is a Bitwise comparison. Meaning each byte has to match. if (2 == '2') (will return true) 2=== '2' will return false