Page 1 of 1

Bug(s ?) in my code | main.lua:1201: attempt to index local 'pSprite' (a nil value)

Posted: Sat Dec 14, 2019 2:56 pm
by ColonelEnRocket
Hi i'm Neil and today I have un little big problem...


I develop a game (called Retrail) and for implemented sprite easily i've maked functions factory.

And when the player collide with a trap or an ennemie and die (after the level 6), the game crash.

So... what is the problem


___________________________________________________
Debug :
Error
main.lua:1201: attempt to index local 'pSprite' (a nil value)

Traceback
main.lua:1201: in function 'updateSprite'
main.lua:1312: in function 'update'
[C]: in function 'xpcall'
____________________________________________________

Image


When collision with a trap...

Image





Image


when collision with the ennemie...


Image




And the code...


Image


Image

Re: Bug(s ?) in my code | main.lua:1201: attempt to index local 'pSprite' (a nil value)

Posted: Sat Dec 14, 2019 10:08 pm
by Xugro
You should upload your .love file. Then someone can take a look at the source code to find the bug.

Just a wild guess: You delete sprites by

Code: Select all

lstSprites[index] = nil
This creates gaps in the table. So the table has the same length before deletion, but one index is emtpy. If you try to access that index the table returns nil. Use

Code: Select all

table.remove(lstSprites, index)
instead.

Re: Bug(s ?) in my code | main.lua:1201: attempt to index local 'pSprite' (a nil value)

Posted: Sun Dec 15, 2019 12:16 pm
by ColonelEnRocket
Hi ! Thanks !

The download link of the code source :
https://www.fromsmash.com/hLo8pZ.L2t-c0

Re: Bug(s ?) in my code | main.lua:1201: attempt to index local 'pSprite' (a nil value)

Posted: Mon Dec 16, 2019 12:06 pm
by pgimeno
The download link does not work for me. Can't you just upload it as an attachment? Select "Full Editor & Preview" to add attachments to a post.

Re: Bug(s ?) in my code | main.lua:1201: attempt to index local 'pSprite' (a nil value)

Posted: Mon Dec 16, 2019 5:49 pm
by zorg
I don't remember the filesize limits for forum attachments but the zip's 35 MB because of the music included

Re: Bug(s ?) in my code | main.lua:1201: attempt to index local 'pSprite' (a nil value)

Posted: Tue Dec 17, 2019 8:51 pm
by Xugro
If you call the function playerDeath() in love.update(dt) you initialize a new lstSprites. After creating a new player you jump back into the code line where playerDeath() was called. And there the local loop variable nSprite is still iterating over the old number of #lstSprites. Since there are not enough sprites in lstSprites yet (since lstSprites is still initializing) the next local sprite is nil. And that's where your game crashes.

Do not set lstSprites to an empty list (line 411) and fill it with new sprites while still iterating over it.