Preserving Variables and Not Re-Executing Code

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey for Windows 4.

Moderators: Gurus, Addon Administrators

CarlitoGil
Posts: 294
Joined: Sun Sep 07, 2008 10:46 am
Location: Dominican Republic
Contact:

Preserving Variables and Not Re-Executing Code

Post by CarlitoGil »

Hello, I've prepared a script to demonstrate a question I have:
How to preserve global variables and not execute code outside subs everytime a sub is called, without sleeping or timers?

Code: Select all

Option Explicit

Sub OnStartUp()
	Dim Menu

	Set Menu = SDB.UI.AddMenuItem(SDB.UI.Menu_TbSearch, 1, 0)
 	Menu.UseScript = Script.ScriptPath
	Menu.IconIndex = 27
	Menu.OnClickFunc = "TEST1"
	Menu.Caption = "TEST 1"

	Set Menu = SDB.UI.AddMenuItem(SDB.UI.Menu_TbSearch, 1, 0)
 	Menu.UseScript = Script.ScriptPath
	Menu.IconIndex = 27
	Menu.OnClickFunc = "TEST2"
	Menu.Caption = "TEST 2"

	Set Menu = SDB.UI.AddMenuItem(SDB.UI.Menu_TbSearch, 1, 0)
 	Menu.UseScript = Script.ScriptPath
	Menu.IconIndex = 27
	Menu.OnClickFunc = "TEST3"
	Menu.Caption = "TEST 3"
	
End Sub

msgbox("GLOBAL")

Sub TEST1(Menu) ' the "GLOBAL" message is shown everytime
	msgbox("TEST 1")
End Sub

Dim Test2Timer
Sub TEST2(Menu) ' the "GLOBAL" message is shown only the first time
	msgbox("TEST 2")
	
	Set Test2Timer = SDB.CreateTimer(1000)
	Script.RegisterEvent Test2Timer, "OnTimer", "Test2Timeout"
	Test2Timer.Enabled = True
End Sub

Sub Test2Timeout(Source)
	Test2Timer.Enabled = False
	msgbox("TEST 2 TIMEOUT")
End Sub

Dim Test3Timer
Sub TEST3(Menu) ' the "GLOBAL" message is shown the first time and when the timer is destroyed
	msgbox("TEST 3")
	
	Set Test3Timer = SDB.CreateTimer(1000)
	Script.RegisterEvent Test3Timer, "OnTimer", "Test3Timeout"
	Test3Timer.Enabled = True
End Sub

Sub Test3Timeout(Source)
	Test3Timer.Enabled = False
	Script.UnregisterEvents Test3Timer
	Set Test3Timer = Nothing
	msgbox("TEST 3 TIMEOUT")
End Sub
TEST 3 only shows the "GLOBAL" message the first time and when there are no timers.
I'd like a script to behave like TEST 3 without using a timer, is it posible?
iTunesMonkey + Acoustid – Find metadata from iTunes and Acoustid
Eyal
Posts: 3116
Joined: Sun Jun 26, 2005 9:27 am
Location: Québec

Re: Preserving Variables and Not Re-Executing Code

Post by Eyal »

What exactly is your goal?
GIL wrote:How to preserve global variables
Global variables are defined outside of subs, usually before first Sub. They retain their values until session ends.
Skins for MediaMonkey: Cafe, Carbon, Helium, Spotify, Zekton. [ Wiki Zone ].
CarlitoGil
Posts: 294
Joined: Sun Sep 07, 2008 10:46 am
Location: Dominican Republic
Contact:

Re: Preserving Variables and Not Re-Executing Code

Post by CarlitoGil »

Let's say you don't want to end the session just yet, and if you register a timer to resume the script in a little while, it won't end.
MM doesn't end the session if a timer is registered.

But I don't actually need a timer, there is an event that's going to resume the script when it's needed.
So, I want to remove the timer without ending the session.
iTunesMonkey + Acoustid – Find metadata from iTunes and Acoustid
Eyal
Posts: 3116
Joined: Sun Jun 26, 2005 9:27 am
Location: Québec

Re: Preserving Variables and Not Re-Executing Code

Post by Eyal »

By "ending the session" I meant ending MediaMonkey. Global variables will retain their values until then.

Secondly, it's not a good idea (it's against structural programming) to have code outside of subs or functions (though variables are OK).

You can also use ini or registry functions to keep variables value outside of sessions, but I don't think it's what you want to do.
Skins for MediaMonkey: Cafe, Carbon, Helium, Spotify, Zekton. [ Wiki Zone ].
CarlitoGil
Posts: 294
Joined: Sun Sep 07, 2008 10:46 am
Location: Dominican Republic
Contact:

Re: Preserving Variables and Not Re-Executing Code

Post by CarlitoGil »

Eyal wrote:By "ending the session" I meant ending MediaMonkey. Global variables will retain their values until then.
Well, I misunderstood your meaning of "ending the session".
Global variables will not retain their values until ending MM.

Code: Select all

Option Explicit

Sub OnStartUp()
	Dim Menu

	Set Menu = SDB.UI.AddMenuItem(SDB.UI.Menu_TbSearch, 1, 0)
	Menu.UseScript = Script.ScriptPath
	Menu.IconIndex = 27
	Menu.OnClickFunc = "TEST1"
	Menu.Caption = "TEST 1"

	Set Menu = SDB.UI.AddMenuItem(SDB.UI.Menu_TbSearch, 1, 0)
	Menu.UseScript = Script.ScriptPath
	Menu.IconIndex = 27
	Menu.OnClickFunc = "TEST2"
	Menu.Caption = "TEST 2"

End Sub

Dim KeepVariable

Sub TEST1(Menu)
	KeepVariable = False
	msgbox("TEST 1")
	' session "ends"
End Sub

Dim Test2Timer
Sub TEST2(Menu)
	msgbox("TEST 2")
	msgbox("alive? "& KeepVariable)
	KeepVariable = True
	
	' keep session alive
	Set Test2Timer = SDB.CreateTimer(0)
	Script.RegisterEvent Test2Timer, "OnTimer", ""
	Test2Timer.Enabled = False
End Sub

The variable that I need to keep alive has an object, so an INI won't help.
In my tests, if an object is not assigned to SDB.Objects after the script "ends", it's lost.
Unless, of course, I prevent the script from "ending" by registering a dummy timer.
This object is the one that would trigger the event that calls a sub for the script to continue, if it dies, no trigger.

There are many global variables being shared by multiple subs, and the event could be triggered thousand of times, very quickly. so saving and loading every time would be too messy.
Eyal wrote:it's not a good idea (it's against structural programming) to have code outside of subs or functions (though variables are OK).
I did not say anything about code outside of subs or functions.
iTunesMonkey + Acoustid – Find metadata from iTunes and Acoustid
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Re: Preserving Variables and Not Re-Executing Code

Post by trixmoto »

Personally I create a dictionary object and store it in SDB.Objects, then I store all my global variables as items within that dictionary. A large number of my scripts use this mechanism. And if the global variable you want to hold is an object, then yes, the way to do this is to also add this to SDB.Objects, that's what it's there for.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
CarlitoGil
Posts: 294
Joined: Sun Sep 07, 2008 10:46 am
Location: Dominican Republic
Contact:

Re: Preserving Variables and Not Re-Executing Code

Post by CarlitoGil »

I appreciate it, but I'll keep adding this

Code: Select all

Set DummyTimer = SDB.CreateTimer(0)
Script.RegisterEvent DummyTimer, "OnTimer", ""
DummyTimer.Enabled = False
It's a hack, but doesn't change the code to work, and performance wise, I think having the global variables is faster, and better looking than accessing them on SDB.Objects("Diccionary").Item("Variable") many times.
Also, is easier to share code between an Auto Script and a Search Script.
The Search Scripts keep the variables alive for the active script until the web search window is closed.

I should have put this on Wishlist, they could add something to SDB.Tools to do what my DummyTimer does.
It seems that if a large number of your scripts have to do this, it's something needed.
iTunesMonkey + Acoustid – Find metadata from iTunes and Acoustid
Post Reply