Page 1 of 1

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

Posted: Mon Jul 31, 2023 2:49 am
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.

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

Posted: Tue Aug 01, 2023 12:56 am
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.

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

Posted: Tue Aug 01, 2023 7:51 am
by togFox
^ This.

"table" is a reserved word (kind of) so just rename that variable name.

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

Posted: Tue Aug 01, 2023 8:28 pm
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.

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

Posted: Wed Aug 02, 2023 2:56 pm
by pgimeno
Just another example of the harm that globals can do.

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