Hello guys. I would like download image from server and then set it as texture of sprite. Download image from server is pretty easy, here is part of code:
http.request(url, "GET", function(self, id, res)
if res.status ~= 200 and res.status ~= 304 then
print("Unable to get image", res.response)
return
end
local img = image.load(res.response)
if not img then
print("Unable to load image")
return
end
The problem is that i do not know what to do next. I have tried something like this:
resource.set_texture( go.get("#sprite", "texture0"), some header, img.buffer )
but img.buffer is in string format. So my question is that is there some easy workaround to download image from server and set it as texture of sprite?
Thanks
Thank you @britzl, but unfortunately i have already tried this extension without success. Maybe i just srewed up so i will adress next questions to creator of extension. Thanks a lot
local function load_and_set_texture(url)
http.request(url, "GET", function(self, id, res)
if res.status ~= 200 and res.status ~= 304 then
print("Unable to get image", res.response)
return
end
local img = image.load(res.response)
if not img then
print("Unable to load image")
return
end
local img_resource = imageloader.load{data = img.buffer}
pprint(img_resource)
resource.set_texture( go.get("#sprite", "texture0"), img_resource.header, img_resource.buffer )
end)
end
Wow @britzl that worked like a charm. Thank you for fast reply. If anyone have hard time with this problem here is the final code of function that works:
local function load_and_set_texture(url)
http.request(url, "GET", function(self, id, res)
if res.status ~= 200 and res.status ~= 304 then
print("Unable to get image", res.response)
return
end
local img_resource = imageloader.load{data = res.response}
resource.set_texture( go.get("#sprite", "texture0"), img_resource.header, img_resource.buffer )
end)
end