View Single Post
01-07-11, 10:35 PM   #4
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
Originally Posted by Akkorian View Post
Hi ChaosInc,

Putting parentheses after the function name makes Lua call the function. If you want to assign the function itself to a variable, rather than its return values, you just write the function name. If it’s a method name, rather than a standalone function, then you usually have to write it as a string, in quotes, so you can look it up in the object table later.

This would work:
Code:
-- disable mouse stuffs
cb = CreateFrame( "CHECKBUTTON", "CFM_ClickButton", CFM_Panel1, "OptionsCheckButtonTemplate" )
cb:SetPoint( "LEFT", CFM_ClickText, "RIGHT", 0, 0)
cb:SetScale( 0.75 )
cb:SetScript( "OnClick", function( self )
	if selName == nil or selName == "" then
		return print( "Please select a frame first." )
	else
		CFM_ToggleSetting( self, "disableMouse" )
	end
end )

-- code stuffs
function CFM_ToggleSetting( self, prop )
	local frame = _G[ activeProfile[ selName ].frame ]
	local func
	-- determine what function will be needed based on passed property
	if prop == "disableMouse" then
		func = "EnableMouse"
	end
	if prop == "clamp" then
		func = "SetClampedToScreen"
	end
	if self:GetChecked() == 1 then
		activeProfile[ selName ][ prop ] = true
		frame[ func ]( frame, true )
	else
		activeProfile[ selName ][ prop ] = false
		frame[ func ]( frame, false )
	end
end
But, it might be easier to just use the function name instead of some other string, like this:

Code:
CFM_ToggleSetting( self, "EnableMouse" )

function CFM_ToggleSetting( self, func )
	local frame = _G[ activeProfile ][ selName ].frame
	-- determine what function will be needed based on passed property
	if self:GetChecked() == 1 then
		activeProfile[ selName ][ func ] = true
		frame[ func ]( frame, true )
	else
		activeProfile[ selName ][ func ] = false
		frame[ func ]( frame, false )
	end
end
The latter wouldn't work since I intend mutliple buttons to use this function. Otherwise I'd just put it all into the click script like it currently is. I'm trying to reduce the amount of redundant coding that currently exists. I tried to use simply func = EnableMouse and such, but didn't work. I give your example a run and see how it works.
  Reply With Quote