Hi, I made a somewhat working web mat addon. However the Material returns nil the first time called, and works the second. Any idea why it does not work the first time? Here's the code:
WebMats = WebMats or {}
if not file.Exists("webtextures", "DATA") then
file.CreateDir("webtextures")
end
function WebMats:Fetch(url, flags)
local uid = util.CRC(url) .. ".png"
local iMat
if file.Exists("webtextures/" .. uid, "DATA") then
iMat = Material("../data/webtextures/" .. uid, flags)
return iMat
end
http.Fetch(url, function(body, len, headers, code)
file.Write("webtextures/" .. uid, body)
iMat = Material("../data/webtextures/" .. uid, flags)
return iMat
end)
end
Its because http.Fetch is not async with its callbacks.
You'd probably want to make a callback function like so:
WebMats = WebMats or {}
if not file.Exists("webtextures", "DATA") then
file.CreateDir("webtextures")
end
function WebMats:Fetch(url, flags, callback)
local uid = util.CRC(url) .. ".png"
local iMat
if file.Exists("webtextures/" .. uid, "DATA") then
iMat = Material("../data/webtextures/" .. uid, flags)
if (callback) then
callback(iMat);
end
return iMat
end
http.Fetch(url, function(body, len, headers, code)
file.Write("webtextures/" .. uid, body)
iMat = Material("../data/webtextures/" .. uid, flags)
if (callback) then
callback(iMat);
end
return iMat
end)
end
--Example usage
/*
WebMats:Fetch("http://google.com/image.png", "noclamp", function(mat)
surface.SetDrawColor(255, 255, 255, 255);
surface.SetMaterial(mat);
surface.DrawTexturedRect(0, 0, 64, 64);
end)
*/
Sorry, you need to Log In to post a reply to this thread.