Error claims a value is nil, which makes no sense

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.
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

Error claims a value is nil, which makes no sense

Post by dyl4n130 »

Everytime I attempt to run my code I get an error that says a value in my if statement is nil.

The if statement in question:

Code: Select all

elseif rX[i] < rX[n] + 2 and rX[n] < rX[i] + 2 and rY[i] < rY[n] + 2 and rY[n] < rY[i] + 2 then
Ive managed to narrow it down to it thinking that rX[n] is nil. The absolutely baffling thing is that they are both being called from this:

Code: Select all

for i = 1, #rX do
    for n = 1, #rX do
      if isCollided(i, n) then
Which means that i and n are the same value. So it is literelly comparing rX[1] and rX[1] and says that one is nil and the other is fine. Does anyone have any idea what this could mean? I have tried everything I can think of, at this point I'm certain the solution is some dumb typo I made that is just flying over my head. Here is all the relevant code:

Code: Select all

function doFights()
  for i = 1, #rX do
    for n = 1, #rX do
      if isCollided(i, n) then
        if rS[i] > rS[n] then
          table.remove(rX, n)
          table.remove(rY, n)
          table.remove(rS, n)
        elseif rS[i] < rS[n] then
          table.remove(rX, i)
          table.remove(rY, i)
          table.remove(rS, i)
        elseif rS[i] == rS[n] then
          if love.math.random(0, 1) == 0 then
            table.remove(rX, n)
            table.remove(rY, n)
            table.remove(rS, n)
          else
            table.remove(rX, i)
            table.remove(rY, i)
            table.remove(rS, i)
          end
        end
      end
    end
  end
end

function isCollided(i, n)
  if rX[i] < rX[n] + 2 and rX[n] < rX[i] + 2 and rY[i] < rY[n] + 2 and rY[n] < rY[i] + 2 then
    return 1
  else
    return 0
  end
end
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

Re: Error claims a value is nil, which makes no sense

Post by dyl4n130 »

So the problem is definitley in the `n` for loop. In other areas I replaced n with 1 and it ran fine. So the loop that does i works fine, but the exact same loop with a differnent letter doesnt work. This is really annoying
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Error claims a value is nil, which makes no sense

Post by raidho36 »

If you want to remove table elements mid-loop you need to loop backwards.
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

Re: Error claims a value is nil, which makes no sense

Post by dyl4n130 »

raidho36 wrote: Fri Sep 01, 2017 1:13 am If you want to remove table elements mid-loop you need to loop backwards.
could you elaborate? I dont think that is the problem since the program crashes before it even gets to the table.remove
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Error claims a value is nil, which makes no sense

Post by raidho36 »

If you remove a table element, it will be shorter than it was when you started looping, so last loop indices will be out of range and produce nil. I'm sure that's your problem. Just loop it backwards.
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Error claims a value is nil, which makes no sense

Post by Sir_Silver »

What raidho means is, instead of iterating from 1 to the # of your table, iterate from the # of your table to 1 stepping by -1:

Code: Select all

for i = #my_table, 1, -1 do

end
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

Re: Error claims a value is nil, which makes no sense

Post by dyl4n130 »

raidho36 wrote: Fri Sep 01, 2017 1:29 am If you remove a table element, it will be shorter than it was when you started looping, so last loop indices will be out of range and produce nil. I'm sure that's your problem. Just loop it backwards.
did that, same problem. it returns nil before table elements start getting removed
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

Re: Error claims a value is nil, which makes no sense

Post by dyl4n130 »

Sir_Silver wrote: Fri Sep 01, 2017 1:47 am What raidho means is, instead of iterating from 1 to the # of your table, iterate from the # of your table to 1 stepping by -1:

Code: Select all

for i = #my_table, 1, -1 do

end
thats what i thought he meant, didnt work though
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Error claims a value is nil, which makes no sense

Post by raidho36 »

Then it must be your other table. If it's not the same length it would return nil on trailing elements.
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

Re: Error claims a value is nil, which makes no sense

Post by dyl4n130 »

raidho36 wrote: Fri Sep 01, 2017 2:02 am Then it must be your other table. If it's not the same length it would return nil on trailing elements.
all the tables have to be the same length. they are all defined within the same for loop in love.load
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 50 guests