moho:UpdateUI()

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:

moho:UpdateUI()

Post by MehdiZangenehBar »

anyone knows why moho:UpdateUI() cause crash in dialog?
User avatar
synthsin75
Posts: 10016
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: moho:UpdateUI()

Post 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.
User avatar
MehdiZangenehBar
Posts: 54
Joined: Wed Feb 07, 2024 7:17 pm
Contact:

Re: moho:UpdateUI()

Post 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
User avatar
synthsin75
Posts: 10016
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: moho:UpdateUI()

Post 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
User avatar
MehdiZangenehBar
Posts: 54
Joined: Wed Feb 07, 2024 7:17 pm
Contact:

Re: moho:UpdateUI()

Post by MehdiZangenehBar »

brilliant! Thank You so much!
Post Reply