Initialize/Terminate variables scope

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Initialize/Terminate variables scope

Post 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?
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

PeterH
Posts: 2829
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Initialize/Terminate variables scope

Post by PeterH »

I'm afraid this only works for perm?

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: Initialize/Terminate variables scope

Post by Marco »

PeterH wrote:I'm afraid this only works for perm?
I didn't see that written anywhere, time for glasses? :oops:
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

PeterH
Posts: 2829
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Initialize/Terminate variables scope

Post 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.

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: Initialize/Terminate variables scope

Post 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?
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

PeterH
Posts: 2829
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Initialize/Terminate variables scope

Post 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.)

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: Initialize/Terminate variables scope

Post 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?
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

PeterH
Posts: 2829
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Initialize/Terminate variables scope

Post by PeterH »

You are right: with 0014 it does not work as expected :cry:

klownboy
Posts: 4462
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Initialize/Terminate variables scope

Post 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

PeterH
Posts: 2829
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Initialize/Terminate variables scope

Post 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.

klownboy
Posts: 4462
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Initialize/Terminate variables scope

Post 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

admin
Site Admin
Posts: 66375
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Initialize/Terminate variables scope

Post by admin »

Difficult. Needs deeper meditation.

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: Initialize/Terminate variables scope

Post 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?
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

klownboy
Posts: 4462
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8737 at 100% 2560x1440

Re: Initialize/Terminate variables scope

Post 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

admin
Site Admin
Posts: 66375
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Initialize/Terminate variables scope

Post 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;

Post Reply