Adding An Image To A Table

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.
Rukiryo
Prole
Posts: 13
Joined: Sun Dec 18, 2011 4:29 am

Adding An Image To A Table

Post by Rukiryo »

So, I'm fairly new to this, and I'm making a system to store values of objects. I want to insert the image into a table, with the key "object". I made a function that adds it into a table. So, basically, here.

Code: Select all

objectimage = --the loaded image

table = {object = objectimage}

When I go through the table inside my draw function..

function love.draw()
for i, v in pairs(table) do
love.graphics.draw(v.object,300,300)
end
end

--It doesn't work. 
The error I get is:
Incorrect Paramater Type: (expected userdata)

So, I'm not sure why this is happening. I'm new to this language. Help appreciated alot!
Also, this is an example. The real code has got alot in it, as I'm making a platformer engine, but this is the issue. The image wont be called upon by the key I use for it, I get that error.
User avatar
ivan
Party member
Posts: 1918
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Adding An Image To A Table

Post by ivan »

There's a couple of things wrong with your code.
First, you probably don't want to use the global variable "table" because you'll overwrite your reference to functions like "table.remove" "table.insert" and so on.
Secondly, your code creates just one table and attempts to iterate it.
I would suggest something like:

Code: Select all

objects = {}
objects[1] = {image = objectimage, x = 300, y = 300}
objects[2] = {image = objectimage2, x = 200, y = 200}

function love.draw()
  for i, v in pairs(objects) do
    love.graphics.draw(v.image,v.x,v.y)
  end
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Adding An Image To A Table

Post by Robin »

You also might want to use ipairs instead of pairs, if you want to treat the image table as a list. Especially if you want to impose a certain order in what is drawn over what.
Help us help you: attach a .love.
Rukiryo
Prole
Posts: 13
Joined: Sun Dec 18, 2011 4:29 am

Re: Adding An Image To A Table

Post by Rukiryo »

Okay, I'll try this out. And don't worry about my table name, I was just doing that as an example. I think I get your method now, I'll try it.
Rukiryo
Prole
Posts: 13
Joined: Sun Dec 18, 2011 4:29 am

Re: Adding An Image To A Table

Post by Rukiryo »

So, your ways to manage my table was nicer, but it didn't answer my question, of why it won't let me store a loaded image in the table. I want to use the key "object" but it always tells me the error I posted when I try to draw it from the table as table.objects (TABLE ISN'T NAMED TABLE, DON'T WORRY). Any ideas?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Adding An Image To A Table

Post by bartbes »

First, don't double post.

Then, as robin said, it's because you're acting like you have a table of tables, where you have not. table = {{object = some_image}} should work.
Rukiryo
Prole
Posts: 13
Joined: Sun Dec 18, 2011 4:29 am

Re: Adding An Image To A Table

Post by Rukiryo »

I explained this terribly..gah..I appologise..I'm new to foruming..
So, I have a table, that I put other tables inside. Each added table has the object and values for it, like x and y. When I say tablename.x, and y, it always works, and I can print the correct number, but when I use .object, that's defined the same was as x and y, it won't work :/
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Adding An Image To A Table

Post by bartbes »

Well, I'm going off the code you posted, which is sane, is it not?
Rukiryo
Prole
Posts: 13
Joined: Sun Dec 18, 2011 4:29 am

Re: Adding An Image To A Table

Post by Rukiryo »

That made no sense. Please only reply if you're going to help.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Adding An Image To A Table

Post by Robin »

If you want help, show your code. What you're saying is happening is impossible. We can't help you unless you show what you're doing, instead of just telling us.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot], steVeRoll and 2 guests