Page 1 of 1

I finnally started using bump.lua for my collisions, however I still have problems understanding it

Posted: Fri May 12, 2023 9:56 pm
by MaxGamz
I decided to use this since it's perfect for my platform game and it's beginner friendly. However, I came across some problems with figuring out how to use it. Specifically, how to use it for my tilemap. I tried using this function to try to add collidable blocks, however, it doesn't seem to work and gives me errors when I execute it.

function:

Code: Select all

local function addBlock(x,y,w,h)
    local block = {x=x,y=y,w=w,h=h}
    blocks[#blocks + 1] = block
    world:add(block, x,y,w,h)
end
implementation (in the main.load function)

Code: Select all

    addBlock(50, 100, 200, 32)
    addBlock(150, 100, 100, 32)

Re: I finnally started using bump.lua for my collisions, however I still have problems understanding it

Posted: Fri May 12, 2023 10:17 pm
by MrFariator
What's the error? Always make sure to attach an error log if you want help. The code you posted doesn't really tell us anything, assuming the variables "blocks" and "world" actually exist.

Re: I finnally started using bump.lua for my collisions, however I still have problems understanding it

Posted: Fri May 12, 2023 11:24 pm
by MaxGamz
MrFariator wrote: Fri May 12, 2023 10:17 pm What's the error? Always make sure to attach an error log if you want help. The code you posted doesn't really tell us anything, assuming the variables "blocks" and "world" actually exist.

this is the error I received:

Error

main.lua:10: attempt to get length of upvalue 'blocks' (a nil value)


Traceback

[love "callbacks.lua"]:228: in function 'handler'
main.lua:10: in function 'addBlock'
main.lua:21: in function 'load'
[love "callbacks.lua"]:136: in function <[love "callbacks.lua"]:135>
[C]: in function 'xpcall'
[C]: in function 'xpcall'

Re: I finnally started using bump.lua for my collisions, however I still have problems understanding it

Posted: Sat May 13, 2023 12:45 am
by MrFariator
As it says, your "blocks" variable is undefined (nil value). If you try to treat an undefined variable like a table, then you're bound to get errors like that. You'll need to do something like this in your code to make it work (assuming you have defined "world" somewhere else in your code):

Code: Select all

local blocks = {} -- define the blocks table
local function addBlock(x,y,w,h)
    local block = {x=x,y=y,w=w,h=h}
    blocks[#blocks + 1] = block
    world:add(block, x,y,w,h)
end
addBlock(50, 100, 200, 32)
addBlock(150, 100, 100, 32)
Do note that if your "blocks" variable is a local, it needs to be defined in the scope above the addBlock function. Eq, the following would result in a similar error:

Code: Select all

local function addBlock(x,y,w,h)
    local block = {x=x,y=y,w=w,h=h}
    blocks[#blocks + 1] = block -- the code can't find the definition of "blocks", so you'll get a "indexing a nil value" error here
    world:add(block, x,y,w,h)
end
local blocks = {}
addBlock(50, 100, 200, 32)
addBlock(150, 100, 100, 32)