Page 1 of 1
-
Posted: Sun Jun 26, 2011 3:09 am
by ruarai
-
Re: Creating a function that will add to a table
Posted: Sun Jun 26, 2011 3:22 am
by TechnoCat
Code: Select all
function newobject(name,world,m,x,y,r,width,height,angle)
objects.name = {}
objects.name.body = love.physics.newBody(world, x, y, m, r)
objects.name.shape = love.physics.newRectangleShape(objects.name.body, 0, 0, width, height, angle) --anchor the shape to the body, and make it a width of 650 and a height of 50
end
Here's your problem.
objects.name.body is literally objects["name"]["body"]
try objects[name] instead
Re: Creating a function that will add to a table
Posted: Sun Jun 26, 2011 3:28 am
by ruarai
-
Re: Creating a function that will add to a table
Posted: Sun Jun 26, 2011 3:33 am
by TechnoCat
You're still using objects.name in objects.name.shape
objects[name].shape
Re: Creating a function that will add to a table
Posted: Sun Jun 26, 2011 3:52 am
by ruarai
-
Re: Creating a function that will add to a table
Posted: Sun Jun 26, 2011 4:32 am
by ivan
You are missing the line "objects[name] = {}":
Code: Select all
function newobject(name,world,m,x,y,r,width,height,angle)
objects[name] = {}
objects[name] = love.physics.newBody(world, x, y, m, r)
objects[name].shape = love.physics.newRectangleShape(objects.name.body, 0, 0, width, height, angle)
end
Also, it would be a good idea to put an assert at the beginning of the function:
To prevent accidentally overwriting your objects by name.
Re: Creating a function that will add to a table
Posted: Sun Jun 26, 2011 4:49 am
by TechnoCat
ivan wrote:You are missing the line "objects[name] = {}":
Code: Select all
function newobject(name,world,m,x,y,r,width,height,angle)
objects[name] = {}
objects[name] = love.physics.newBody(world, x, y, m, r)
objects[name].shape = love.physics.newRectangleShape(objects.name.body, 0, 0, width, height, angle)
end
ivan, you missed one [name] yourself. lol
Also, you probably wanted to store newBody into "body".
Code: Select all
function newobject(name,world,m,x,y,r,width,height,angle)
objects[name] = {}
objects[name].body = love.physics.newBody(world, x, y, m, r)
objects[name].shape = love.physics.newRectangleShape(objects[name].body, 0, 0, width, height, angle)
end
Re: Creating a function that will add to a table
Posted: Sun Jun 26, 2011 5:16 am
by ruarai
-
Re: Creating a function that will add to a table
Posted: Sun Jun 26, 2011 8:51 am
by Robin
This is your problem:
Code: Select all
newobject(ground,world,0,650/2,625,0,650,50,0)
The name ground doesn't point to anything, so it points to nil.
Change to:
Code: Select all
newobject("ground",world,0,650/2,625,0,650,50,0)
Also, when something errors, you get a traceback. You'll want to look at that when something errors, as it provides vital clues. Even if you can't find anything yourself, you should still copy it so we can see it too.
Even better, give us a .love, so we can experience the error ourselves. (See
the forum rules.)
-
Posted: Sun Jun 26, 2011 8:58 am
by ruarai
-