Page 1 of 1

moho:UpdateUI()

Posted: Sun May 05, 2024 10:08 pm
by MehdiZangenehBar
anyone knows why moho:UpdateUI() cause crash in dialog?

Re: moho:UpdateUI()

Posted: Sun May 05, 2024 11:01 pm
by synthsin75
If memory serves, I think it's because it sets up an infinite loop with the dialog's update widgets function.
It's been quite a while since I've run into though.

Re: moho:UpdateUI()

Posted: Sun May 05, 2024 11:17 pm
by MehdiZangenehBar
NO, just a simple example that shows the problem:

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
-- **************************************************

local dialog_table = {}

-- **************************************************
-- Events
-- **************************************************

function TestScript:OnMouseDown(moho, mouseEvent)
end

function TestScript:DoLayout(moho, layout)
	dialog_table.moho = moho

	local dialog = LM.GUI.SimpleDialog('Dialog', dialog_table)
	local dialog_layout = dialog:GetLayout()
	local button = LM.GUI.Button('Update', MOHO.MSG_BASE)
	dialog_layout:AddChild(button, LM.GUI.ALIGN_FILL, 0)

	local popup = LM.GUI.PopupDialog('Popup', true, 0)
	popup:SetDialog(dialog)
	layout:AddChild(popup, LM.GUI.ALIGN_LEFT, 0)
end

function dialog_table:HandleMessage(msg)
	--self.moho:UpdateUI() -- cause crash
end

Re: moho:UpdateUI()

Posted: Sun May 05, 2024 11:34 pm
by synthsin75
You can't call functions with self.moho outside of dolayout, as that only refers to the state of moho when the dialog is launched (there's no longer an actual object to be updated).
You need to use the script helper for dialog functions that don't take moho as an argument: https://mohoscripting.com/classes/ScriptInterfaceHelper

Try this instead:

Code: Select all

function dialog_table:HandleMessage(msg)
	local helper = MOHO.ScriptInterfaceHelper:new_local()
	local moho = helper:MohoObject()
	moho:UpdateUI() -- cause crash
	helper:delete()
end

Re: moho:UpdateUI()

Posted: Sun May 05, 2024 11:46 pm
by MehdiZangenehBar
brilliant! Thank You so much!