WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   AddOn Help/Support (https://www.wowinterface.com/forums/forumdisplay.php?f=3)
-   -   New to Addons : Issue with SetTexture (https://www.wowinterface.com/forums/showthread.php?t=59761)

TJones1467 01-14-24 01:08 PM

New to Addons : Issue with SetTexture
 
I am new to LUA and creating addons, but I am not new to programming in general. I am working on an extremely simple addon to begin to figure things out. All I am trying to do at the moment is create a big red box in the middle of the game screen. Here is the code I have.

Code:

SLASH_RELOADUI1 = "/rl"
SlashCmdList.RELOADUI = ReloadUI

SLASH_FRAMESTK1 = "/fs"
SlashCmdList.FRAMESTK = function()
    LoadAddOn("Blizzard_DebugTools")
    FrameStackTooltip_Toggle()
end

local testFrame = CreateFrame("Frame", testFrame, UIParent)
testFrame:SetPoint("CENTER")
testFrame:SetSize(300,300)

local testFrame.texture = testFrame:CreateTexture()
testFrame.texture:SetAllPoints(testFrame)
testFrame.texture:SetTexture(255,0,0,0.5)

The first two slash commands are (probably obviously) just to help reload the UI and open the frame stack.

As far as I can tell, this should work. When I take a look at the frame stack, the frame is there but it isn't showing anything. I've included a link below to a screenshot of what I see with frame stack activated. I have bug grabber installed and it shows no errors.

https://www.dropbox.com/scl/fi/v9q5h...f5k11dnwa&dl=0

If anyone could point out what I am doing wrong here, I would greatly appreciate it.

Fizzlemizz 01-14-24 02:34 PM

Install BugGrabber and BugSack to help with debugging. They work together.

You should be getting an error
Code:

local testFrame = CreateFrame("Frame", testFrame, UIParent)
is fine

Code:

local testFrame.texture = testFrame:CreateTexture()
is not fine. This second local is trying to create a new testFrame variable and .texture is trying to assume it is already a table (which it isn't).

Use:
Code:

testFrame.texture = testFrame:CreateTexture()
The first testFrame is a table (because frames are tables) so this should work.

That and
Code:

testFrame.texture:SetColorTexture(1,0,0,0.5)
SetTexture sets the texture file (name on disk) or file ID and WoW colours are 0 to 1. SetColorTexture creates a solid texture and colours it in one step.

TJones1467 01-14-24 03:44 PM

so, just to make sure I'm understanding this correctly. Please excuse my .Net (i know.. i know..) terminology.

the syntax testFrame.texture = testFrame:CreateTexture() is using the texture property of the testFrame (table) and then creating the texture to be applied to that property. is that right?

If that is correct, then I completely understand why it wasn't working. Thank you!

Fizzlemizz 01-14-24 05:21 PM

Any time you see local in front of a variable name, you're creating a new variable (type to be determined on the other side of the =).

Code:

local testFrame = CreateFrame("Frame", testFrame, UIParent)
Creates a new Frame and assigns it to the testFrame variable of type table (as previously noted, frames are tables)

Lua Code:
  1. testFrame.texture = testFrame:CreateTexture()
Creates a new texture Region and assigns it the texture key of testFrame. Same as:
Lua Code:
  1. testFrame["texture"] = testFrame:CreateTexture()

But you can't add a key to a table that hasn't been created which is what
Code:

local testFrame.texture = testFrame:CreateTexture()
was trying to do (testFrame being nil at the time you get to the =).

dragonflyy 01-23-24 10:17 PM

Quote:

Originally Posted by TJones1467 (Post 343172)
so, just to make sure I'm understanding this correctly. Please excuse my .Net (i know.. i know..) terminology.

the syntax testFrame.texture = testFrame:CreateTexture() is using the texture property of the testFrame (table) and then creating the texture to be applied to that property. is that right?

If that is correct, then I completely understand why it wasn't working. Thank you!

local is part of the declaration, since you are familiar with .net this would be the same as trying to declare a variable twice with a type set, ie:

string value = "Something"
(some other code)
string value = "Nothing"

This would throw an exception, whereas if you wanted to change the value it would look more like:
string value = "Something"
(some other code)
value = "Nothing"

However, in Lua, variables are not typed, so we just declare if it's a local. If you don't add the local then it defaults as a global and is accessible inside all of WoW

ParrEsold 01-25-24 05:01 AM

Is your understanding of the syntax "testFrame.texture = testFrame:CreateTexture()" accurate, where it utilizes the texture property of the testFrame (table) and creates the texture to be applied to that property?

Fizzlemizz 01-25-24 09:15 AM

The .texture part is just making a table key, it could be anything. The :CreateTexture() creates the texture region and by using testFrame:CreateTexture() just means that testFrame will automatically be the parent of the new texture.

Code:

testFrame.SomethingToPutAPrettyPictureIn = testFrame:CreateTexture()
Same as
Code:

testFrame["SomethingToPutAPrettyPictureIn"] = testFrame:CreateTexture()
I get the feeling these days that some of the responses/questions in response seem like AI in training.


All times are GMT -6. The time now is 10:52 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI