Save/Load Preferences

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
User avatar
MehdiZangenehBar
Posts: 54
Joined: Wed Feb 07, 2024 7:17 pm
Contact:

Save/Load Preferences

Post by MehdiZangenehBar »

I just implemented a simple save/load preferences functionality, but it seems dosn't work:

Code: Select all

-- **************************************************
-- General information about this script
-- **************************************************

ScriptName = "TestScript"

TestScript = {}

function TestScript:Name()
	return 'Name'
end

function TestScript:Version()
	return 'Version'
end

function TestScript:UILabel()
	return 'UILabel'
end

function TestScript:Creator()
	return 'Creator'
end

function TestScript:Description()
	return 'Description'
end


-- **************************************************
-- Is Relevant / Is Enabled
-- **************************************************

function TestScript:IsRelevant(moho)
	return true
end

function TestScript:IsEnabled(moho)
	return true
end

-- **************************************************
-- Variables
-- **************************************************

TestScript.boolean1 = false

-- **************************************************
-- Prefs
-- **************************************************

function TestScript:LoadPrefs(prefs)
	self.boolean1 = prefs:GetBool("TestScript.boolean1", false)
end

function TestScript:SavePrefs(prefs)
	prefs:SetBool("TestScript.boolean1", self.boolean1)
end

function TestScript:ResetPrefs()
	self.boolean1 = false
end

-- **************************************************
-- Keyboard/Mouse Control
-- **************************************************

function TestScript:OnMouseDown(moho, mouseEvent)
end

-- **************************************************
-- Tool Panel Layout
-- **************************************************

TestScript.BOOLEAN_1 = MOHO.MSG_BASE

function TestScript:DoLayout(moho, layout)
	self.boolean1Checkbox = LM.GUI.CheckBox('Boolean 1', self.BOOLEAN_1)
	layout:AddChild(self.boolean1Checkbox, LM.GUI.ALIGN_LEFT, 0)
end

function TestScript:UpdateWidgets(moho)
	TestScript.boolean1Checkbox:SetValue(self.boolean1)
end

function TestScript:HandleMessage(moho, view, msg)
	if msg == self.BOOLEAN_1 then
		print('Message BOOLEAN_1 received')
	else
		
	end
end
User avatar
KuzKuz
Posts: 506
Joined: Mon Aug 19, 2013 5:12 pm
Location: Ukraine

Re: Save/Load Preferences

Post by KuzKuz »

It appears you've used the script builder from https://mohoscripting.com/, but you forgot to assign the checkbox value to a variable in the HandleMessage function.
User avatar
MehdiZangenehBar
Posts: 54
Joined: Wed Feb 07, 2024 7:17 pm
Contact:

Re: Save/Load Preferences

Post by MehdiZangenehBar »

You right, now works correctly, Thanks!

Code: Select all

function TestScript:HandleMessage(moho, view, msg)
	if msg == self.BOOLEAN_1 then
		print('Message BOOLEAN_1 received')
		TestScript.boolean1 = TestScript.boolean1Checkbox:Value()
	else
		
	end
end
Post Reply