Can't solve error on game

Hello guys, after some struggle I managed to parameterize the functions in my code but I get an error I can’t solve: attempt to perform arithmetic on a nil value (x2). Here’s the code (the error is at line 3 which is called by invert() function). Any suggestion?

--calcola la lunghezza di un segmento dati gli estremi &nbsp;&nbsp;&nbsp;&nbsp;local function segmentLenght( x1, y1, x2, y2 ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local xFactor = x2 - x1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local yFactor = y2 - y1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local dist = math.sqrt( (xFactor\*xFactor) + (yFactor\*yFactor) ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return dist &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;--calcola l'intervallo di tempo per percorrere un segmento con una certa velocità &nbsp;&nbsp;&nbsp;&nbsp;local function segmentTime(S, V) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local timeInterval = S / V &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return timeInterval &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;--calcola l'angolo tra due oggetti local function angleBetween ( srcObj, dstObj ) local xDist = dstObj.x-srcObj.x ; local yDist = dstObj.y-srcObj.y local angleBetween = math.deg( math.atan( yDist/xDist ) ) if srcObj.x \< dstObj.x then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;angleBetween = angleBetween+90 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;angleBetween = angleBetween-90 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end return angleBetween end local iblue=0 &nbsp;&nbsp;&nbsp;&nbsp;local ired=0 &nbsp;&nbsp;&nbsp;&nbsp;local jblue=0 &nbsp;&nbsp;&nbsp;&nbsp;local jred=0 &nbsp;&nbsp;&nbsp;&nbsp;local deltablue = 1 &nbsp;&nbsp;&nbsp;&nbsp;local deltared = 1 &nbsp;&nbsp;&nbsp;&nbsp;local blueSegmentTransition &nbsp;&nbsp;&nbsp;&nbsp;local redSegmentTransition &nbsp;&nbsp;&nbsp;&nbsp;local blueInPitch = false &nbsp;&nbsp;&nbsp;&nbsp;local redInPitch = false &nbsp;&nbsp;&nbsp;&nbsp;local blueX = {} &nbsp;&nbsp;&nbsp;&nbsp;local blueY = {} &nbsp;&nbsp;&nbsp;&nbsp;local redX = {} &nbsp;&nbsp;&nbsp;&nbsp;local redY = {} &nbsp;&nbsp;&nbsp;&nbsp;local timeTable = {} &nbsp;&nbsp;&nbsp;&nbsp;local lenblueX local function invert(player, i, j, delta, SegmentTransition, playerX, playerY) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transition.pause(player) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if player.x \<= display.contentCenterX - halfpost or player.x \>= display.contentCenterX + halfpost then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delta = delta \* (-1) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if j==0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j = 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;segmentTransition = transition.to(player, { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time = segmentTime( segmentLenght( player.x, player.y, playerX[i+delta+j], playerY[i+delta+j]), v), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = playerX[i+delta+j], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y = playerY[i+delta+j], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onComplete=function() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onCompleteMove(player, i, j, delta, SegmentTransition, playerX, playerY); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transition.resume(player) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local function startMoving(player, i, j, delta, SegmentTransition, playerX, playerY) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if player.x \< display.contentCenterX then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while playerX[i] \< player.x do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = i + 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delta = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j = 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = playerX &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while playerX[i] \> player.x do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = i - 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delta = -1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;segmentTransition = transition.to(player, { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time = segmentTime( segmentLenght( player.x, player.y, playerX[i], playerY[i]), v), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = playerX[i], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y = playerY[i], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onComplete=function() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onCompleteMove(player, i, j, delta, SegmentTransition, playerX, playerY); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;local function EllipseY(X) &nbsp;&nbsp;&nbsp;&nbsp;return display.contentCenterY - math.sqrt((1-(X^2)/(a^2))\*b^2) - longside &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;local leftX = display.contentCenterX - a - halfpost &nbsp;&nbsp;&nbsp;&nbsp;local leftPostX = display.contentCenterX - halfpost &nbsp;&nbsp;&nbsp;&nbsp;local rightPostX = display.contentCenterX + halfpost &nbsp;&nbsp;&nbsp;&nbsp;local rightX = display.contentCenterX + a + halfpost &nbsp;&nbsp;&nbsp;&nbsp;local highY = display.contentCenterY - longside - b &nbsp;&nbsp;&nbsp;&nbsp;local function blueHooking(player, i, j, delta, SegmentTransition, playerX, playerY, playerInPitch) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if playerInPitch and player.y \<= display.contentCenterY then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if player.x - leftX \<= 3 or rightX - player.x \<= 3 or player.y - highY \<= 2 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;player.isAwake = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;startMoving(player, i, j, delta, SegmentTransition, playerX, playerY) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerInPitch = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif player.x \< leftPostX and player.y - EllipseY(player.x-display.contentCenterX+halfpost) \<= 3 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;player.isAwake = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;startMoving(player, i, j, delta, SegmentTransition, playerX, playerY) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerInPitch = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif player.x \>rightPostX and player.y - EllipseY(player.x-display.contentCenterX-halfpost) \<= 3 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;player.isAwake = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;startMoving(player, i, j, delta, SegmentTransition, playerX, playerY) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerInPitch = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;Runtime:addEventListener( "enterFrame", function() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blueHooking(player2, iblue, jblue, deltablue, blueSegmentTransition, blueX, blueY, blueInPitch); &nbsp;&nbsp;&nbsp;&nbsp;end) 

button1:addEventListener( “tap”, function()

invert(player1, ired, jred, deltared, redSegmentTransition, redX, redY);

end)

Add some print messages to check exactly what values you have for I, j and delta when accessing the playerX table in invert, my guess is they aren’t what you are expecting and the fetched element in playerX doesn’t exist.

I’ve already tried before and that’s the output from console (in order the printed values belong to i , j, delta, playerX[i+j+delta], playerY[i+j+delta]:

21:01:44.672 0 21:01:44.672 1 21:01:44.672 -1 21:01:44.672 nil 21:01:44.672 nil 21:01:44.672 ERROR: Runtime error 21:01:44.672 C:\Users\mimmi\Desktop\sababounce\_dev\gameView.lua:114: attempt to perform arithmetic on local 'x2' (a nil value) 21:01:44.672 stack traceback: 21:01:44.672 C:\Users\mimmi\Desktop\sababounce\_dev\gameView.lua:114: in function 'segmentLenght' 21:01:44.672 C:\Users\mimmi\Desktop\sababounce\_dev\gameView.lua:204: in function 'invert' 21:01:44.672 C:\Users\mimmi\Desktop\sababounce\_dev\gameView.lua:969: in function \<C:\Users\mimmi\Desktop\sababounce\_dev\gameView.lua:968\> 21:01:44.672 ?: in function \<?:169\>

In your example (which is a bit hard to read, lots of code  :slight_smile: ):

i = 0

j = 1

delta = -1

Therefore: i + j + delta = 0

Passing playerX9[i + j + delta] is referring to a non-existent 0th entry in a table.  In Lua, tables start at 1.

–john

There’s your answer. then. 

0 + 1 + -1 = 0. Generally, a Lua table won’t have a value at index 0 unless you specifically tell it to put one there.

Also, where are your redX and redY tables populated? In this code, they are just blank tables, so whatever you try to access in them (their aliases are playerX and playerY within the invert function) will be nil.

Yeah, the tables are populated (didn’t post whole code). The observation about the oth entry is mindblowing! Thank you so much, indeed while trying to print some entries with 0th index I get nil value. I’ll try fix the code in order to avoid this problem. Why then did it use to work when the invert fuction was not parameterized? (directly accessing one of the tables)

 OK, just talked with my roomate (who coded this part) and he told me he considered that while coding (no parameterization) and it worked by some reason (probably it didn’t happen that particular situation) so don’t mind my last post. I’ll try to work around this asap and post back.

So I’ve sorted this out: (hope my thought is right, I’m thinking as I would do in C) previously I didn’t use a parameterized function for invert so it used to refer and modify the global i,j and delta variables. Now due to parameters (which are passed by value I assume) the invert function is not directly modifying the i,j and delta variables but is only operating on his copies (the parameters). Because of this it ended up executing the invert function with the starting values of parameters (0,1 and -1 which ended up in a 0th index for table). What I want now is to pass them by reference (C again XD) or directly modify them in another way. Any tip on how to perform this?

EDIT: I’ve tried declaring i,j and delta as tables and passing them to the function (by reference I assume due to Lua standards) but nothing changes

EDIT: I eventually managed to pass them properly. Now it works. Thx guys!

How did you get it working? Just so if other people have a similar problem, they can use your solution.

I was going to suggest that you pass the parameters in and return them back again if you intend to change them.

I defined i j and delta as tables and passed only the index of that table to the function (thus I could refer to them). So now it all works. The problem consisted in passing the params by value.

Add some print messages to check exactly what values you have for I, j and delta when accessing the playerX table in invert, my guess is they aren’t what you are expecting and the fetched element in playerX doesn’t exist.

I’ve already tried before and that’s the output from console (in order the printed values belong to i , j, delta, playerX[i+j+delta], playerY[i+j+delta]:

21:01:44.672 0 21:01:44.672 1 21:01:44.672 -1 21:01:44.672 nil 21:01:44.672 nil 21:01:44.672 ERROR: Runtime error 21:01:44.672 C:\Users\mimmi\Desktop\sababounce\_dev\gameView.lua:114: attempt to perform arithmetic on local 'x2' (a nil value) 21:01:44.672 stack traceback: 21:01:44.672 C:\Users\mimmi\Desktop\sababounce\_dev\gameView.lua:114: in function 'segmentLenght' 21:01:44.672 C:\Users\mimmi\Desktop\sababounce\_dev\gameView.lua:204: in function 'invert' 21:01:44.672 C:\Users\mimmi\Desktop\sababounce\_dev\gameView.lua:969: in function \<C:\Users\mimmi\Desktop\sababounce\_dev\gameView.lua:968\> 21:01:44.672 ?: in function \<?:169\>

In your example (which is a bit hard to read, lots of code  :slight_smile: ):

i = 0

j = 1

delta = -1

Therefore: i + j + delta = 0

Passing playerX9[i + j + delta] is referring to a non-existent 0th entry in a table.  In Lua, tables start at 1.

–john

There’s your answer. then. 

0 + 1 + -1 = 0. Generally, a Lua table won’t have a value at index 0 unless you specifically tell it to put one there.

Also, where are your redX and redY tables populated? In this code, they are just blank tables, so whatever you try to access in them (their aliases are playerX and playerY within the invert function) will be nil.

Yeah, the tables are populated (didn’t post whole code). The observation about the oth entry is mindblowing! Thank you so much, indeed while trying to print some entries with 0th index I get nil value. I’ll try fix the code in order to avoid this problem. Why then did it use to work when the invert fuction was not parameterized? (directly accessing one of the tables)

 OK, just talked with my roomate (who coded this part) and he told me he considered that while coding (no parameterization) and it worked by some reason (probably it didn’t happen that particular situation) so don’t mind my last post. I’ll try to work around this asap and post back.

So I’ve sorted this out: (hope my thought is right, I’m thinking as I would do in C) previously I didn’t use a parameterized function for invert so it used to refer and modify the global i,j and delta variables. Now due to parameters (which are passed by value I assume) the invert function is not directly modifying the i,j and delta variables but is only operating on his copies (the parameters). Because of this it ended up executing the invert function with the starting values of parameters (0,1 and -1 which ended up in a 0th index for table). What I want now is to pass them by reference (C again XD) or directly modify them in another way. Any tip on how to perform this?

EDIT: I’ve tried declaring i,j and delta as tables and passing them to the function (by reference I assume due to Lua standards) but nothing changes

EDIT: I eventually managed to pass them properly. Now it works. Thx guys!

How did you get it working? Just so if other people have a similar problem, they can use your solution.

I was going to suggest that you pass the parameters in and return them back again if you intend to change them.

I defined i j and delta as tables and passed only the index of that table to the function (thus I could refer to them). So now it all works. The problem consisted in passing the params by value.