Shoutbox

Saving Settings - 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: Saving Settings (/showthread.php?tid=86978)

Saving Settings by ArkaneArkade on 11-01-2008 at 02:38 AM

Hey guys,
I'm looking for a little help, regarding the best way to save persistent settings in a script.  I've been messing around with various different methods all day, and have been having no luck with them.
I started off using the registry, but that just turned buggy once I implemented multiple users.  After that I've tried ini files, and xml files, with little success.
Ideally I'd like to use something neat and tidy, such as xml, but going over things earlier I reallly didn't understand what I was doing, and don't wanna use code like that.  I've been reading the msn pages and stuff, but didn't help much, and I don't want the code if I don't understand it fully, cos I'd really rather learn it a bit better.

so basically, does anyone have any recommendations on good working methods of saving settings.  All I want atm is 2 strings and a bool for 2 seperate users, but I'm hoping to use many more later, once I've ironed out the problems.
Cheers guys,
Leroux


RE: Saving Settings by markee on 11-01-2008 at 04:00 AM

If you only want a few settings like that then I think registry is the best way to go.  Take a look at this page to see how to use the email of the user in the path to be able to differentiate them.  I personally prefer to use the UserId rather than Email address as it adds a little security to the settings imo.


RE: Saving Settings by ArkaneArkade on 11-01-2008 at 04:29 AM

I did have the registry working, pretty much identical to that, and it's been fine for long enough, but I tried using the email address and after that it started failing to read the registry about a quarter of the time.  The variables I tried setting ended up just blank.
I'll try it with the userid to see if that makes any difference to it.  Perhaps its just my email address screwing it up.


RE: Saving Settings by markee on 11-01-2008 at 04:58 AM

You need to make sure that the settings are there to be able to be read and that you are signed in when trying to read them or else it will fail.


RE: Saving Settings by SmokingCookie on 11-01-2008 at 02:27 PM

Suggestion

code:
function RegRead(Key) {
      if(Messenger.MyStatus>  0) { // See if someone is signed in
            try { // try reading from Key
                  return new ActiveXObject("WScript.Shell").RegRead(Key);
            }
            catch(e) { // error: it's very likely the key does not exist;
                  return "__none"; // common string telling your script the key doesn't exist
            }
      }
      return "__notsignedin"; // tells the script that no one is signed in
}

RE: Saving Settings by ArkaneArkade on 11-01-2008 at 05:33 PM

Cheers guys, but I did already have it done through a try/catch function that was called at initialise if MyStatus > 1, and also on user signin, just in case noone was in at signon.
Like I said, the the regread was failing about 1 in every 4 atempts, but it was always the same key that I was trying to get, so it definitly existed.

The problem seems to have stopped for the moment now that I'm using the userid instead of email so perhaps it was just that for whatever reason.


RE: Saving Settings by SmokingCookie on 11-01-2008 at 05:38 PM

Hmm.. What OS are you using? I always use email (don't know why :P ) and it works perfectly..


RE: Saving Settings by ArkaneArkade on 11-01-2008 at 05:43 PM

I'm on XP SP3.  If it works perfectly for you it may be something stupid, my computer screwing up or something (god I hope not, I've already lost one this month).


RE: Saving Settings by SmokingCookie on 11-01-2008 at 05:49 PM

Hmm.. Perhaps you can post some beta version of your script, or an example of your code?

and remove it after 30 minutes


RE: RE: Saving Settings by ArkaneArkade on 11-01-2008 at 05:55 PM

quote:
Originally posted by SmokingCookie
Hmm.. Perhaps you can post some beta version of your script, or an example of your code?

and remove it after 30 minutes

Lol.  Well, here's the code.  I severely doubt the script will be used by many, and none of this it major enough to steal since it could all be figured out really easily.

code:
function OnEvent_Initialize(MessengerStart)
{
    if ((MessengerStart == false) && (Messenger.MyStatus > 1))
    { LoadSettings(); }
}

function LoadSettings()
{
    try
    {
    X360Gamer = Shell.RegRead(MsgPlus.ScriptRegPath + "\\" + Messenger.MyUserId + "\\X360Gamer");
    X360GamerTag = Shell.RegRead(MsgPlus.ScriptRegPath + "\\" + Messenger.MyUserId + "\\X360GamerTag");
    X360PSM = Shell.RegRead(MsgPlus.ScriptRegPath + "\\" + Messenger.MyUserId + "\\X360PSM");
    }
    catch(exception)
    {
        ShowSettings();
    }
}

function ShowSettings()
{
    X360Settings = MsgPlus.CreateWnd("Interface.xml", "Settings");
    X360Settings.SetControlText("EdtGamerAccount", X360Gamer);
    X360Settings.SetControlText("EdtGamerTag", X360GamerTag);
    if(X360PSM == "Gamenames") { X360Settings.Button_SetCheckState("RadGamenames", true) }
    else if(X360PSM == "Gimmicks") { X360Settings.Button_SetCheckState("RadGimmicks", true) }   
}


- I stand corrected.  I've just saved again after copying that data (no change detected) and the catch went.  Ignored, save again, it got the data.  Just wierd.
RE: Saving Settings by SmokingCookie on 11-01-2008 at 06:01 PM

Well I see what you're up to :P

Why do you call ShowSettings if an error occurs in LoadSettings? Just set X360Gamer (and the other 2) to an empty string (var myVar = "") and create the "Settings" window..

Also, a little note: you my use "(!MessengerStart) && (Messenger.MyStatus > 0)" instead of "MessengerStatus == false (etc)"..

And if you start Messenger, thes script will never be started, unless you hit [CTRL] + [S] in Plus!'s (ot your) editor. You may want to call OnEvent_Initialize with parameter false when OnEvent_SigninReady occurs. It may be handy to declare a variable on the global scope with value false to indicate the script has not yet initialised. Look:

code:
var Initialised = false;

function OnEvent_Initialize(boolMsgStart) {
      if((!boolMsgStart) && (Messenger.MyStatus > 0)) {
            // Whatever you want to do
            Initialised = true;
      }
}

function OnEvent_SigninReady(strEmail) {
      if(!Initialised) { // equal to if(Initilaised == false)
            OnEvent_Initialize(false);
            // Whatever you want to do
      }
}


RE: Saving Settings by ArkaneArkade on 11-01-2008 at 06:10 PM

I knew not to trust you, seeing through what I'm doing ;).  Ah well, I've made no secret of it, just trying to do something I like, and learn a littl ein the process.

TBH, the showsettings was being used because thats how I did it in my last script, which worked fine, and to ensure that the strings were always there since it doesn't work without.

Cheers for the !MessengerStart suggestion.  I'm still learning little tricks like that.  hopefully one day I will return to form and be able to script anything again.  (Believe it or not - I struggle to - I was once top of class at Uni, far above everyone else.  Now, I'm on the streets, being handed scraps by those with plenty).


- Sorry, should have explained the script bit there.  When I put up the code, I omitted the UserSignon, which called LoadSettings as well, so that the script did also get the vars when someone signed on, so as to give multi user support.


RE: Saving Settings by SmokingCookie on 11-01-2008 at 06:17 PM

quote:
Originally posted by Leroux
I knew not to trust you, seeing through what I'm doing ;).
quote:

Yea, you use meaningful variable names :P

About the !MessengerStart thingy..
The exclamation sign is the "NOT" operator. So if you have MyVar = false; then !MyVar will be true. You can basically think of the NOT operator "inveting" a boolean.
Also, code controlled by "if(true)" will always be executed (I know it sounds self-explaining :P ). if(!false) will also be executed. (false will be invertedby the "!").


RE: RE: Saving Settings by ArkaneArkade on 11-01-2008 at 06:31 PM

quote:
Originally posted by SmokingCookie

code:
var Initialised = false;

function OnEvent_Initialize(boolMsgStart) {
      if((!boolMsgStart) && (Messenger.MyStatus > 0)) {
            // Whatever you want to do
            Initialised = true;
      }
}

function OnEvent_SigninReady(strEmail) {
      if(!Initialised) { // equal to if(Initilaised == false)
            OnEvent_Initialize(false);
            // Whatever you want to do
      }
}



I don't mean to criticise, but won't that script only work once, either at startup or when someone signs in, meaning that any subsequent signons (brother for instance) will still have the same variables, or is there some sneaky bit I'm missing?

As for meaningful variables - I have ocd, and can't do otherwise.  Besides, much of my scripting is learned by looking at others scripts, and I'd love someday if that was what mines were used (along with my creation purpose) for and it actually helped people pickup some things.
RE: Saving Settings by SmokingCookie on 11-01-2008 at 06:36 PM

That's why you may want to use OnEvent_Signout() as well ;)

When OnEvent_Signout occurs, you may set Initialised to false (following my example). When OnEvent_Signin (and therefore OnEvent_Initialize) is called, Initialised will be set to true, and LoadSettings is called (which then overwrites the previously loaded variables).

You may only declare a variable once (on "the global scope" or "outside functions"), but you can assign it as many values as you want ;)
Inside functions, however, you may use variable names more than once, as long as they're not declared in the same function.

Example:

code:
var myGlobalVar = "Gone global"; // Global scope: accessible throughout the script

function OnEvent_Initialize(bool) {
      var myVar = "Hello world"; // local scope: only in this function
      myGlobalVar = "WorldWideWeb"; // It's global, accessible everywhere
}

function OnEvent_Uninitialize(bool) {
      var myVar = "Goodbye world"; // local scope
      Debug.Trace(myGlobalVar); // Again: global scope so accessible throughout script
}


EDIT::

As a security measure, you can let LoadSettings check if Initialised is true:

code:
function LoadSettings() {
     if(Initialised) {
          //Load them now
     }
}

// an extra security measure could be
function LoadSettings() {
     if((Initialised) && (Messenger.MyStatus > 0)) {
          //Load them now
     }
}



RE: RE: Saving Settings by ArkaneArkade on 11-01-2008 at 06:45 PM

quote:
Originally posted by SmokingCookie
That's why you may want to use OnEvent_Signout() as well ;)
[/code]
I guess you could do it that way.  Just making sure you never had aces up your sleeve.  Unfortunately I got work just now, but I'll give it a try when I get back.  Cheers mate.