Page 1 of 2
Adding An Image To A Table
Posted: Fri Dec 23, 2011 3:27 am
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.
Re: Adding An Image To A Table
Posted: Fri Dec 23, 2011 5:43 am
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
Re: Adding An Image To A Table
Posted: Fri Dec 23, 2011 7:53 am
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.
Re: Adding An Image To A Table
Posted: Fri Dec 23, 2011 3:34 pm
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.
Re: Adding An Image To A Table
Posted: Fri Dec 23, 2011 3:42 pm
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?
Re: Adding An Image To A Table
Posted: Fri Dec 23, 2011 3:49 pm
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.
Re: Adding An Image To A Table
Posted: Fri Dec 23, 2011 8:23 pm
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 :/
Re: Adding An Image To A Table
Posted: Fri Dec 23, 2011 8:35 pm
by bartbes
Well, I'm going off the code you posted, which is sane, is it not?
Re: Adding An Image To A Table
Posted: Sat Dec 24, 2011 1:26 am
by Rukiryo
That made no sense. Please only reply if you're going to help.
Re: Adding An Image To A Table
Posted: Sat Dec 24, 2011 1:32 am
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.