quote:
Originally posted by stoshrocket
Use a loop and a couple of arrays to replace the number?
Don't do this. There is absolutely no need for a loop to simply show the name of the current status, you can perfectly achieve this with an object literal indexed by the status number. Using arrays for this is totally unnecessary since we only need the ability to look up a certain property value (the name) by its identifier (the number).
js code:
// On the top of your script, outside any function definition
var oStatusNames = {
0: "unknown",
2: "appearing offline",
3: "online",
4: "busy",
5: "be right back",
6: "idle",
7: "away",
8: "in a call",
9: "out to lunch"
};
// The fixed event handler for chat window created
function OnEvent_ChatWndCreated( ChatWnd )
{
// Find the name of the current status in our object
var status = oStatusNames[ Messenger.MyStatus ];
// Make a nice message
var message = "Your current status is " + status + "!";
// Display a toast
MsgPlus.DisplayToast( "Your current status", message );
}
As you can see, there's no need for looping through one array and replace all numbers by their status names: you perfectly know what status number you're looking for as you're building the message yourself! There's also no real necessity for calling MsgPlus.RemoveFormatCodes() when you don't add any format codes in the message. Of course, when the message could have been set by the user through some kind of script preference, you're better off removing the format codes, but in this simple example I chose not to.
As for setting your status, you can simply set Messenger.MyStatus yourself, it's not read-only:
js code:
function OnEvent_ChatWndSendMessage( ChatWnd, Message )
{
// See if I'm not busy yet
if( Messenger.MyStatus !== 4 ) {
// Set to busy
Messenger.MyStatus = 4;
}
// Return the unmodified message
// Never forget to return something for ChatWndSendMessage!
return Message;
}
quote:
Originally posted by 5n4k3
Thanks for that, btw is there a place where I can learn about "arrays", etc?
I highly recommend learning some JScript/JavaScript if you want to write Plus! scripts. There are many places to learn this, but perhaps the
W3Schools tutorials are best suited for newcomers. Just beware that you're not programming in a website environment, so anything related to the place of your script in the HTML or the Document Object Model is not available in a Plus! script. Other than that, the syntax of JavaScript is as good as identical to JavaScript's syntax, therefore it's a very good starting point.
Arrays may not be ideal for this example (see above), but they're very interesting to work with. Basically, an array is a numbered list of stuff in which you can add, read, change and remove elements. Have a look at the W3Schools topics on arrays, that'll probably help you understand.