Page 1 of 2

Initialize/Terminate variables scope

Posted: 17 Feb 2013 13:09
by Marco

Code: Select all

"_Initialize"
 global $a=1;
"Show"
 msg $a;
"Test"
 msg $a;
"_Terminate"
 msg Bye;
I would expect to read "1" in the message window. Is my expectation correct or initialize/terminate are supposed to work like this only with permanent variables?

Re: Initialize/Terminate variables scope

Posted: 17 Feb 2013 13:20
by PeterH
I'm afraid this only works for perm?

Re: Initialize/Terminate variables scope

Posted: 17 Feb 2013 13:22
by Marco
PeterH wrote:I'm afraid this only works for perm?
I didn't see that written anywhere, time for glasses? :oops:

Re: Initialize/Terminate variables scope

Posted: 17 Feb 2013 13:36
by PeterH
Ah - glasses. Seems I'm lucky: I use them... :P

But to the problem: _Initialize is a script for it's own, as every other script in the file - and so it has variables for it's own. So you can either define variables as global in _Initialize *and* in the other script(s), or use perm - as a variable defined as perm is automatically known in every script.

Re: Initialize/Terminate variables scope

Posted: 17 Feb 2013 13:41
by Marco
Oh, forgot to type global first!

Code: Select all

"_Initialize"
 global $a=1;
"Show"
 global $a;
 msg $a;
"Test"
 global $a;
 msg $a;
"_Terminate"
 msg Bye;
But neither this does what I would expect. Don't these script belong to the same stack, or stuff like that?
Or should I assume that then initialize/terminate are designed for perm only?

Re: Initialize/Terminate variables scope

Posted: 17 Feb 2013 13:47
by PeterH
:?:
Just tried it with "Try Script..." :arrow: works as I'd expect!

For both routines "1" is displayed :D

(I'm using 12.10.0013 in the moment.)

Re: Initialize/Terminate variables scope

Posted: 17 Feb 2013 13:50
by Marco
PeterH wrote::?:
Just tried it with "Try Script..." :arrow: works as I'd expect!

For both routines "1" is displayed :D

(I'm using 12.10.0013 in the moment.)
And with fresh 12.10.0014?

Re: Initialize/Terminate variables scope

Posted: 17 Feb 2013 13:55
by PeterH
You are right: with 0014 it does not work as expected :cry:

Re: Initialize/Terminate variables scope

Posted: 17 Feb 2013 14:47
by klownboy
Same here, though I tried the same (Marco) script with perm and it worked fine with v12.10.0014 (i.e., I received "1" and then "bye"). At least initially, I'm not sure it was Don's intention for it to work with global, he seemed to be concentrating on "perm" based on this thread http://www.xyplorer.com/xyfc/viewtopic.php?f=5&t=9224 Even in the beta description, Don refers to "perm" when referring to "_Initialize" and "_Terminate". Though I'm not sure why it wouldn't or shouldn't work with global.
Ken

Re: Initialize/Terminate variables scope

Posted: 17 Feb 2013 15:02
by PeterH
klownboy wrote:Same here, though I tried the same (Marco) script with perm and it worked fine with v12.10.0014 (i.e., I received "1" and then "bye"). At least initially, I'm not sure it was Don's intention for it to work with global, he seemed to be concentrating on "perm" based on this thread http://www.xyplorer.com/xyfc/viewtopic.php?f=5&t=9224 Even in the beta description, Don refers to "perm" when referring to "_Initialize" and "_Terminate". Though I'm not sure why it wouldn't or shouldn't work with global.
Ken
It's the question what you want to achieve, I think.

If you want to use variables to display in labels it seems that perm is best. (Or the only that makes sense?)
But I'd expect that normal rules for scripting apply here, too. That is: defining of global and perms has well defined meaning, and assigning of values has, too.

So if I define a global in scripts like "_Initialize", "_somename" or "Main" it should function alike. And the same is for assigning values.

From my POV _Initialize is nothing but a script that's automatically executed before label-selection is shown.

Re: Initialize/Terminate variables scope

Posted: 17 Feb 2013 15:33
by klownboy
Yes, I totally agree as long as the globals are declared in the other scripts within the MSR file, the usual rules should apply, and yes, we'll have to use a Perm variable if we want to display it in a menu. "_Initialize" will run in any MSR file first, if present so it's not just for the use prior to a menu display though that certainly is a big feature. I really like the fact that now Don has tweaked it such that if you have a menu, the script will end (go to "_Terminate") if you escape out of it for some reason and it will not display the menu. The "_Initialize" and "_Terminate" are great new features which will significantly improve the scripting possibilties especially used in conjunction with <get trigger> and now <get drop> as well. We can have a CTB that does one thing if we drop or drag a file on it with a left or right mouse drag, and do something totally different if we left or right right click on it. :D
Ken

Re: Initialize/Terminate variables scope

Posted: 17 Feb 2013 17:06
by admin
Difficult. Needs deeper meditation.

Re: Initialize/Terminate variables scope

Posted: 17 Feb 2013 17:10
by Marco
PeterH wrote:So if I define a global in scripts like "_Initialize", "_somename" or "Main" it should function alike. And the same is for assigning values.

From my POV _Initialize is nothing but a script that's automatically executed before label-selection is shown.
Exactly.
Imo, perm should be used for sharing data between different scripts, i.e. scripts coming/belonging to entirely different sources.
Thus, scripts belonging to a MSR should use globals, while scripts coming from different xys files (or different buttons, or catalog items) should rely on perms.
This would explain also my doubts about perms in script captions: shouldn't globals alone suffice?

Re: Initialize/Terminate variables scope

Posted: 17 Feb 2013 19:15
by klownboy
From my POV _Initialize is nothing but a script that's automatically executed before label-selection is shown.
Yes, but "_Initialize" is so much more than that since it is auto-recognized in any MSR file, it is executed first in any situation. It opens up additional possibilities as in other programs like AHK. Yes, I think it's a good thing that Don was able to make an XY variable like environmental variables display in a menu. The new scripts "_Initialize" and "_Terminate" should not be directly related to use of Perms. I'm not sure if he had coding reasons why he choose Perm in lieu of Globals as far as menu display.
Ken

Re: Initialize/Terminate variables scope

Posted: 17 Feb 2013 20:02
by admin
OK, this will work now:

Code: Select all

"_Initialize"
 global $a = "Bye!";
"Show 1"
 global $a;
 msg $a;
"Show 2"
 global $a;
 msg $a;
"_Terminate"
 global $a;
 msg $a;