Image wont work in table?[Code Included]

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

Image wont work in table?[Code Included]

Post by Rukiryo »

So, I have an image and I insert it into a table, but it won't work. It lets me access everything else in the table, just not the object. Or maybe I'm doing this wrong.

main.lua

Code: Select all

require ("character.lua")--Not needed, it's currently empty
require ("platformPhysics.lua")
function love.load()
char = love.graphics.newImage("character.png")
end
AddObject(char,300,300,0,"No Status","Character")
function love.draw()
count=0
for i, v in pairs(objects) do
love.graphics.draw(v.object,v.x,v.y)--THIS IS THE ERROR. it won't accept v.object, I get the error:
--Incorrect parameter type: expected userdata
end

end

function love.update()
PlatformGravityHandler()
end
platformPhysics.lua

Code: Select all


platforms = {}--Stationary platforms that are bound to a spot unless moved by editing the x/y values (will auto update)
objects = {}--Objects that are unbound and will move as affected by gravity
--PLEASE NOTE: WHEN ADDING PLATFORMS THE CHARACTER COLLIDES WITH THE TOP OF THE IMAGE, MAKE DRAWING TOUCH THE TOP OF IMAGE

--[[
--I've set this up for when it's done and I redistribute it
xval = x position of object
yval = y position of object
gravForce = the object gravity, make 0 for floating. Gravity is in 'pixel/ms. Make negative for floating.
Gravity Notice: As of V0.1, gravity does not change speed when falling, it will fall at a constant pace.
status = apply a string to it, use for whatever, to check stuff, good as a property.
name = apply a name to it, should you want to find it later!
sx = size x(in pixels) --THIS DOES NOT SET SIZE-IT IS A REFFERENCE FOR CALCULATIONS
sy = size y(in pixels) --THIS DOES NOT SET SIZE-IT IS A REFFERENCE FOR CALCULATIONS
Note: Rotated platforms won't be accepted. 
Note: Changing a property of a platform inside the table will affect it, automatically.
]]

function AddPlatform(platform,xval,yval,status,name)
local ptable = {}
local x = xval
local y = yval
local stats = status
local pname = name

table.insert(ptable,x,"x")
table.insert(ptable,y,"y")
table.insert(ptable,stats,"status")
table.insert(ptable,pname,"name")
table.insert(ptable,platform,"platform")

table.insert(platforms,ptable)
end

function AddObject(object,xval,yval,sx,sy,gravForce,status,name)

local ptable = {object = object,x = xval,y = yval}


table.insert(objects,ptable)
end

function PlatformGravityHandler()
--This function isn't done, it just adds to the x value as a test. No errors here.


	for i, v in pairs(objects) do
	v.x = v.x + 1
	end
end
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Image wont work in table?[Code Included]

Post by tentus »

Making a new thread was unnecessary, but whatever.

From glancing over your code, i think your problems stem from a poor grasp of tables. They're way easier than you're making them. Also, you skip some parameters when you call your AddObject function, which could be destroying things without you knowing it.

I copied your code over, deleted everything unused in it, and it seems to work for me.
Kurosuke needs beta testers
Rukiryo
Prole
Posts: 13
Joined: Sun Dec 18, 2011 4:29 am

Re: Image wont work in table?[Code Included]

Post by Rukiryo »

I still get the error, saying it expected userdata. Can you post the modifications you made?
Rukiryo
Prole
Posts: 13
Joined: Sun Dec 18, 2011 4:29 am

Re: Image wont work in table?[Code Included]

Post by Rukiryo »

I figured it out! Char kept being redefined, because the love.load is always running, ahh. Is there any way to make something define itself once?
I just put this in the load

if not char then
--load
--add to objects function
end

Is there an easy way to have objects only load once?
I will want to name some objects the same thing, just use locals. That would work.
Suggestions though?
I'm somewhat experienced in lua from RBX.Lua, that I programmed as a kid.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Image wont work in table?[Code Included]

Post by Robin »

Rukiryo wrote:I figured it out! Char kept being redefined, because the love.load is always running, ahh. Is there any way to make something define itself once?
Are you calling love.load() anywhere? You shouldn't do that. Then love.load() is called only once.

EDIT: scratch that, you should move AddObject(char,300,300,0,"No Status","Character") inside love.load(), because when you call it now, "char" is undefined, leading to the error when you try to draw it.
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Image wont work in table?[Code Included]

Post by bartbes »

Stop double posting, next time you'll get an official forum warning. :x
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Image wont work in table?[Code Included]

Post by Jasoco »

Remember. You can easily edit your post. Do not double-post if you were the last person to post in a thread and a significant amount of time hasn't passed yet. i.e., if your thread's still on the first page near the top, you do not need to bump it. If you need to add something, press edit and add it to your last post. It's common forum courtesy. And this forum makes it easy to just edit.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot] and 6 guests