Saving variables ActiveXObject & shell - 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 variables ActiveXObject & shell (/showthread.php?tid=94821)
Saving variables ActiveXObject & shell by PedroGabriel on 06-19-2010 at 05:48 PM
I'm making a script
but the var's don't save, If I relog and return my vars desapear...
how to make vars don't reset?
I'm trying...
my code:
code: var score=0
function scorewrite(score){
var Shell = new ActiveXObject("WScript.Shell");
var ValRegPath = MsgPlus.ScriptRegPath + score;
Shell.RegWrite(ValRegPath, 0);
}
function scoreread(Score){
var score = Shell.RegRead(ValRegPath);
var score = Shell.RegRead(MsgPlus.ScriptRegPath + score);
}
function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, rMessage, MsgKind){
if (rMessage=='+1'){score+=1; scorewrite(score);}
}
function OnEvent_Timer(Timer){
if (Timer=='rato'){
Messenger.MyPersonalMessage=score
MsgPlus.AddTimer('rato',1000)
}
}
function OnEvent_Initialize(MessengerStart){
MsgPlus.AddTimer('rato',1000)
score = scoreread(score);
}
function OnEvent_SignIn(){score=scoreread(score);}
function OnEvent_Uninitialize(MessengerExit){scorewrite(score);}
with my code
when you say +1
add +1 to score and show on the psm
if I leave from the Messenger and return
when I say +1 nothing happend...
I really want save var's to close msn and don't lose the values
Sorry my bad inglish...
Thanks!
RE: Saving variables ActiveXObject & shell by Matti on 06-19-2010 at 06:30 PM
It appears that your registry path is faulty. From what I understand, you want to read and write the key with the following path:
code: HKEY_CURRENT_USER\Software\Patchou\Messenger Plus! Live\GlobalSettings\Scripts\My Script\score
Now look at what you wrote:
Javascript code:
1. function scorewrite(score){
2. var Shell = new ActiveXObject("WScript.Shell");
3. var ValRegPath = MsgPlus.ScriptRegPath + score;
4. Shell.RegWrite(ValRegPath, 0);
5. }
On line 3 you are trying to build the key path, however you are doing this by concatenating the script's registry path with the value of "score". Thus, you get key paths such as:
code: HKEY_CURRENT_USER\Software\Patchou\Messenger Plus! Live\GlobalSettings\Scripts\My Script\0
HKEY_CURRENT_USER\Software\Patchou\Messenger Plus! Live\GlobalSettings\Scripts\My Script\1
HKEY_CURRENT_USER\Software\Patchou\Messenger Plus! Live\GlobalSettings\Scripts\My Script\2
...
Since these are all different keys, you are reading from and writing to different keys. No wonder you get different results!
For more details about variables and strings, I'd suggest learning about the basic rules and concepts of JScript. There are very good tutorials on the internet, for example W3School's JavaScript tutorials.
Some other important remarks:
- You are defining Shell as a local variable in scorewrite(), but you are trying to access it from scoreread() as well. This won't work, as Shell doesn't exist in the function scope of scoreread(). A solution would be to make Shell a global variable by defining it outside any function, making it accessible for all functions in your script.
- In OnEvent_Initialize(), you are trying to get a return value from scoreread() and store this in "score". However, scoreread() doesn't have a return statement and will return just void, disposing any value previously stored in "score".
- The timer will still continue to run after the user has signed out in which case it will fail to set a personal message. You should either cancel the timer in the sign out event or check whether the user is still logged in every time the timer event is fired.
- The timer interval is way to low to be used safely. The Messenger protocol allows you to change your status info (name, message, status and display picture) maximum 10 times per minute. By changing the message every second, the risk of passing that limit is very real. I'd suggest increasing the interval to 6 to 10 seconds.
I did my best to "fix" your code and added comments explaining how everything is done. Please spend some time studying how this works and learn from it to write better code yourself.
Javascript code:
// Global variables
var Score = 0;
var ScorePath = '';
var Shell = new ActiveXObject('WScript.Shell');
/* READ/WRITE SCORE */
function ReadScore(){
// Check if registry path is set
if(ScorePath){
// Read score to global variable
Score = Shell.RegRead(ScorePath);
}
// Return current score
return Score;
}
function WriteScore(){
// Check if registry path is set
if(ScorePath){
// Store score in registry
Shell.RegWrite(ScorePath, Score);
}
}
/* MAIN EVENTS */
function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, rMessage, MsgKind){
if (rMessage == '+1'){
// Increase score by one
Score++;
// Write score
ScoreWrite();
}
}
function OnEvent_Timer(Timer){
if (Timer == 'rato'){
// Set personal message
Messenger.MyPersonalMessage = 'Score: ' + score;
// Recreate timer
MsgPlus.AddTimer('rato', 10000);
}
}
/* STARTUP EVENTS */
function OnEvent_Initialize(MessengerStart){
// Call sign in event if already logged in
if(Messenger.MyStatus > 0) OnEvent_SignInReady(Messenger.MyEmail);
}
function OnEvent_SigninReady(Email){
// Set score registry path
ScorePath = MsgPlus.ScriptRegPath + '\\Score';
// Start timer
MsgPlus.AddTimer('rato', 10000);
}
/* SHUTDOWN EVENTS */
function OnEvent_Signout(Email){
// Stop timer
MsgPlus.CancelTimer('rato');
// Write score
ScoreWrite();
// Reset globals
Score = 0;
ScorePath = '';
}
RE: Saving variables ActiveXObject & shell by PedroGabriel on 06-19-2010 at 08:30 PM
Thank you for your help, I managed to learn a lot.
eg the Messenger can only change the psm 10 times per minute
Thanks you!
i'm testing your code and noticed that he has not done exactly what I wanted ...
when I left and return to the Messenger
I write "+1" but the score does not increase ...
is this the only error that has ...
(I can not fix this error, is hard...)
thanks again
RE: RE: Saving variables ActiveXObject & shell by Matti on 06-20-2010 at 07:51 AM
Oops, small typo.
Javascript code:
function OnEvent_Timer(Timer){
if (Timer == 'rato'){
// Set personal message
Messenger.MyPersonalMessage = 'Score: ' + Score; // Recreate timer
MsgPlus.AddTimer('rato', 10000);
}
}
The old name "score" should have been replaced by "Score". JScript is case sensitive when it comes to variable and function names.
RE: Saving variables ActiveXObject & shell by PedroGabriel on 06-20-2010 at 07:56 AM
oh Thanks for your help, I will try
EDIT:
the code works...
but when I leave from the MSN and return the Score var reset
why?
the code have this line:
code: function OnEvent_SigninReady(Email){
// Set score registry path
ScorePath = MsgPlus.ScriptRegPath + '\\Score';
// Start timer
MsgPlus.AddTimer('rato', 10000);
}
I really don't understand why the code don't return the var when Messenger start...
thank you again!
RE: Saving variables ActiveXObject & shell by whiz on 06-20-2010 at 10:00 AM
The reason for this is because when you sign in to Messenger, the score variable is defined as 0, but it isn't actually set from the registry each time, so it will just continue from the beginning each time.
Change the sign-in function to this:
Javascript code:
function OnEvent_SigninReady(Email){
// Set score registry path
ScorePath = MsgPlus.ScriptRegPath + '\\Score';
// Set score variable Score = ReadScore(); // Start timer
MsgPlus.AddTimer('rato', 10000);
}
RE: Saving variables ActiveXObject & shell by Matti on 06-20-2010 at 02:07 PM
Dammit, can't believe I missed that.
Oh, and you don't really need to assign the return value of ReadScore() to Score, just call it and it'll set Score itself.
|