-

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
ruarai
Prole
Posts: 11
Joined: Sun Jun 26, 2011 3:04 am

-

Post by ruarai »

-
Last edited by ruarai on Sun Feb 19, 2023 6:05 am, edited 1 time in total.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Creating a function that will add to a table

Post 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
ruarai
Prole
Posts: 11
Joined: Sun Jun 26, 2011 3:04 am

Re: Creating a function that will add to a table

Post by ruarai »

-
Last edited by ruarai on Sun Feb 19, 2023 6:06 am, edited 1 time in total.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Creating a function that will add to a table

Post by TechnoCat »

You're still using objects.name in objects.name.shape
objects[name].shape
ruarai
Prole
Posts: 11
Joined: Sun Jun 26, 2011 3:04 am

Re: Creating a function that will add to a table

Post by ruarai »

-
Last edited by ruarai on Sun Feb 19, 2023 6:06 am, edited 1 time in total.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Creating a function that will add to a table

Post 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:

Code: Select all

assert(objects[name] == nil)
To prevent accidentally overwriting your objects by name.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Creating a function that will add to a table

Post 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
ruarai
Prole
Posts: 11
Joined: Sun Jun 26, 2011 3:04 am

Re: Creating a function that will add to a table

Post by ruarai »

-
Last edited by ruarai on Sun Feb 19, 2023 6:06 am, edited 1 time in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Creating a function that will add to a table

Post 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.)
Help us help you: attach a .love.
ruarai
Prole
Posts: 11
Joined: Sun Jun 26, 2011 3:04 am

-

Post by ruarai »

-
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 10 guests