request. IMAGE instead of USERDATA

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
whiteland92
Prole
Posts: 12
Joined: Fri Jul 04, 2014 1:30 am

request. IMAGE instead of USERDATA

Post by whiteland92 » Thu Mar 10, 2016 7:21 pm

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"))

User avatar
Nixola
Inner party member
Posts: 1940
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: request. IMAGE instead of USERDATA

Post by Nixola » Thu Mar 10, 2016 7:53 pm

You can use [wiki] love.filesystem.newFileData[/wiki] for the latter.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics

User avatar
whiteland92
Prole
Posts: 12
Joined: Fri Jul 04, 2014 1:30 am

Re: request. IMAGE instead of USERDATA

Post by whiteland92 » Thu Mar 10, 2016 8:02 pm

and so it does. thanks :D

User avatar
zorg
Party member
Posts: 2868
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: request. IMAGE instead of USERDATA

Post by zorg » Thu Mar 10, 2016 8:07 pm

For the former, using [wiki]Object:type[/wiki] instead of lua's type function would be the solution.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
whiteland92
Prole
Posts: 12
Joined: Fri Jul 04, 2014 1:30 am

Re: request. IMAGE instead of USERDATA

Post by whiteland92 » Thu Mar 10, 2016 8:14 pm

well that was fast and easier then i though it would be. thank you so much. both of you :awesome:

works like a charm :D

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

User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: request. IMAGE instead of USERDATA

Post by T-Bone » Sat Mar 12, 2016 1:25 pm

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?

User avatar
slime
Solid Snayke
Posts: 2883
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: request. IMAGE instead of USERDATA

Post by slime » Sat Mar 12, 2016 1:36 pm

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)

User avatar
whiteland92
Prole
Posts: 12
Joined: Fri Jul 04, 2014 1:30 am

Re: request. IMAGE instead of USERDATA

Post by whiteland92 » Sat Mar 12, 2016 6:36 pm

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)

User avatar
zorg
Party member
Posts: 2868
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: request. IMAGE instead of USERDATA

Post by zorg » Sat Mar 12, 2016 7:14 pm

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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: request. IMAGE instead of USERDATA

Post by Robin » Mon Mar 21, 2016 2:25 pm

You can use this function for that:

Code: Select all

function realType(obj)
    return obj.type and obj:type() or type(obj)
end
Help us help you: attach a .love.

Post Reply

Who is online

Users browsing this forum: Google [Bot], Imagic and 23 guests