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

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.
Post Reply
ColonelEnRocket
Prole
Posts: 2
Joined: Sat Dec 14, 2019 2:34 pm

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

Post 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
Xugro
Party member
Posts: 114
Joined: Wed Sep 29, 2010 8:14 pm

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

Post 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.
ColonelEnRocket
Prole
Posts: 2
Joined: Sat Dec 14, 2019 2:34 pm

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

Post by ColonelEnRocket »

Hi ! Thanks !

The download link of the code source :
https://www.fromsmash.com/hLo8pZ.L2t-c0
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post by zorg »

I don't remember the filesize limits for forum attachments but the zip's 35 MB because of the music included
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Xugro
Party member
Posts: 114
Joined: Wed Sep 29, 2010 8:14 pm

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests