Page 1 of 1
dual for loop instance error
Posted: Mon Feb 19, 2018 5:16 am
by RoadhammerGaming
Hi, I am trying to compare the x and y positions of 2 instances to check for a collision, in my project I may have any number of instances of object enemy and object weapon, so I thought I could check for this by using a nested for loop, this is my code:
Code: Select all
for j = #enemy, 1, -1 do
local oE = enemy[j]
if (o.x + 20) > oE.xX and (o.x +20) < oE.xX + 41 and (o.y + 22) > oE.yYX and (o.y +22) < oE.yYX + 45 then
points = points + (10 * level)
table.remove(weapon, i)
table.remove(enemy, j)
end
end
and this is the error code I'm getting:
I am new at love2d and lua, so imagine I'm overlooking something or doing something wrong, can anyone tell me what could be the problem or steer me in the right direction? thanks
Re: dual for loop instance error
Posted: Mon Feb 19, 2018 9:03 am
by grump
RoadhammerGaming wrote: ↑Mon Feb 19, 2018 5:16 am
Hi, I am trying to compare the x and y positions of 2 instances to check for a collision, in my project I may have any number of instances of object enemy and object weapon, so I thought I could check for this by using a nested for loop, this is my code:
There is no nested loop in the code you posted, and if I'm not mistaken, this code can't produce the error message.
You're trying to compare a variable that's not been initialized (nothing has been assigned to it) with a number. Since you're adding numbers to all the variables on the left side of your comparison operators (< and >), your code would throw "attempt to perform arithmetic on a nil value", not "attempt to compare nil with a number" if those vars were nil. There is no way this code results in your error.
Please check again and post the relevant code.
Re: dual for loop instance error
Posted: Mon Feb 19, 2018 1:48 pm
by RoadhammerGaming
grump wrote: ↑Mon Feb 19, 2018 9:03 am
RoadhammerGaming wrote: ↑Mon Feb 19, 2018 5:16 am
Hi, I am trying to compare the x and y positions of 2 instances to check for a collision, in my project I may have any number of instances of object enemy and object weapon, so I thought I could check for this by using a nested for loop, this is my code:
There is no nested loop in the code you posted, and if I'm not mistaken, this code can't produce the error message.
You're trying to compare a variable that's not been initialized (nothing has been assigned to it) with a number. Since you're adding numbers to all the variables on the left side of your comparison operators (< and >), your code would throw "attempt to perform arithmetic on a nil value", not "attempt to compare nil with a number" if those vars were nil. There is no way this code results in your error.
Please check again and post the relevant code.
Oh sorry about that I didn't paste the whole loop, this should be all of it:
for i = #weapon, 1, -1 do
local o = weapon
for j = #enemy, 1, -1 do
local oE = enemy[j]
if (o.x + 20) > oE.xX and (o.x +20) < oE.xX + 41 and (o.y + 22) > oE.yYX and (o.y +22) < oE.yYX + 45 then
points = points + (10 * level)
table.remove(weapon, i)
table.remove(enemy, j)
end
end
if (o.x < -10) or (o.x > love.graphics.getWidth() + 10)
or (o.y < -10) or (o.y > love.graphics.getHeight() + 10) then
table.remove(weapon, i)
end
end
AAAHHHH and I see exactly what I did wrong!! I misspelled The variables xX and yY in the last part of the comparison, that's embarrassing sorry for this post and thanks for your help.