Author Topic: [error]exception indexing '20'  (Read 16312 times)

Breakord

  • Sapling
  • **
  • Thank You
  • -Given: 14
  • -Receive: 7
  • Posts: 57
  • Eufloria: Yes
[error]exception indexing '20'
« on: February 06, 2013, 03:59:13 PM »
When testing my codes,an error occured and I dont know why...
So I am here again to ask for good souls' help... =_=
The error message in console is:
lua:513:exception indexing '20'(the number changes each time, '20'or'35'or'2'or...)
Codes:
Code: [Select]
function LevelSetup()
...
...
SeedlingsInBattle={}
for i=0,GetNumAsteroids()-1 do
   SeedlingsInBattle[i]={}
   for j=0,11 do
      SeedlingsInBattle[i][j]={}
   end
end
end
function LevelLogic()
...
...
while GameRunning() do
   for i=0,GetNumAsteroids()-1 do--check each asteroid
      local a=GetAsteroid(i)
      if a:GetNumSeedlingsExcluding(a.Owner)>0 then--check if there are other factions' seedlings on the asteroid,which means the battle begins or continues
         for j=0,11 do--check each faction
            if a:GetNumSeedlings(j)>0 then
               for count=0,a:GetNumSeedlings(j)-1 do
                  SeedlingsInBattle[i][j][count]=a:Seedlings(j)[count]--*****Here is line 513*****
               end
            end
         end
      end
   end
...
...
   coroutine.yield()
end
...
...
end
=_=,Whats wrong with "SeedlingsInBattle[\i][j][count]=a:Seedlings(j)[count-1]" ?
By the way,it's been a while since I saw Alex and Rudolf online last time...
So busy you two are, but dont forget to take a rest.
« Last Edit: February 06, 2013, 04:12:46 PM by Breakord »

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 4
  • -Receive: 24
  • Posts: 932
  • Eufloria: Yes
Re: [error]exception indexing '20'
« Reply #1 on: February 07, 2013, 04:15:16 AM »
It may be that the seedling has moved away from the asteroid.

What does the Seedlings() function do? What should all of this code do?

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: [error]exception indexing '20'
« Reply #2 on: February 07, 2013, 05:46:24 AM »
It may be that the seedling has moved away from the asteroid.

What does the Seedlings() function do? What should all of this code do?

It returns an array of the seedlings on the asteroid. The number passed in returns the seedlings owned by that faction.

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: [error]exception indexing '20'
« Reply #3 on: February 07, 2013, 10:35:58 PM »
lua:513:exception indexing '20'(the number changes each time, '20'or'35'or'2'or...)
Code: [Select]
while GameRunning() do
   for i=0,GetNumAsteroids()-1 do--check each asteroid
      local a=GetAsteroid(i)
      if a:GetNumSeedlingsExcluding(a.Owner)>0 then--check if there are other factions' seedlings on the asteroid,which means the battle begins or continues
         for j=0,11 do--check each faction
            if a:GetNumSeedlings(j)>0 then
               for count=0,a:GetNumSeedlings(j)-1 do
                  SeedlingsInBattle[i][j][count]=a:Seedlings(j)[count]--*****Here is line 513*****
               end
            end
         end
      end
   end
...
...
   coroutine.yield()
end

I think it would be good practice to reset this array per tick, I don't really think this will have any huge effect on the performance, but this will ensure that the old list is wiped, which is important!
I'm also wondering if it is possible to do a Seedlings() without any parameter, which I suspect will return the whole array of seedlings.

And:

Code: [Select]
for j = 0,11 do--check each faction
if a:GetNumSeedlings(j) > 0 then
SeedlingsInBattle[i][j] = a:Seedlings(j)--*****Here is line 513*****
end
end
The returned value from Seedlings() is an array, which means you can put it directly in as an array rather than picking out every single seedling "manually".

Breakord

  • Sapling
  • **
  • Thank You
  • -Given: 14
  • -Receive: 7
  • Posts: 57
  • Eufloria: Yes
Re: [error]exception indexing '20'
« Reply #4 on: February 09, 2013, 09:16:30 PM »
I think it would be good practice to reset this array per tick, I don't really think this will have any huge effect on the performance, but this will ensure that the old list is wiped, which is important!I'm also wondering if it is possible to do a Seedlings() without any parameter, which I suspect will return the whole array of seedlings.
Eh..Actually I have added "SeedlingsInBattle[\i][j]={}"  to reset the array just after I posted this topic..And new codes to detect enemy mines on the asteroid(to detect another kind of battle situation).Unfortunately, I have tried and it is impossible to do a Seedlings() without parameters.
« Last Edit: February 09, 2013, 09:38:52 PM by Breakord »

Breakord

  • Sapling
  • **
  • Thank You
  • -Given: 14
  • -Receive: 7
  • Posts: 57
  • Eufloria: Yes
Re: [error]exception indexing '20'
« Reply #5 on: February 10, 2013, 09:24:45 AM »
The returned value from Seedlings() is an array, which means you can put it directly in as an array rather than picking out every single seedling "manually".
Yes, I know, the reason I didn't is that the array is a userdata value, if put it directly in as an array like
"SeedlingsInBattle[\i][j]=a:Seedlings(j)"
I can't use function "table.getn(Array)" to get the length of SeedlingsInBattle[\i][j] ..
Back on topic, I think Pilchard is right, but I dont know how to prove it.
The error seems to be inevitable, so I am thinking about something like"on error resume next".

Aino

  • Ent
  • ******
  • Thank You
  • -Given: 4
  • -Receive: 30
  • Posts: 1,527
  • Eufloria: Yes
Re: [error]exception indexing '20'
« Reply #6 on: February 10, 2013, 09:50:29 AM »
Yes, I know, the reason I didn't is that the array is a userdata value, if put it directly in as an array like
"SeedlingsInBattle[\i][j]=a:Seedlings(j)"
I can't use function "table.getn(Array)" to get the length of SeedlingsInBattle[\i][j] ..
Back on topic, I think Pilchard is right, but I dont know how to prove it.
The error seems to be inevitable, so I am thinking about something like"on error resume next".

I don't think that's the cause, but you can prove it by testing it. Just create a map with two asteroid, and one which has seedlings and you simply move your seedlings from one asteroid to the other. Try different distances between the asteroid, some factors may come in to count?
I don't this it's the cause tough,since if a seedling leaves the asteroid, it won't be counted as on it anymore and the seedling should be erased from the array.

One thing you can do, when adding them to the array, is to simply check if they are nil values, also try using the "assert" function when checking them. It MIGHT work :)

Breakord

  • Sapling
  • **
  • Thank You
  • -Given: 14
  • -Receive: 7
  • Posts: 57
  • Eufloria: Yes
Re: [error]exception indexing '20'
« Reply #7 on: February 10, 2013, 11:37:07 AM »
I don't think that's the cause, but you can prove it by testing it. Just create a map with two asteroid, and one which has seedlings and you simply move your seedlings from one asteroid to the other. Try different distances between the asteroid, some factors may come in to count?
I don't this it's the cause tough,since if a seedling leaves the asteroid, it won't be counted as on it anymore and the seedling should be erased from the array.
One thing you can do, when adding them to the array, is to simply check if they are nil values, also try using the "assert" function when checking them. It MIGHT work :)
Thanks~:)

Breakord

  • Sapling
  • **
  • Thank You
  • -Given: 14
  • -Receive: 7
  • Posts: 57
  • Eufloria: Yes
Re: [error]exception indexing '20'
« Reply #8 on: February 10, 2013, 12:01:50 PM »
I don't think that's the cause, but you can prove it by testing it. Just create a map with two asteroid, and one which has seedlings and you simply move your seedlings from one asteroid to the other. Try different distances between the asteroid, some factors may come in to count?
I don't this it's the cause tough,since if a seedling leaves the asteroid, it won't be counted as on it anymore and the seedling should be erased from the array.
One thing you can do, when adding them to the array, is to simply check if they are nil values, also try using the "assert" function when checking them. It MIGHT work :)
By the way, there is no nil value in arrays in lua...when you try to get a null element, an exception will occur "exception indexing '20'".
It is different with trying to get a undefined variable, which an error will occur "a nil value".
So it is impossible to check if it is the end of the array through checking if the value is nil..
:)