Thread Tools Display Modes
03-15-14, 12:34 PM   #1
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Is there a way to query if a texture is available?

Do you know a way to query if a special texture (specified by a texture path) is available or not?

I'm almost sure that it is not possible. But ... well ... you never know ...

Thank you.
  Reply With Quote
03-15-14, 12:49 PM   #2
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
I think that :SetTexture returns true if the texture was found. It returns true under some circumstances, at least. Not 100% sure that it's that.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
03-15-14, 01:00 PM   #3
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Originally Posted by Lombra View Post
I think that :SetTexture returns true if the texture was found. It returns true under some circumstances, at least. Not 100% sure that it's that.
Thank you. Unfortunaly as far as I experienced it SetTexture always returns 1 ... if the texture path exists or not. :/

[e]
SetTexture("") returns nil. Whatever this is good for.
  Reply With Quote
03-15-14, 02:25 PM   #4
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
I think when they introduced the streaming client they broke SetTexture's ability to determine if a texture exists.

Anyway, I developed a way to do this a while back for a map project I was working on. As long as the dimensions of the texture you're checking aren't 8x8 you can use it.

It works by taking advantage of the fact that setting a texture's size to 0 causes it to default to the real texture's actual dimensions, if it fails to load the texture then it falls back on the green "missing" texture which seems to be 8x8.

If you need to check a lot of textures at once you'll need to make some sort of queue since textures take time to load if they aren't cached. You can use an OnUpdate script or frames with OnSizeChanged scripts, whatever's more convenient for your situation.

Lua Code:
  1. function TestTexture()
  2.     local f = CreateFrame('frame') -- Don't create a new frame for every texture, this is just an example
  3.     local tx = f:CreateTexture()
  4.     tx:SetPoint('CENTER', WorldFrame) -- The texture has to be "visible", but not necessarily on-screen (you can also set its alpha to 0)
  5.     f:SetAllPoints(tx)
  6.     f:SetScript('OnSizeChanged', function(self, width, height)
  7.         local size = format('%.0f%.0f', width, height) -- The floating point numbers need to be rounded or checked like "width < 8.1 and width > 7.9"
  8.         if size == '88' then
  9.             print(tx:GetTexture(), "doesn't exist or can't be determined")
  10.         else
  11.             print(tx:GetTexture(), "exists")
  12.         end
  13.     end)
  14.     tx:SetTexture('interface/icons/inv_mushroom_11')
  15.     tx:SetSize(0,0) -- Size must be set after every SetTexture
  16. end
Keep in mind if the texture gets parented to another frame its size is going to get scaled.
  Reply With Quote
03-15-14, 02:44 PM   #5
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,359
That's a very clever workaround Semlar.
  Reply With Quote
03-15-14, 10:34 PM   #6
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Wow. That's a fantastic idea. Thanks a lot sir.
  Reply With Quote
07-21-16, 07:37 PM   #7
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
This method still works however the size is 1x1 pixel now. Which is a lot better since a proper texture can't be this size. So:

Lua Code:
  1. if size == '11' then
  Reply With Quote
11-17-20, 11:00 PM   #8
TeHypno
A Defias Bandit
Join Date: Jan 2010
Posts: 2
Method I'm using in 9.0.2
Lua Code:
  1. local f = CreateFrame('frame')
  2. local t = f:CreateTexture()
  3. function TextureExists(path)
  4.     t:SetTexture("?")
  5.     t:SetTexture(path)
  6.     return (t:GetTexture() ~= "?")
  7. end
  Reply With Quote

WoWInterface » Developer Discussions » Graphics Help » Is there a way to query if a texture is available?

Thread Tools
Display Modes

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