Thread Tools Display Modes
04-28-23, 08:56 AM   #1
AeroMaxxD
An Aku'mai Servant
Join Date: Dec 2022
Posts: 33
Attempt to call global a nil value?

Would anyone happen to know why self would be a nil value?

This has been working perfectly fine for a few days, but I must have broken it somehow, I stripped the code right back to try and hung down what I broke, but I still get the error.

Code:
1x [string "*OnloadTest.xml:25_OnLoad"]:1: attempt to call global 'AMD_OnLoad' (a nil value)
[string "*OnloadTest.xml:25_OnLoad"]:1: in function <[string "*OnloadTest.xml:25_OnLoad"]:1>

Locals:
self = AMD_TabOnCharacterFrame {
 0 = <userdata>
}
(*temporary) = nil
(*temporary) = AMD_TabOnCharacterFrame {
 0 = <userdata>
}
(*temporary) = "attempt to call global 'DGT_OnLoad' (a nil value)"

Lua Code:
  1. local TabName="AMD";
  2. local TabID=CharacterFrame.numTabs+1;
  3. local Tab=CreateFrame("Button", "$parentTab"..TabID, CharacterFrame, "CharacterFrameTabTemplate", TabID);
  4. PanelTemplates_SetNumTabs(CharacterFrame, TabID);
  5. Tab:SetPoint("LEFT", "$parentTab"..(TabID-1), "RIGHT", -16, 0);
  6. Tab:SetText(TabName);
  7. -- Tab:SetID(TabID);
  8.  
  9. tinsert(CHARACTERFRAME_SUBFRAMES, "AMD_TabOnCharacterFrame");
  10.  
  11. hooksecurefunc("CharacterFrameTab_OnClick", function(self, button)
  12.     if self:GetID() == TabID then
  13.         ToggleCharacter("AMD_TabOnCharacterFrame")
  14.     end
  15. end)
  16.  
  17. function AMD_OnLoad(self)
  18.     print("AddOn loaded")
  19. end

Code:
<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/ http://wowprogramming.com/FrameXML/UI.xsd">
	<Frame name="AMD_TabOnCharacterFrame" parent="CharacterFrame" frameStrata="HIGH" enableMouse="true" movable="true" setallpoints="true" id="4">
		<Size x="206" y="580"/>
		<Frames>
			<Frame name="$parent_History" parent="AMD_TabOnCharacterFrame">
				<Size x="300" y="325"/>
				<Anchors>
					<Anchor point="CENTER" relativePoint="CENTER" relativeTo="CharacterFrame"/>
				</Anchors>
				<Layers>
					<Layer level="OVERLAY">
						<FontString name="History_Title" parentKey="History_title" inherits="GameFontNormal">
							<Color r="1" g="1" b="1" a="1"/>
							<Anchors>
								<Anchor point="TOP">
									<Offset x="0" y="20"/>
								</Anchor>
							</Anchors>
						</FontString>
					</Layer>
				</Layers>
			</Frame>
		</Frames>
		<Scripts>
			<OnLoad>
				AMD_OnLoad(self)
			</OnLoad>
		</Scripts>
	</Frame>
</Ui>

Last edited by AeroMaxxD : 04-28-23 at 09:00 AM.
  Reply With Quote
04-28-23, 09:17 AM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
It has to do with the way the files are ordered in the .toc (again)

We changed it to XML first so the .lua code could "see" AMD_TabOnCharacterFrame

Now, when it's created, you're asking AMD_TabOnCharacterFrame to call a function in your .lua (which hasn't been loaded yet)

Instead of using an OnLoad script in the XML just do whatever you were going to do to AMD_TabOnCharacterFrame in the .lua.

Lua Code:
  1. hooksecurefunc("CharacterFrameTab_OnClick", function(self, button)
  2.     if self:GetID() == TabID then
  3.         ToggleCharacter("AMD_TabOnCharacterFrame")
  4.     end
  5. end)
  6. --[[
  7. function AMD_OnLoad(self)
  8.     print("AddOn loaded")
  9. end
  10. ]]--
  11.  
  12. -- Instead of the AMD_OnLoad function:
  13. AMD_TabOnCharacterFrame_NextButton:SetText("OnLoad Change")

Depending on what the end goal is there might be a better alternative but this should work for now.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
04-28-23, 09:27 AM   #3
AeroMaxxD
An Aku'mai Servant
Join Date: Dec 2022
Posts: 33
Actually, it's ok I remember what I had changed, I put the XML file before the LUA file in the TOC file.
  Reply With Quote
04-28-23, 09:44 AM   #4
AeroMaxxD
An Aku'mai Servant
Join Date: Dec 2022
Posts: 33
I'm not sure I understood that.

What is "OnLoad Change" is that the name of a function?

I'm getting the error "attempt to call method 'SetText' (a nil value)".

I'm using this code however, slightly different from yours.

Lua Code:
  1. AMD_TabOnCharacterFrame:SetText("OnLoad Change")

Originally Posted by Fizzlemizz View Post
Lua Code:
  1. AMD_TabOnCharacterFrame_NextButton:SetText("OnLoad Change")
  Reply With Quote
04-28-23, 10:06 AM   #5
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
Originally Posted by AeroMaxxD View Post
I'm not sure I understood that.

What is "OnLoad Change" is that the name of a function?
Just changing the text of the NextButton to show something happend after the frame was created (what you "might" have previously done in your OnLoad function)

Originally Posted by AeroMaxxD View Post
I'm getting the error "attempt to call method 'SetText' (a nil value)".

I'm using this code however, slightly different from yours.

Lua Code:
  1. AMD_TabOnCharacterFrame:SetText("OnLoad Change")
Because AMD_TabOnCharacterFrame doesn't have/isn't a FontString which is why I used the AMD_TabOnCharacterFrame_NextButton but I might have the name wrong or maybe you've removed the button...

I don't know what your code looks once you've made changes so I'm just guessing what might work.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
04-28-23, 10:48 AM   #6
AeroMaxxD
An Aku'mai Servant
Join Date: Dec 2022
Posts: 33
Ah ok sure I understand now, yeah I removed the button and pretty much everything as I was trying to figure out what I had broken.

If what was in my Onload function was a script that prints out a table into the frame can I not reference an onload function in lua?
  Reply With Quote
04-28-23, 11:06 AM   #7
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
Your OnLoad function was global so it can be called pretty much anywhere. The only proviso being that the file containing the function has to be loaded before the function is used (same goes for within in the same .lua file, a function (variable etc.) must be defined before it is used.)
Lua Code:
  1. MyFunction() -- error
  2. function MyFunction()
  3.     print("This is MY function!")
  4. end

Lua Code:
  1. function MyFunction()
  2.     print("This is MY function!")
  3. end
  4. MyFunction() -- no error
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
04-28-23, 11:32 AM   #8
AeroMaxxD
An Aku'mai Servant
Join Date: Dec 2022
Posts: 33
Oh sorry what I meant was how to change this:

Code:
<Scripts>
	<OnLoad>
		AMD_OnLoad(self)
		self:Hide()
	</OnLoad>
</Scripts>
into lua code?

Like maybe
Lua Code:
  1. AMD_TabOnCharacterFrame:OnLoad(AMD_OnLoad(self));
  Reply With Quote
04-28-23, 11:44 AM   #9
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
You don't need
Code:
AMD_TabOnCharacterFrame:OnLoad(AMD_OnLoad(self));
but if you wanted to add a method to the frame you could use a Mixin or
Lua Code:
  1. function AMD_TabOnCharacterFrame:OnLoad()
  2.     print(self:GetName(), "I'm loaded now!")
  3. end
  4.  
  5. AMD_TabOnCharacterFrame:OnLoad()

Adding an OnLoad function in lua to AMD_TabOnCharacterFrame doesn't really make sense in your case as it's already been loaded so, just using:
Lua Code:
  1. print(AMD_TabOnCharacterFrame:GetName(), "I'm loaded now!")

instead of adding the function and making the call would do the same.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 04-28-23 at 11:55 AM.
  Reply With Quote
04-30-23, 02:39 AM   #10
AeroMaxxD
An Aku'mai Servant
Join Date: Dec 2022
Posts: 33
Oh but my onload function updates some stuff.

Lua Code:
  1. function AMD_OnLoad(self)
  2.     print("AMD: AddOn loaded")
  3.  
  4.     AMD_TabOnCharacterFrame:RegisterEvent("PLAYER_ENTERING_WORLD");
  5.     AMD_TabOnCharacterFrame:RegisterEvent("VARIABLES_LOADED");
  6.  
  7.     AMD_TabOnCharacterFrame:SetScript(
  8.         "OnEvent", function(self, event, ...)
  9.             print("DEBUG: Event:", event); -- For debugging
  10.  
  11.             if (event == "VARIABLES_LOADED") then
  12.                 if AMD_CharacterSavedVariables == nil then
  13.                     print("AMD_CharacterSavedVariables : ", AMD_CharacterSavedVariables );
  14.  
  15.                     -- No Saves for this character found
  16.                     AMD_CharacterSavedVariables = {};
  17.                 end
  18.  
  19.                 if AMD_SavedVariables == nil then
  20.                     print("AMD_SavedVariables: ", AMD_SavedVariables);
  21.  
  22.                     -- No Saves found
  23.                     AMD_SavedVariables = {};
  24.  
  25.                     AMD_OpagueBackgroundCheckbox:SetChecked(true);
  26.                     AMD_DisplayFormatIconRadioButton:SetChecked(true);
  27.                 else
  28.                     print("AMD_SavedVariables: ", AMD_SavedVariables);
  29.  
  30.                     AMD_OpaqueBackgroundCheckbox:SetChecked(AMD_SavedVariables["OpaqueBackground"]);
  31.                     if (AMD_SavedVariables["DisplayFormat"] == true) then
  32.                         AMD_DisplayFormatIconRadioButton:SetChecked(true);
  33.                         AMD_DisplayFormatTextRadioButton:SetChecked(false);
  34.                     else
  35.                         AMD_DisplayFormatIconRadioButton:SetChecked(false);
  36.                         AMD_DisplayFormatTextRadioButton:SetChecked(true);
  37.                     end
  38.                 end
  39.  
  40.                 if (AMD_OpaqueBackgroundCheckbox:GetChecked() == true) then
  41.                     AMD_TabOnCharacterFrame:EnableDrawLayer("BORDER");
  42.                 else
  43.                     AMD_TabOnCharacterFrame:DisableDrawLayer("BORDER");
  44.                 end
  45.             end
  46.  
  47.             if (event == "PLAYER_ENTERING_WORLD") or (event == "PLAYER_MONEY") then
  48.                 AMD_UpdateScreen(event);
  49.             end
  50.         end
  51.     )
  52. end

Last edited by AeroMaxxD : 04-30-23 at 03:00 AM.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Attempt to call global a nil value?


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