Author Topic: Need help with for loops  (Read 4692 times)

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Need help with for loops
« on: February 13, 2011, 12:46:56 PM »
Ok, making some stuff... But I get tis damn retarded error:

Code: [Select]
for i = 0,numroids do
neighbourstart[i] = numneighbours
for j = 0,numroids do
if i ~= j and i ~= nil and j ~= nil then

length = GetAsteroid(i).position.x - GetAsteroid(j).position.x
height = GetAsteroid(i).position.y - GetAsteroid(j).position.y

distancetoroid = math.sqrt((length * length) + (height * height))
distancetoroid = distancetoroid - GetAsteroid(j).Radius

if distancetoroid < GetAsteroid(j).SendDistance then

numneighbours = numneighbours + 1
neighbour[numneighbours] = GetAsteroid(j)
end
end
end
neighbourend[i] = numneighbours
end

It says that j(the for loop variable) is nil while i isn't, and they both run the same thing, WTF?

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Need help with for loops
« Reply #1 on: February 13, 2011, 02:10:30 PM »
Code: [Select]
if i ~= j and i ~= nil and j ~= nil then
This is usually the problem.

Suppose i = 3 and j = nil.

First it checks if 3 is equal to nil.....but wait!  Before we've even got to our nil check, we've already tried to compare nil with a number.  :>

If you're checking for nils, you need to do that seperately from any other kind of check.


Try it like this:


Code: [Select]
if i ~= nil and j ~= nil then
if i ~= j then

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Need help with for loops
« Reply #2 on: February 13, 2011, 02:17:50 PM »
Also, note that there is an error in these lines:


Code: [Select]
distancetoroid = distancetoroid - GetAsteroid(j).Radius

if distancetoroid < GetAsteroid(j).SendDistance then

When you do this type of distance-between-asteroids check, to see if one asteroid can send to another asteroid, you start with 2 asteroids; i and j.
You pick one of the asteroids to be The Sender Asteroid and the other asteroid is The Destination Asteroid.

You then subtract the radius of The Destination Asteroid from the distance between them (which you have done on the first line).
Then you check if that result is less than the send distance of The Sender Asteroid (in which case the sender is in range of the destination).


In the code above, you're subtracting the destination asteroid and checking against the send distance of the destination asteroid!  You can't have them the same on both lines otherwise it won't work how you want, all the measurements will be off.

To fix, have it like this:

Code: [Select]
distancetoroid = distancetoroid - GetAsteroid(i).Radius

if distancetoroid < GetAsteroid(j).SendDistance then


...or this will work just as well:

Code: [Select]
distancetoroid = distancetoroid - GetAsteroid(j).Radius

if distancetoroid < GetAsteroid(i).SendDistance then
« Last Edit: February 13, 2011, 02:21:49 PM by annikk.exe »

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 4
  • Posts: 1,809
Re: Need help with for loops
« Reply #3 on: February 13, 2011, 02:28:44 PM »
This still doesn't fully explain the problem you've been having though.  Why is j producing a nil value in the first place?

Which line number does the error come from?  (Console will tell you)

Is it possible there's an error with the way numroids is calculated?  Then when you come to reference things like GetAsteroid(j).position.x it might be refering GetAsteroid(7) when there are only 3 asteroids in the level, for example.  That would produce and error message as well.


Impossible to say really, without more detail on the error message and corresponding line number.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Need help with for loops
« Reply #4 on: February 13, 2011, 07:16:46 PM »
It's strange in the first place, but here is the roid counter, taken from my AI which was taken from your AI :P
Code: [Select]
for roids = 0,200 do
if GetAsteroid(roids) ~= nil then
if GetAsteroid(roids).radius > 10 then

numroids = numroids + 1
end
end
end

EDIT: The error message come from line 120, which doesn't say you much... but 120 is:
Code: [Select]
length = GetAsteroid(i).position.x - GetAsteroid(j).position.x and j is the problem...

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: Need help with for loops
« Reply #5 on: February 13, 2011, 07:34:28 PM »
Figured it out, it was that numroids started on 0 not -1 :P