Why Is This Bit of Code Not Working?


local TexturedQuad = {
	texture = surface.GetTextureID( "materials/exampleHUD/iconsvtf/heart.vtf" ),
	x = 17,
	y = ScrH() - 143,
	w = 16,
	h = 16
}

So, the following error pops up after the script is ran:


Missing Vgui material materials/exampleHUD/iconsvtf/heart.vtf


Now, usually this would be easy to fix… However, I cannot see why this isn’t working. I have the file name and I also have the path right.

Just a little more info:

  • I’ve also tried to remove the file extension in the table.
    ** These are also the famfamfam silk icons converted to vtf, by myself.
    *** This is all from local files. No servers are involved, besides the lan, pretty obvious from picture… But oh well.

Get rid of ‘materials/’
The function starts looking for materials in materials folder as it’s root.

Still not working, code is changed to the following:


local TexturedQuad = {
	texture = surface.GetTextureID( "exampleHUD/iconsvtf/heart.vtf" ),
	x = 17,
	y = ScrH() - 143,
	w = 16,
	h = 16
}

Remove the .vtf

Still getting this god damned error…

New code:


local TexturedQuad = {
	texture = surface.GetTextureID( "exampleHUD/iconsvtf/heart" ),
	x = 17,
	y = ScrH() - 143,
	w = 16,
	h = 16
}

Don’t you always need a .vmt that you use with every .vtf?

You need to create a VMT file for the VTF.

You could try this instead:



local myTexture = CreateMaterial("heartIcon", "UnlitGeneric", {
	["$basetexture"] = "exampleHUD/iconsvtf/heart",
	["$vertexcolor"] = 1,
});


Then to draw it:
(in your drawing function)



surface.SetMaterial(myTexture);
surface.SetDrawColor(Color(255, 255 255));
//Obviously change the x/y, width/height to your values..
surface.DrawTexturedRect(x, y, width, height ) 


I’ve always found that surface.SetMaterial works better than surface.SetTexture - so with your thing you could do this:



local TexturedQuad = {
	texture = Material( "exampleHUD/iconsvtf/heart.vtf" ),
	x = 17,
	y = ScrH() - 143,
	w = 16,
	h = 16
}

surface.SetMaterial(TexturedQuad.texture)
-- more drawing stuff


Not sure if that works though, but I have a feeling it would work a little bit better

Try using this code instead:
surface.SetDrawColor(255,255,255,255)
surface.SetMaterial( Material( “LOCATION OF PICTURE HERE”, “noclamp” ) )
surface.DrawTexturedRect(100,100),64,64)
surface.SetTexture(0)

Re-position it to your liking. This definitely works with .png’s. Make sure to have this is your hud draw function.

That’s pretty much exactly what I said… but you shouldn’t be running Material in a HUD draw function, since it’s pretty expensive to run it. You should cache the material first

I am kinda new to actual lua coding. Just trying to help people out. Sry :stuck_out_tongue:
So you are saying to put the surface.SetMaterial out of the drawhud function?

You should generally only be calling Material once per material.



--Outside your draw function
local mat = Material( "path", "smooth mips noclamp" )

--Inside your draw function
surface.SetMaterial( mat )


This way we aren’t recreating a new Material object for each material each frame.

Ok, thx. This thread actually helped me out too xD. I’m just wonder as to why so many people disagree when I asked a question lol.

nevermind, my question was dumb.

I’ll try this out, I’ll update you.