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

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
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

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

Post 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)
MrFariator
Party member
Posts: 563
Joined: Wed Oct 05, 2016 11:53 am

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

Post 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.
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

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

Post 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'
MrFariator
Party member
Posts: 563
Joined: Wed Oct 05, 2016 11:53 am

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

Post 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)
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests