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 = '';
}