-
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- TechnoCat
- Inner party member
- Posts: 1612
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: Creating a function that will add to a table
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
objects.name.body is literally objects["name"]["body"]
try objects[name] instead
Re: Creating a function that will add to a table
-
Last edited by ruarai on Sun Feb 19, 2023 6:06 am, edited 1 time in total.
- TechnoCat
- Inner party member
- Posts: 1612
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: Creating a function that will add to a table
You're still using objects.name in objects.name.shape
objects[name].shape
objects[name].shape
Re: Creating a function that will add to a table
-
Last edited by ruarai on Sun Feb 19, 2023 6:06 am, edited 1 time in total.
Re: Creating a function that will add to a table
You are missing the line "objects[name] = {}":
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.
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
Code: Select all
assert(objects[name] == nil)
- TechnoCat
- Inner party member
- Posts: 1612
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: Creating a function that will add to a table
ivan, you missed one [name] yourself. lolivan 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
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
-
Last edited by ruarai on Sun Feb 19, 2023 6:06 am, edited 1 time in total.
- 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
This is your problem:
The name ground doesn't point to anything, so it points to nil.
Change to:
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.)
Code: Select all
newobject(ground,world,0,650/2,625,0,650,50,0)
Change to:
Code: Select all
newobject("ground",world,0,650/2,625,0,650,50,0)
Even better, give us a .love, so we can experience the error ourselves. (See the forum rules.)
Help us help you: attach a .love.
Who is online
Users browsing this forum: No registered users and 3 guests