Thread Tools Display Modes
02-28-13, 05:17 PM   #1
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Disable event on click?

Hi,

I have a quick question to ask. I am not use to not using KgPanels and I am just trying to make a small addon instead with a .lua file.

This should be very simple. How do I make the Shadowed Unit Frames addon recognisable in my script since it just comes up as nil:

Code:
if ShadowUF.db:GetCurrentProfile() == "ProfileName" then
	BTL:Show()
	TL:Hide()
end
Lua error says:
attempt to index global 'ShadowUF' (a nil value)

Thank you!

_
  Reply With Quote
02-28-13, 05:39 PM   #2
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
I don't see exactly how your topic title relates to what you're asking.

Anyhow. What's probably happening is that your addon loads after Shadowed Unit Frames. You need to delay all code with references to ShadowUF until it actually exists. You can do this by registering the ADDON_LOADED event and letting your code run only once the addon parameter is what you need.

For example;

Code:
local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")
f:SetScript("OnEvent", function(self, event, addon)
	if addon ~= "ShadowedUnitFrames" then return end
	self:UnregisterEvent("ADDON_LOADED") -- Once we have the addon we need, we don't need to register anymore

	... -- Your code here
end)
I don't know what the actual addon is called so you'll have to replace that.
  Reply With Quote
02-28-13, 05:51 PM   #3
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Originally Posted by Haleth View Post
I don't see exactly how your topic title relates to what you're asking.
Damn I forgot to edit the title, sorry about that. Not sure how that got there!

Thank you so much, I will try that now!
  Reply With Quote
02-28-13, 06:08 PM   #4
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Ok my finished code is there:

Code:
local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")
f:SetScript("OnEvent", function(self, event, addon)
	if addon ~= "ShadowUF" then return end
	self:UnregisterEvent("ADDON_LOADED")
	if ShadowUF.db:GetCurrentProfile() == "MayronUI" then
		BTL:Show()
		TL:Hide()
	else
		TL:Show()
		BTL:Hide()
	end
end)
But unfortunately the:

"BTL:Show()
TL:Hide()"

part never reaches as both of the frames are showing for some reason. I had to also change "ShadowedUnitFrames" to "ShadowUF" because for some reason it came up with a lua error saying that "db" is a nil value but with that instead I do not get any errors. Not sure what that's about. I will keep up the experimenting.
  Reply With Quote
02-28-13, 06:13 PM   #5
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
It's probably being shown after you hide it.
  Reply With Quote
02-28-13, 09:07 PM   #6
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 124
Originally Posted by Mayron View Post
if ShadowUF.db:GetCurrentProfile() == "MayronUI" then
BTL:Show()
TL:Hide()
else
TL:Show()
BTL:Hide()
end
end)
[/code]

But unfortunately the:

"BTL:Show()
TL:Hide()"

part never reaches as both of the frames are showing for some reason. I had to also change "ShadowedUnitFrames" to "ShadowUF" because for some reason it came up with a lua error saying that "db" is a nil value but with that instead I do not get any errors. Not sure what that's about. I will keep up the experimenting.

Hey

Are BTL and TL variables which point to the frames somewhere else in your script? I'm just asking because if you've posted your full script, there's no way that your code is going to know what BTL and TL is.

I'd also consider registering to the "PLAYER_LOGIN" event too, just in-case your addon loads after ShadowedUF. If it does load after ShadowedUF, "ADDON_LOADED" will not catch the event (because it fired before your addon existed).


Lua Code:
  1. local shadowLoaded = false;
  2.  
  3. f:RegisterEvent("ADDON_LOADED");
  4. f:RegisterEvent("PLAYER_LOGIN");
  5.  
  6. f:SetScript("OnEvent", function(self, event, addon)
  7.        
  8.     if ( ( event == "ADDON_LOADED" ) and ( addon == "ShadowUF" ) ) then
  9.         shadowLoaded = true;
  10.     elseif  ( ( event == "PLAYER_LOGIN" ) and IsAddOnLoaded("ShadowUF") ) then
  11.         shadowLoaded = true;
  12.     end
  13.  
  14.     if ( not shadowLoaded ) then return; end
  15.  
  16.     print("ShadowUF has been loaded"); -- temporary to use as a debug tool to ensure that you're code is getting this far.
  17.  
  18.     self:UnregisterEvent("ADDON_LOADED");
  19.     self:UnregisterEvent("PLAYER_LOGIN");
  20.  
  21.     if ShadowUF.db:GetCurrentProfile() == "MayronUI" then
  22.         BTL:Show();
  23.         TL:Hide();
  24.     else
  25.         TL:Show();
  26.         BTL:Hide();
  27.     end
  28.  
  29. end)

Right enough, an easier way would be to simply add ShadowUF as a dependency in your TOC file if your code is pointless without ShadowUF (or an optional dependency if you'd still like your code to run even if ShadowUF has been disabled). The game will then try to ensure that ShadowUF is loaded prior to your addon which will remove some of the guesswork.

Using print() like the example above is a pretty handy way of working out where you're going wrong although I suspect that in the above example, if ShadowUF is loaded early on in the addon loading process, it might not print to chat even if your func does reach that line of code... but it's worth trying.

Aanson
__________________
__________________

Last edited by Aanson : 02-28-13 at 09:41 PM.
  Reply With Quote
03-01-13, 01:12 PM   #7
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Originally Posted by Aanson View Post
Hey

Are BTL and TL variables which point to the frames somewhere else in your script? I'm just asking because if you've posted your full script, there's no way that your code is going to know what BTL and TL is.
Thank you for helping but that script (even though no errors came up) did not work.

I can most the entire script the project I'm working on if that helps. Basically I want to expand on my Setup addon so that you click the install button and it uses a reflux command to change everything to "MayronUI" profile and reloads the UI. After that I need a script that if any of the addons are set to "MayronUI" profile then it hides the Setup box art with the install buttons etc and displays this new box:



Here you can enable some "bonus features" which do the following things in the annotation on the image above. I wanted to make this because its complicated for users to have to follow long steps of moving hte chat box with other addons after installing the UI so I wanted to make it easier. So far so good but the main issue is that the script does not recognise the addons loading and I really do not know what to do about it.

Your welcome to see what I've done so far and if I have done other things wrong and the work is too much then I understand.

The Addon file is here:
http://www.mediafire.com/?awc6e990yvz227y
  Reply With Quote
03-01-13, 04:55 PM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
I didn't look at your code, but I'd use the PLAYER_LOGIN event -- so that all non-LoD addons are loaded by the time it fires and, assuming they're using AceAddon's OnInitialize, have loaded and set up their profiles -- in conjunction with a saved variable for your addon; in this (fairly loose) example I've used the saved variable name MUI_INSTALLED.

Code:
local addons = {
	-- Global names of addon objects go here:
	"ShadowUF",
	"OtherAddon",
	"AnotherAddon",
}

local setupMode = "setup"

frame:SetScript("OnEvent", function(self, event, ...)
	if event == "PLAYER_LOGIN" then
		if MUI_INSTALLED then
			setupMode = nil
			-- Nothing to do. Turn everything off.
			return
		end

		for _, addon in pairs(addons) do
			if _G[addon].db:GetCurrentProfile() == "MayronUI" then
				setupMode = "bonus"
				break
			end
		end
		
		if setupMode == "setup" then
			-- Show initial setup window
		elseif setupMode == "bonus" then
			-- Show bonus setup window
		end
	end
end)

SetupExitButton:SetScript("OnClick", function(self)
	-- Do all the setup stuff
	ReloadUI()
end)

BonusExitButton:SetScript("OnClick", function(self)
	local reload
	if EnableChat:IsChecked() then -- asumes this is a CheckButton, not just a Button
		-- Setup bottom left chat box
		reload = true
	end
	if DisableClassColors:IsChecked() then
		-- Turn off class colors
		reload = true
	end
	if ClassicMode:IsChecked() then
		-- Setup classic mode
		reload = true
	end
	MUI_INSTALLED = true
	if reload then
		ReloadUI()
	else
		-- Nothing left to do. Turn everything off.
		self:Hide()
	end
end)
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
03-02-13, 04:31 AM   #9
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
wow I didn't expect all of that but thanks so much. It may take me a long time to implement it with my code as I don't know a lot about lua but hopefully your notes will help me out. I really hope I can get this working! Thank you Phanx
  Reply With Quote
03-03-13, 03:42 AM   #10
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Ok I have now implemented your code and I am not getting any lua errors but I still haven to managed to get it to work. I have not changed the buttons into checkbuttons yet as I am not sure how to do that but most importantly because when I log on, the UIParent is still showing, the "block" frame I am using for the slight transparent black background frame is missing and both the "TL" and "BTL" frames which are the setup box and bonus box is showing together and I am unsure what the issue is. So I have been trying to get that right before worrying about the checkbuttons. I thought it would have something to do with the fact that MUI_INSTALLED has not been told what it is. That part confused me but I can either paste the entire code here or you can download the MayronSetup addon with the lua file:

http://www.mediafire.com/?4mw4u7s408s6yib

Thank you for the help I really appreciate it and will of course give you credit for your work if we get it working ^^
  Reply With Quote
03-03-13, 03:44 AM   #11
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
MUI_INSTALLED should be defined as a saved variable in the TOC file for your MayronUI addon. It doesn't need to be defined in the Lua file because it's never used before your addon's ADDON_LOADED event, when the game automatically defines it as either its saved value, or as nil if you haven't loaded the addon before or haven't set the variable to anything else before.

Code:
## SavedVariables: MUI_INSTALLED
Also, your variable names are quite horrible. You're not writing a macro; you can use descriptive names instead of "TL".

Also #2, you don't need to give every object a global name -- in fact, most objects don't need one -- and global names should be descriptive enough that they both (a) identify which addon they belong to, and (b) be extremely unlikely to conflict with anything else, so "BBL" and "ExitB" aren't good global names. In your case, I don't see anything in MayronSetup.lua that actually needs a global name, so I'd suggest passing nil for that part of the CreateFrame call.

Edit #3: There are some typos in your text, eg "Shadowed Unite Frames" with an extra "e", and "reframe from changing" instead of "refrain from cchanging".

Edit #4: It is against Blizzard's addon policy to solicit donations in-game, so you should remove the part where you ask for donations and describe how to send donations. It's okay to include that on the download page or in a README file in your addon's folder, just not in-game.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.

Last edited by Phanx : 03-03-13 at 04:16 AM.
  Reply With Quote
03-03-13, 09:34 AM   #12
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Thank you for that very interesting info and for taking the time to look at my .lua file. I was actually expecting there to be much worse errors in it since its the first big lua project I've ever worked on. I've definitely learnt a lot and will correct those errors.

Also in your "frame:SetScript("OnEvent", function(self, event, ...)" part of the script, the "frame" comes up as a nil variable when I run it in wow. Is that because I am meant to rename the "frame" to any other frame that preferably is not already using an OnEvent script and which is always showing such as the "block" frame that does not disappear until the very end? Either that or some how defining "frame" as an actual variable then.


Edit: It is also saying that this line:

Code:
if _G[addon].db:GetCurrentProfile() == "MayronUI" then
that "db" is a nil value with a lua error for some reason

Last edited by Mayron : 03-03-13 at 12:00 PM.
  Reply With Quote
03-03-13, 12:49 PM   #13
ravagernl
Proceritate Corporis
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,176
Originally Posted by Mayron View Post
Also in your "frame:SetScript("OnEvent", function(self, event, ...)" part of the script, the "frame" comes up as a nil variable when I run it in wow. Is that because I am meant to rename the "frame" to any other frame that preferably is not already using an OnEvent script and which is always showing such as the "block" frame that does not disappear until the very end? Either that or some how defining "frame" as an actual variable then.
Yes, or use "local frame = CreateFrame("Frame")" and paste that above it.

Edit: It is also saying that this line:

Code:
if _G[addon].db:GetCurrentProfile() == "MayronUI" then
that "db" is a nil value with a lua error for some reason
That would probably be because "OtherAddon" or "AnotherAddon" (noted in the loop as the variable "addon") does not exist in the global environment (_G), or because it does not have a db property, or because it has a db property but the db property is not a AceDB profile database.

One way to solve it:
lua Code:
  1. local addon
  2.         for _, addonName in pairs(addons) do
  3.             addon = _G[addonName]
  4.             print(addon and addonName .. " was detected." or addonName .. " was not detected.")
  5.             if addon and addon.db and addon.db.GetCurrentProfile and addon.db.GetCurrentProfile() == "MayronUI" then
  6.                 setupMode = "bonus"
  7.                 -- Some debug here, always test!
  8.                 print(addonName .. " database was detected.")
  9.                 break
  10.         end

Last edited by ravagernl : 03-03-13 at 12:53 PM.
  Reply With Quote
03-03-13, 02:25 PM   #14
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
I've fixed everything you recommended Phanx except I have not altered the donations thing just yet as I will do that when I have the actual addon up and running because I thought it would be easier that way. All of the frames with horrible names on them such as "BTL" have been renamed to things such as "BonusTopLeft" to make it easier.

And thank you ravagernl for that but I cannot seem to get that to work. Having great difficulty.

I have uploaded the latest updated .lua file so you can have a look at where I went wrong if you like. I've made a lot of the code much easier to read hopefully. Thank you so much!

http://www.mediafire.com/?tlg83dq8sy3a854
  Reply With Quote
03-03-13, 11:00 PM   #15
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
I'm fairly certain that the global name for kgPanels is "kgPanels" with a lowercase "k", not KgPanels" with an uppercase "K".

Also:

You should parent your "top level" frames to UIParent, not WorldFrame, so that they respect the user's UI scaling settings and are correctly hidden when the UI should be hidden.

Most of your sub-frames look like they should actually just be Texture objects, not Frame objects, and for Button objects you should use SetNormalTexture instead of SetBackdrop.

Font strings in WoW are limited to around 32px in height. SetFont won't complain if you give it larger values, but WoW also won't actually render the fontstring with a larger height. Once you get over 28-36 (depending on the font file you're using) the rendered fontstring won't actually get any bigger.

Font strings default to white and center-aligned, so there's no need to explicitly set those properties.

I'd suggest keeping related sections of code together -- eg. create a button, add a font string, set scripts, create the next button, etc. instead of create all buttons, then create all font strings, then set all scripts. It's also a good idea to define objects in roughly the order they'll be displayed, so you'd want to define your title bar, then your section headings, then your section descriptions, then your exit button last.

Anyway, I ended up rewriting the whole thing:
http://pastebin.com/3TfYtMzY

Works as far as I can tell without having any of the relevant addons installed for it to do profile stuff with.

Edit: For future reference, just putting the relevant file on pastebin.com or paste.wowace.com is faster than re-uploading the whole ZIP with 6 MB of texture files.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
03-04-13, 01:28 AM   #16
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
I cannot believe you rewrite everything! wow thank you so much that looks fantastic. I didn't imagine anyone to put that much work into getting it to work I have not tested this out as I am currently at work so now I'm excited to get back home and then I will.

I just have to say though that I had the frames set to Worldframe so that I could use an event to hide the UIParent but still show the frames so that it looks liked this:



As you can see there is no UIParent so it looks much less cluttered and nicer to look at. Does your code still do this or can I switch back to using worldframe?

And I found out myself that WoW font size did not change when I went over 32 after trying to make the "Install" font 100 ^^

Thank you again for teaching me far more than I ever expected lol. You are amazing
  Reply With Quote
03-04-13, 02:46 AM   #17
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
You can switch it back to WorldFrame... but if you do, then add this at the top of the OnShow function:

Code:
UIParent:Hide()
And add a corresponding OnHide function:

Code:
MayronSetupFrame:SetScript("OnHide", function(self)
    UIParent:Show()
end)
If you want the same for the "bonus features" frame, you should add the "blackout" texture to it, too.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
03-04-13, 02:52 AM   #18
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
I don't think you can hide/show UIParent programmatically without tainting.
  Reply With Quote
03-04-13, 12:25 PM   #19
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Everything is working great except for one small issue.

When I use the addon to install the UI on one character, the SavedVariable file says "MUI_INSTALLED = true" which is set for the entire account by the looks of it and so I cannot then use the addon again on another character to set the UI up for a second.

Is there a way around this? Thank you


Also I can live without this but thought I might as well ask. When The UIParent:Hide() function fires with your event you posted I noticed that when I press the Escape key, the UIParent shows up again which I didn't think happened before but never really tested it. That to me seemed a bit odd. Is there a way to stop that from happening?

Last edited by Mayron : 03-04-13 at 12:31 PM.
  Reply With Quote
03-04-13, 12:53 PM   #20
ravagernl
Proceritate Corporis
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,176
Originally Posted by Mayron View Post
Everything is working great except for one small issue.

When I use the addon to install the UI on one character, the SavedVariable file says "MUI_INSTALLED = true" which is set for the entire account by the looks of it and so I cannot then use the addon again on another character to set the UI up for a second.

Is there a way around this? Thank you
Change "## SavedVariables: MUI_INSTALLED"
to: "## SavedVariablesPerCharacter: MUI_INSTALLED"
in the toc file.

It's that easy.

Also I can live without this but thought I might as well ask. When The UIParent:Hide() function fires with your event you posted I noticed that when I press the Escape key, the UIParent shows up again which I didn't think happened before but never really tested it. That to me seemed a bit odd. Is there a way to stop that from happening?
I do not think so, IIRC the keybinding to toggle the UI (ctrl-z by default) just toggles the visibility of the UIParent. When the UI is hidden the escape key behaves the same(UI needs to be shown if you want to see the game menu).

Another way to do this is to use an overlay texture or frame instead of hiding the entire UIParent:
lua Code:
  1. local overlay = CreateFrame("Frame", nil, UIParent)
  2. overlay:SetAllPoints() -- as big as the UIParent
  3. -- mess aaround with the following two lines to set the black overlay correctly.
  4. overlay:SetFrameStrata("HIGH")
  5. overlay:SetFrameLevel(999) -- not sure what the method is to set it to top level
  6. -- following line is optional, if you want to disable clicking the interface
  7. overlay:EnableMouse()
  8. -- uncomment the following line when you are done.
  9. -- overlay:Hide()
  10.  
  11. -- chatframe background does not have transparency, that's why it's used
  12. overlay:SetBackdrop({bgFile = [[Interface\ChatFrame\ChatFrameBackground]]})
  13. overlay:SetBackdropColor(0, 0, 0)
  14. overlay:SetAlpha(.75) -- controls how translucent the frame is.

This doesn't hide the UI, but it does give the user the attention that UI setup is needed.

Last edited by ravagernl : 03-04-13 at 02:17 PM. Reason: fixes
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Disable event on click?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off