Page 1 of 1

I always get this error code

Posted: Wed May 01, 2024 12:11 am
by Navi
So, when i try to do some movment i always get the error saying that squarex,squarey or squarespeed is equivalent to nil.

here my code:

Code: Select all

local square = {
    squarex = 0,
    squarey = 0 ,
    squarespeed = 100
}
function love.draw()
    love.graphics.setColor(0,0,1)
    love.graphics.rectangle('fill',square.squarex,square.squarey,50,60)
end
function love.update(dt)
    if love.keyboard.isDown('right') then
    squarex = squarex + square * dt
    end
end
If someone can help me out thanks! :D

Re: I always get this error code

Posted: Wed May 01, 2024 1:26 am
by BrotSagtMist
" squarex = squarex + square * dt"
This line makes no sense whatsoever. Yure multiplying by a table.

Re: I always get this error code

Posted: Wed May 01, 2024 8:17 pm
by TyperWithS
You want to rewrite: "squarex = squarex + square * dt"
To: "squarex = squarex + square.squarespeed * dt"

Re: I always get this error code

Posted: Wed May 01, 2024 8:24 pm
by Azzla
Change your update function to this:

Code: Select all

function love.update(dt)
    if love.keyboard.isDown('right') then
    	square.squarex = square.squarex + square.squarespeed * dt
    end
end
You need to access the properties of the table like you are in the draw function. Also consider the following:

Code: Select all

local square = {
    x = 0,
    y = 0,
    speed = 100
}
So that your code is easier to read and you don't have to redundantly type 'square' all the time.