Can I shorten the fallowing code using tables or anything like that
d1,d2,d3… All are physics bodies with info “d1”,“d2”,“d3”…
if contact.bodyA.info == "d1" or contact.bodyB.info == "d1" then
d1.y = HEIGHT/2 + 100
end
if contact.bodyA.info == "d2" or contact.bodyB.info == "d2" then
d2.y = HEIGHT/2 + 100
end
if contact.bodyA.info == "d3" or contact.bodyB.info == "d3" then
d3.y = HEIGHT/2 + 100
end
-- goes on for ten bodies
I tried using this but it didn’t work
for i =1,10 do
If contact.bodyA.info == "d[i]" or contact.bodyB.info == "d[i]" then
d[i].y = HEIGHT/2 + 100
end
end
The bodies keep on moving, don’t return to height/2 + 100
instead of “d[i]”, use “d”…i
What should I use instead of ‘d[i].y = height/2 + 100’ cause its giving an error attempt to index global d( a nil value). Btw all the “d’s” are from a class called droppers so actual code is
“d[i].droppers.y = height/2 + 100”, if that would make a difference.
The reason you’re getting an error with d is that it isn’t defined as a table
In setup, you should say d={}, and then instead of defining d1, d2, etc as physics bodies, use d[1], d[2].
You should still store the info as d1, d2, etc as you are doing now, but you need to change
If contact.bodyA.info == "d[i]" or contact.bodyB.info == "d[i]" then
it should be “d”…i not “d[i]”
Thanks!! Understood so it is that if you want to use ‘for i = …’ We have to use a table right