Page 1 of 2
request. IMAGE instead of USERDATA
Posted: Thu Mar 10, 2016 7:21 pm
by whiteland92
if you are using "type(image)" it return "userdata", would it be possible change it to "image" ?
and is it possible to load images from a url, without saving it to your pc?
Code: Select all
local http = require"socket.http"
http.request{
url = "http://i.imgur.com/YGmjfbG.jpg",
sink = function(chunk, err)
if chunk then
f = love.filesystem.newFile("cat.png")
f:open('a')
f:write(chunk)
f:close()
end
return 1
end
}
error(type(love.graphics.newImage("cat.png")),2,love.filesystem.remove("cat.png"))
Re: request. IMAGE instead of USERDATA
Posted: Thu Mar 10, 2016 7:53 pm
by Nixola
You can use [wiki] love.filesystem.newFileData[/wiki] for the latter.
Re: request. IMAGE instead of USERDATA
Posted: Thu Mar 10, 2016 8:02 pm
by whiteland92
and so it does. thanks
Re: request. IMAGE instead of USERDATA
Posted: Thu Mar 10, 2016 8:07 pm
by zorg
For the former, using [wiki]Object:type[/wiki] instead of lua's type function would be the solution.
Re: request. IMAGE instead of USERDATA
Posted: Thu Mar 10, 2016 8:14 pm
by whiteland92
well that was fast and easier then i though it would be. thank you so much. both of you
works like a charm
Code: Select all
local http = require"socket.http"
local data = love.graphics.newImage( love.filesystem.newFileData( http.request("http://i.imgur.com/YGmjfbG.jpg") , "" , "file" ) )
error(data:type(),2)
--> Error: Image
Re: request. IMAGE instead of USERDATA
Posted: Sat Mar 12, 2016 1:25 pm
by T-Bone
One thing that would be useful however, was if Lua's error messages would say the Löve type name it was expecting. So if you did
Code: Select all
love.graphics.draw("cat.png", 10, 10)
You'd get an error saying "expected Drawable, got string" instead of the current "expected Userdata, got string".
Maybe that'd be very hard to implement?
Re: request. IMAGE instead of USERDATA
Posted: Sat Mar 12, 2016 1:36 pm
by slime
It already does that:
Code: Select all
love.graphics.draw("cat.png", 10, 10)
LÖVE wrote:Error: main.lua:1: bad argument #1 to 'draw' (Drawable expected, got string)
Re: request. IMAGE instead of USERDATA
Posted: Sat Mar 12, 2016 6:36 pm
by whiteland92
one problem i found is that if you are using Löve ":type()" on string,number,table.... it crashes the program.
example
Code: Select all
local text = "Hello World!"
local textType = text:type()
-- this will fail and "blue screen" --> Main.lua 2: attempt to call method 'type' (a nil value)
Re: request. IMAGE instead of USERDATA
Posted: Sat Mar 12, 2016 7:14 pm
by zorg
That's to be expected since löve doesn't modify the metatables for basic types (if they have/support one anyway), so it does not add type to them as a callable function.
Re: request. IMAGE instead of USERDATA
Posted: Mon Mar 21, 2016 2:25 pm
by Robin
You can use this function for that:
Code: Select all
function realType(obj)
return obj.type and obj:type() or type(obj)
end