Page 1 of 1

Image wont work in table?[Code Included]

Posted: Sat Dec 24, 2011 6:30 pm
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

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

Posted: Sat Dec 24, 2011 7:23 pm
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.

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

Posted: Sat Dec 24, 2011 8:47 pm
by Rukiryo
I still get the error, saying it expected userdata. Can you post the modifications you made?

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

Posted: Sat Dec 24, 2011 8:52 pm
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.

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

Posted: Sat Dec 24, 2011 9:11 pm
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.

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

Posted: Tue Dec 27, 2011 1:42 pm
by bartbes
Stop double posting, next time you'll get an official forum warning. :x

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

Posted: Tue Dec 27, 2011 7:12 pm
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.