Page 1 of 1
[bump.lua] world:remove doesn't work
Posted: Sun Apr 14, 2019 5:23 pm
by maxtrautwein
Hi! I'm using the collision library bump.lua and everything works well except world:remove, I can't remove any items from my world and I have the following error message "item table must be added to the world before getting its rect. Use world:add." but i have already used world:add to add my player to the world and it still doesn't work.
any ideas? thanks!
Code: Select all
function love.load()
world:add(red, red.x, red.y, red.w, red.h)
end
Code: Select all
function love.keypressed(key)
if key == 'q' then
world:remove(red)
end
end
Re: [bump.lua] world:remove doesn't work
Posted: Mon Apr 15, 2019 9:01 am
by MrFariator
Bump uses hard references to whatever you insert into the world with the add() function. As such, I suspect when you are trying to call remove() with your 'red' object, the 'red' is not the same exact object that you passed to the bump world previously. Are you deleting or recreating the table representing 'red' at some point before the remove() function gets invoked?
Re: [bump.lua] world:remove doesn't work
Posted: Mon Apr 15, 2019 10:02 am
by maxtrautwein
First thank you for your answer.
I have only this at the top of my code
Code: Select all
local red = {x=120, y=300, w=30, h=50, vx=0, vy=0, jump=false}
But I don't understand why it doesn't work, I also tried this inside love.keypressed as a test and it still doesn't work :
Code: Select all
test = {x=0,y=0,w=10,h=10}
if key == "c" then
world:add(test,test.x,test.y,test.w,test.h)
end
if key == "d" then
world:remove(test)
end
Re: [bump.lua] world:remove doesn't work
Posted: Mon Apr 15, 2019 10:33 am
by grump
You're creating a new 'test' table every time love.keypressed is called, and the one you've added before is lost.
This should work:
Code: Select all
local test = { x = 0, y... } -- outside the function!
function love.keypressed(key)
if key == "c" then
world:add(test,test.x,test.y,test.w,test.h)
end
if key == "d" then
world:remove(test)
end
end
Re: [bump.lua] world:remove doesn't work
Posted: Mon Apr 15, 2019 11:17 am
by maxtrautwein
Ho ok yes it works this time but with my other items it still doesn't work, here is an exemple of where I want to use world:remove, "tir" means bullet in french, these are some functions that create a bullet each time I press "space" and delete it when the bullet collide.
Code: Select all
local tirs = {liste={}}
-- TIRS --
function tirs.create(player)
local tir = {x=player.x+40, y=player.y+player.h/3, w=10, h=3}
table.insert(tirs.liste, tir)
world:add(tir,tir.x,tir.y,tir.w,tir.h)
return tir
end
function tirs.delete(tir)
local delete = false
for i = 1, len do
local col = cols[i]
if col.normal.x == -1 or col.normal.x == 1 or col.normal.y == -1 or col.normal.y == 1 then
world:remove(tir)
end
end
end
function tirs.update(dt)
for i = #tirs.liste, 1, -1 do
local tir = tirs.liste[i]
local vx = 0
vx = vx + 500*dt
tir.x, tir.y, cols, len = world:move(tir, tir.x + vx, tir.y)
tirs.delete(tir)
end
end
function tirs.draw()
for i = #tirs.liste, 1, -1 do
local tir = tirs.liste[i]
love.graphics.rectangle("line", tir.x, tir.y, tir.w, tir.h)
end
end
---
function game.keypressed(key)
if key == 'space' then
tirs.create(red)
end
end
Everything works fine until a bullet collide with another object from the world : "item table must be added to the world before getting its rect."
Re: [bump.lua] world:remove doesn't work
Posted: Mon Apr 15, 2019 12:36 pm
by MrFariator
From that code-snippet the problem seems to be rather simple: you successfully remove a bullet from the world as the conditions are true in tirs.delete, but you never remove the bullet from the table containing the list of bullets (tirs.liste), or even break the for-loop in which the world:remove() is called. Thus, immediately the next frame or next tick in the for-loop, after you have removed a bullet, it will try to call bump a function with that said bullet, resulting in an error.
What you probably meant to do was:
Code: Select all
function tirs.delete(tir, cols, len)
local delete = false
for i = 1, len do
local col = cols[i]
if col.normal.x == -1 or col.normal.x == 1 or col.normal.y == -1 or col.normal.y == 1 then
world:remove(tir)
return true -- return a value on successful removal, and break the loop
end
end
end
function tirs.update(dt)
for i = #tirs.liste, 1, -1 do
local tir = tirs.liste[i]
local vx = 0
vx = vx + 500*dt
tir.x, tir.y, cols, len = world:move(tir, tir.x + vx, tir.y)
if tirs.delete(tir, cols, len) then -- check for successful removal
table.remove(tirs.liste, i ) -- remove the bullet from the list
end
end
end
edit: corrected some mistakes in wording
Re: [bump.lua] world:remove doesn't work
Posted: Mon Apr 15, 2019 12:48 pm
by maxtrautwein
hoo ok now I understand, thank you very much it works perfectly! I