My project crashes every time I try to edit the contents of a table in an if else statement.

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
novajourney
Prole
Posts: 1
Joined: Mon Jul 31, 2023 2:47 am

My project crashes every time I try to edit the contents of a table in an if else statement.

Post by novajourney »

if down == true then

if checkForPiece(chessPieceData, chessPieceMouseX, chessPieceMouseY) and (waiting == false) then

pieceIndex = findPieceIndex(chessPieceData, chessPieceMouseX, chessPieceMouseY)

waiting = true

print(pieceIndex)

else

table = chessPieceData[pieceIndex]
chessPiece = table[1]

waiting = false

end

end
If I add a table.insert() or a table.remove() within this if else statement, then my game crashes when down == true. I don't know why this is happening and I can't find a workaround for it either.
duaner
Prole
Posts: 41
Joined: Thu May 07, 2020 6:43 pm
Contact:

Re: My project crashes every time I try to edit the contents of a table in an if else statement.

Post by duaner »

novajourney wrote: Mon Jul 31, 2023 2:49 amIf I add a table.insert() or a table.remove() within this if else statement, then my game crashes when down == true. I don't know why this is happening and I can't find a workaround for it either.
table.insert and table.remove must be called when table is the original lua library, "table". You've assigned a variable with the same name, so lua expects to find a method called insert or remove, which you presumably don't have.

If you change table to a different variable name (e.g. tab), you'll be able to call table.insert(tab, x) normally.
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: My project crashes every time I try to edit the contents of a table in an if else statement.

Post by togFox »

^ This.

"table" is a reserved word (kind of) so just rename that variable name.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: My project crashes every time I try to edit the contents of a table in an if else statement.

Post by milon »

Also consider using [ code ] tags when posting on the forum:
novajourney wrote: Mon Jul 31, 2023 2:49 am

Code: Select all

if down == true then

        if checkForPiece(chessPieceData, chessPieceMouseX, chessPieceMouseY) and (waiting == false) then

            pieceIndex = findPieceIndex(chessPieceData, chessPieceMouseX, chessPieceMouseY)

            waiting = true

            print(pieceIndex)

        else

            table = chessPieceData[pieceIndex]
            chessPiece = table[1]

            waiting = false

        end

    end
If I add a table.insert() or a table.remove() within this if else statement, then my game crashes when down == true. I don't know why this is happening and I can't find a workaround for it either.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

Re: My project crashes every time I try to edit the contents of a table in an if else statement.

Post by pgimeno »

Just another example of the harm that globals can do.

Had 'table' been a local variable, that would not have happened.
Post Reply

Who is online

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