What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » MsgPlus.LoadScriptFile() problem

MsgPlus.LoadScriptFile() problem
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: MsgPlus.LoadScriptFile() problem
Note that LoadScriptFile is not meant to be a "insert all the code from the loaded script in this very spot" function.

LoadScriptFile behaves just the same as how Plus! loads multiple script files in the main script folder.

The global scope of scripts is first completely parsed before they are executed. That is the reason why you can get away with:
code:
function DoSomething() {
    Debug.Trace(mystring)
}
var mystring = "Hello World"

The same happens in Eljay's first example. Everything is in the global scope, so main.js is parsed. With that, Plus! sees that LoadScript is used, so it appends the test.js script to main.js. In other words you'll get one giant script like this:
code:
Debug.Trace('1. Before LoadScriptFile');
MsgPlus.LoadScriptFile('\\C:\\test.js'); // mainingless once this is parsed
Debug.Trace('3. After LoadScriptFile');
Debug.Trace('2. During LoadScriptFile');
with the expected output: 1 3 2 once it is executed...

However, in his second example, he puts MsgPlus.LoadScriptFile in a function. Which means it is not in the global scope anymore. And thus will not be executed (the stuff which is in the loaded script) until Initialize function is executed. Which is done after the globale scope of main.js is parsed and after the global scope of main.js is executed.

So it does:
1) parse global scope of Main.js (and any other script file in the script directory)
2) execute all the stuff as if it was 1 giant script.
3) function Initialize is called
3.1) Debug.Output("1")
3.2) The test.js script is parsed and executed, thus you'll get "2" as output. However, although it seems like it was executed in the scope of this function, it is not. The debug.output in test.js is still in the global scope. Just the execution of main.js is halted for a while while test.js is parsed and executed.
3.3) Debug.Output("3").

To better illustrate what happens you better make test.js like this:
code:
var mystring = "Hello World"
And as a first example, main.js like:[code]MsgPlus.LoadScriptFile('\\C:\\test.js');
Debug.Trace(mystring);
function OnEvent_Initialize() {
    Debug.Trace(mystring)
}
When you first run this script you'll get an error that mystring isn't defined on line 2. This is already proof that test.js is appended after main.js and not inserted.

So, remove that first Debug.Trace line and rerun the script.

Now you'll see that the script executes properly and mystring is printed because it was defined in the global scope of the script and thus also in the scope of the Initialize function.

To make it maybe even more clear, make main.js like:
code:
function OnEvent_Initialize(MessengerStart) {
      MsgPlus.LoadScriptFile('\\C:\\test.js');
      MsgPlus.AddTimer("1", 1000)
}

function OnEvent_Timer() {
      Debug.Trace(mystring);
}
This makes it very clear that even if you put MsgPlus.LoadScriptFile inside a function, it will not act as a "insert code in this spot" method.

After executing this script you'll see that mystring is printed properly, although it was not defined in the Timer event. This because it was loaded and thus defned in the global scope, when the test.js script was loaded in the Initialize function.

To recap:
- MsgPlus.LoadScriptFile is not meant to be an "insert code here" function
- MsgPlus.LoadScriptFile parses and executes script files on demand by appending the script file to the already executing main script. During this time the main script is temporary halted.
- Nomatter where you place it, the stuff inside the loaded script file is always put in the global scope, just like any other individual script file.
- sidenote: every individual script file in the script's directory is joined together with all the other script files to form one giant script. It is this giant script which is actually parsed and executed in Plus!. The individual script files are just there for your editing convenience.

-----------

So, actually, it is expected behaviour though, and I wouldn't expected it to behave any differently (again, it is not a "insert code here" function).

I hope all this makes some sense :p

This post was edited on 12-26-2007 at 11:24 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
12-26-2007 10:33 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
MsgPlus.LoadScriptFile() problem - by Jeroen Noten on 12-25-2007 at 02:44 PM
RE: MsgPlus.LoadScriptFile() problem - by Volv on 12-25-2007 at 03:12 PM
RE: MsgPlus.LoadScriptFile() problem - by Spunky on 12-25-2007 at 05:39 PM
RE: RE: MsgPlus.LoadScriptFile() problem - by Volv on 12-26-2007 at 10:45 AM
RE: MsgPlus.LoadScriptFile() problem - by Eljay on 12-25-2007 at 10:10 PM
RE: MsgPlus.LoadScriptFile() problem - by Matti on 12-26-2007 at 10:07 AM
RE: MsgPlus.LoadScriptFile() problem - by CookieRevised on 12-26-2007 at 10:33 AM
RE: MsgPlus.LoadScriptFile() problem - by Matti on 12-26-2007 at 11:12 AM
RE: MsgPlus.LoadScriptFile() problem - by Jeroen Noten on 12-28-2007 at 07:47 PM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On