Desperately need help finding what in my code is preventing it from running

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
zam970
Prole
Posts: 2
Joined: Tue Mar 04, 2025 2:58 am

Desperately need help finding what in my code is preventing it from running

Post by zam970 »

------ Issue resolved: cannot use 'table' as a variable name because it will link to a { }. ------------------------------

Working on my first project without a tutorial, and it was all going well until I was trying to add in code to prevent the player from entering an area where another object is.

--When I take out the world:update() out of love.main, the game loads fine.

--If I remove the world:bounds() the game will load but will crash as soon as I try to move.

Otherwise I get the error that my file is packaged incorrectly (I don't think this is the issue because it will run without the section of code mentioned above, and I've made a few other projects with the same folder format and not had this issue).



Here's the relevant bits of code(I'm new to this, and I know I can use physics to make this easier, but trying to do it the long way first).

world.lua

Code: Select all

world = {}

function world:load()
    self:objects()
    self:coordinates()
    
end

function world:update(dt)
    self:objects()
    self:bounds()
    self:move()
end

function world:draw()
    
    love.graphics.line(corner.wall.lefttop.x, corner.wall.lefttop.y, corner.wall.leftbottom.x, corner.wall.leftbottom.y)
    love.graphics.line(corner.wall.lefttop.x, corner.wall.lefttop.y, corner.wall.righttop.x, corner.wall.righttop.y)
    love.graphics.line(corner.wall.righttop.x, corner.wall.righttop.y, corner.wall.rightbottom.x, corner.wall.rightbottom.y)
    love.graphics.line(corner.wall.leftbottom.x, corner.wall.leftbottom.y, corner.wall.rightbottom.x, corner.wall.rightbottom.y)
    love.graphics.line(corner.wall.lefttop.x, corner.wall.lefttop.y, 0, 0)
    love.graphics.line( corner.wall.righttop.x, corner.wall.righttop.y, corner.screen.right.x, 0)
    love.graphics.line(corner.screen.right.x, corner.screen.bottom.y, corner.wall.rightbottom.x, corner.wall.rightbottom.y)
    love.graphics.line(corner.wall.leftbottom.x, corner.wall.leftbottom.y, 0, corner.screen.bottom.y)
    love.graphics.rectangle("fill", player.x, player.y, player.width, player.height)
    love.graphics.rectangle("line", table.x, table.y, table.width, table.height)
end

function world:objects()
    player = {}
    player.height = 50
    player.width = 25
    player.speed = 200
    player.diagonalspeed = ((player.speed ^ 2) / 2) ^ 0.5
    player.x = (love.graphics.getWidth() / 2) - player.width
    player.y = (love.graphics.getHeight() * 3 / 4) - player.height
    table = {}
    table.width = 125
    table.height = 75
    table.x = (love.graphics.getWidth()/2) - table.width
    table.y = (love.graphics.getHeight()/2) - table.height
end

function world:coordinates()
    corner = {wall = {lefttop = {}, leftbottom = {}, righttop = {}, rightbottom = {}}, 
    screen = {bottom = {}, right = {}}
    
}
    corner.screen.bottom.y = love.graphics.getHeight()
    corner.screen.right.x = love.graphics.getWidth()
    corner.wall.lefttop.x = love.graphics.getWidth() / 16
    corner.wall.lefttop.y = love.graphics.getHeight() / 9
    corner.wall.leftbottom.x = love.graphics.getWidth() / 16
    corner.wall.leftbottom.y = (love.graphics.getHeight() * 8) / 9
    corner.wall.righttop.x = (love.graphics.getWidth() * 15)/ 16
    corner.wall.righttop.y = love.graphics.getHeight() / 9
    corner.wall.rightbottom.x = (love.graphics.getWidth() * 15)/ 16
    corner.wall.rightbottom.y = (love.graphics.getHeight() * 8) / 9
end

function world:move()
    if love.keyboard.isDown("a") and love.keyboard.isDown("s") then
        player.x = player.x - (player.diagonalspeed * dt)
        player.y = player.y + (player.diagonalspeed * dt)
    elseif love.keyboard.isDown("a") and love.keyboard.isDown("w") then
        player.x = player.x - (player.diagonalspeed * dt)
        player.y = player.y - (player.diagonalspeed * dt)
    elseif love.keyboard.isDown("d") and love.keyboard.isDown("s") then
        player.x = player.x + (player.diagonalspeed * dt)
        player.y = player.y + (player.diagonalspeed * dt)
    elseif love.keyboard.isDown("d") and love.keyboard.isDown("w") then
        player.x = player.x + (player.diagonalspeed * dt)
        player.y = player.y - (player.diagonalspeed * dt)
    elseif love.keyboard.isDown("a") and love.keyboard.isDown("s") == false then
        player.x = player.x - (player.speed * dt)
        player.y = player.y
    elseif love.keyboard.isDown("a") and love.keyboard.isDown("w") == false then
        player.x = player.x - (player.speed * dt)
        player.y = player.y
    elseif love.keyboard.isDown("d") and love.keyboard.isDown("s") == false then
        player.x = player.x + (player.speed * dt)
        player.y = player.y
    elseif love.keyboard.isDown("d") and love.keyboard.isDown("w") == false then
        player.x = player.x + (player.speed * dt)
        player.y = player.y
    elseif love.keyboard.isDown("w") and love.keyboard.isDown("d") == false then
        player.y = player.y - (player.speed * dt)
        player.x = player.x
    elseif love.keyboard.isDown("w") and love.keyboard.isDown("a") == false then
        player.y = player.y - (player.speed * dt)
        player.x = player.x
    elseif love.keyboard.isDown("s") and love.keyboard.isDown("d") == false then
        player.y = player.y + (player.speed * dt)
        player.x = player.x
    elseif love.keyboard.isDown("s") and love.keyboard.isDown("a") == false then
        player.y = player.y + (player.speed * dt)
        player.x = player.x
    end
end

function world:bounds()
    if player.x < (love.graphics.getWidth() / 16) then
        player.x = (love.graphics.getWidth() / 16)
    end
    if player.x > ((love.graphics.getWidth() * 15/ 16) - player.width) then
        player.x = ((love.graphics.getWidth() *15 / 16) - player.width)
    end
    if player.y < ((love.graphics.getHeight() / 9) - (player.height / 3)) then
        player.y = ((love.graphics.getHeight() / 9) - (player.height / 3))
    end
    if player.y > ((love.graphics.getHeight() * 8 / 9) - player.height)then
        player.y = ((love.graphics.getHeight() * 8 / 9) - player.height)
    end
    if player.x > ((love.graphics.getWidth() * 15/ 16) - player.width)then
        player.x = ((love.graphics.getWidth() *15 / 16) - player.width)
    end
    if player.y < ((love.graphics.getHeight() / 9) - (player.height / 3)) then
        player.y = ((love.graphics.getHeight() / 9) - (player.height / 3))
    end
    if player.y > ((love.graphics.getHeight() * 8 / 9) - player.height)then
        player.y = ((love.graphics.getHeight() * 8 / 9) - player.height)
    end
    if (player.x + player.width) > (table.x) and player.y >= table.y and player.y <= (table.y + table.height) then
        player.x = table.x - player.width
    end
    if player.x < (table.x + table.width) and player.y >= table.y and player.y <= (table.y + table.height) then
        player.x = table.x + table.width
    end
    if player.y > table.y and player.x >= table.x and player.x <= (table.s + table.width) then
        player.y = table.y - player.height
    end
    if player.y < table.y + table.height and player.x >= table.x and player.x <= (table.s + table.width) then
        player.y = (table.y + table.height) - (player.height / 3)
    end

end
Last edited by zam970 on Wed Mar 05, 2025 12:40 pm, edited 1 time in total.
CutePenguin
Prole
Posts: 18
Joined: Sat Dec 21, 2024 7:35 am

Re: Desperately need help finding what in my code is preventing it from running

Post by CutePenguin »

how is your game packaged anyway?
zam970
Prole
Posts: 2
Joined: Tue Mar 04, 2025 2:58 am

Re: Desperately need help finding what in my code is preventing it from running

Post by zam970 »

CutePenguin wrote: Tue Mar 04, 2025 6:22 pm how is your game packaged anyway?
In a folder with main.lua and my world.lua file at the same, root level. I've packaged other projects the same way and never had an issue like this.

After looking at the way it colored the code on here, I think the issue may be that I used the word 'table' as a variable, and I think it is linking to a { }. Will try another variable name and see if that fixes it.
loofy2
Prole
Posts: 8
Joined: Fri Mar 14, 2025 1:11 am

Re: Desperately need help finding what in my code is preventing it from running

Post by loofy2 »

table is a reserved word in Lua

checkout this example

Code: Select all

local mytable = { }
table.insert(mytable, "hello world")
print(mytable[1])
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Majestic-12 [Bot], rabbitboots and 6 guests