Bump error "item object must be added..."

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.
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Bump error "item object must be added..."

Post by fridays18 »

Im using bump for my project and im trying to make a system where when it dies it removes itself from the world, the only problem is every time I try I get this error-
Screen Shot 2022-11-30 at 11.08.24 AM.png
Screen Shot 2022-11-30 at 11.08.24 AM.png (220.2 KiB) Viewed 1523 times
From a forum post a little while ago ill link below this says, I must keep the list out of the function to remove it, but I have no clue of how to do that when dealing with class's since im world:add(self) and world:remove(self)

Link : viewtopic.php?f=4&t=86556&p=226785&hili ... ed#p226785

Here is the code incase it can help
Bullet.lua-

Code: Select all

Bullet = Object:extend()

function Bullet:new(x, y)
    self.r = 1
    self.x = x
    self.y = y
    self.speed = 700
		self.w = 10
		self.h = 10
		self.path = 0
	self.vx = 0
	self.vy = 0
	self.dead = false


		if player.d == 4 then
    self.path = 4
	elseif player.d == 1 then
		self.path = 1
	elseif player.d == 2 then
		self.path = 2
	elseif player.d == 3 then
    self.path = 3
		end

		world:add(self, self.x, self.y, self.w, self.h)
end

function Bullet:update(dt)
		self.vx = 0
	self.vy = 0
	
	if self.x > screenx or self.x < 0 or self.y > screeny or self.y < 0 then
	self.dead = true
	end
	
	if self.path == 4 then
    self.vy = self.vy + self.speed * dt
	elseif self.path == 1 then
		self.vx = self.vx - self.speed * dt
	elseif self.path == 2 then
		self.vx = self.vx + self.speed * dt
	elseif self.path == 3 then
    self.vy = self.vy - self.speed * dt
		end

			self.x, self.y = world:move(self, self.x+self.vx, self.y+self.vy)

	local targetX, targetY = self.x+self.vx*dt, self.y+self.vy*dt
	local newX, newY = world:move(self, targetX, targetY)
	if newX == targetX and newY == targetY then -- no collision
		self.x, self.y = newX, newY
	else -- collision
		self.dead = true
	end

	if self.dead == true then
		world:remove(self)
	end

end

function Bullet:draw()
	if self.dead == true then
		love.graphics.circle("line",self.x,self.y,29)
	end
    --love.graphics.circle("fill", self.x, self.y,self.r)
	love.graphics.rectangle("line",self.x,self.y,self.w,self.h)
end


Any help would be much appreciated!
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Bump error "item object must be added..."

Post by darkfrei »

Don't forget to remove it from the list of bullets.
How you call the function Bullet:update(dt)?

The removing must be like

Code: Select all

for i = #Bullets, 1, -1 do -- backwards! No sequence changing
	local bullet = Bullets[i]
	if bullet.dead then
		table.remove (Bullets, i)
	end
end
OR

Code: Select all

for i = #Bullets, 1, -1 do -- backwards! But it changes the sequence 
	local bullet = Bullets[i]
	if bullet.dead then
		-- overwrite current element with the last element and remove last element:
		Bullets[i], Bullets[#Bullets] = Bullets[#Bullets] , nil
	end
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: Bump error "item object must be added..."

Post by fridays18 »

darkfrei wrote: Wed Nov 30, 2022 5:29 pm Don't forget to remove it from the list of bullets.
How you call the function Bullet:update(dt)?

The removing must be like

Code: Select all

for i = #Bullets, 1, -1 do -- backwards! No sequence changing
	local bullet = Bullets[i]
	if bullet.dead then
		table.remove (Bullets, i)
	end
end
OR

Code: Select all

for i = #Bullets, 1, -1 do -- backwards! But it changes the sequence 
	local bullet = Bullets[i]
	if bullet.dead then
		-- overwrite current element with the last element and remove last element:
		Bullets[i], Bullets[#Bullets] = Bullets[#Bullets] , nil
	end
end
I know how to remove it from the list my issue is just removing it from the bump list more than the normal one
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Bump error "item object must be added..."

Post by darkfrei »

fridays18 wrote: Wed Nov 30, 2022 10:27 pm
darkfrei wrote: Wed Nov 30, 2022 5:29 pm Don't forget to remove it from the list of bullets.
How you call the function Bullet:update(dt)?

The removing must be like

Code: Select all

for i = #Bullets, 1, -1 do -- backwards! No sequence changing
	local bullet = Bullets[i]
	if bullet.dead then
		table.remove (Bullets, i)
	end
end
OR

Code: Select all

for i = #Bullets, 1, -1 do -- backwards! But it changes the sequence 
	local bullet = Bullets[i]
	if bullet.dead then
		-- overwrite current element with the last element and remove last element:
		Bullets[i], Bullets[#Bullets] = Bullets[#Bullets] , nil
	end
end
I know how to remove it from the list my issue is just removing it from the bump list more than the normal one
The error message looks like that you have successfully removed it from world and on the next update it's not here anymore, but called.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: Bump error "item object must be added..."

Post by fridays18 »

darkfrei wrote: Thu Dec 01, 2022 11:59 am
fridays18 wrote: Wed Nov 30, 2022 10:27 pm
darkfrei wrote: Wed Nov 30, 2022 5:29 pm Don't forget to remove it from the list of bullets.
How you call the function Bullet:update(dt)?

The removing must be like

Code: Select all

for i = #Bullets, 1, -1 do -- backwards! No sequence changing
	local bullet = Bullets[i]
	if bullet.dead then
		table.remove (Bullets, i)
	end
end
OR

Code: Select all

for i = #Bullets, 1, -1 do -- backwards! But it changes the sequence 
	local bullet = Bullets[i]
	if bullet.dead then
		-- overwrite current element with the last element and remove last element:
		Bullets[i], Bullets[#Bullets] = Bullets[#Bullets] , nil
	end
end
I know how to remove it from the list my issue is just removing it from the bump list more than the normal one
The error message looks like that you have successfully removed it from world and on the next update it's not here anymore, but called.
Hm, in my update code it removes it from the world than the list so I dont see how it would be an issue since its all back to back
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Bump error "item object must be added..."

Post by darkfrei »

From line 47 of bullet.lua will be called the "move" function from bump.lua, but the bullet is not in the world.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: Bump error "item object must be added..."

Post by fridays18 »

Quick update I fixed the problem, I was using a system to set to to dead if it collided then later on I would call it and say is self.dead == true than remove, what fixed it is just directly implementing it in the initial system

Code: Select all

	if self.dead == false then
			self.x, self.y = world:move(self, self.x+self.vx, self.y+self.vy)

	local targetX, targetY = self.x+self.vx*dt, self.y+self.vy*dt
	local newX, newY = world:move(self, targetX, targetY)
	if newX == targetX and newY == targetY then -- no collision
		self.x, self.y = newX, newY
	else -- collision
		self.dead = true
			world:remove(self)
	end

			end
Thanks for the help!
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Bump error "item object must be added..."

Post by darkfrei »

Why you call update bullet by dead bullet?
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: Bump error "item object must be added..."

Post by fridays18 »

darkfrei wrote: Thu Dec 01, 2022 12:57 pm Why you call update bullet by dead bullet?
what do you mean?
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: Bump error "item object must be added..."

Post by fridays18 »

Now my enemy script is returning the same error

these are very confusing times

Code: Select all

--Makes enemy a class
Enemy = Object.extend(Object)

--calling enemy() plays this function
function Enemy.new(self,x,y)
    self.x = x
    self.y = y
    self.width = 10
    self.height = 10
	
    self.speed = math.random(50,70)
		self.health = math.random(1,50)
		self.dead = false
		self.check = false
		self.c1 = math.random(1,255)
		self.c2 = math.random(1,255)
		self.c3 = math.random(1,255)
		world:add(self, self.x, self.y, self.width, self.height)
	self.vx = 0
	self.vy = 0
	self.range = false

end

function Enemy.update(self, dt)
	--here

	self.vx = 0
	self.vy = 0

	self.range = CheckCollision(self.x - 20,self.y - 20 ,self.width + 40,self.height + 40,player.x,player.y,player.w,player.h)

if self.range == true then
	if self.x < player.x + 10 then
		self.vx = self.vx + self.speed * dt
	elseif self.x > player.x + 10 then
		self.vx = self.vx - self.speed * dt
	end

	if self.y < player.y + 10 then
		self.vy = self.vy + self.speed * dt
	elseif self.y > player.y + 10 then
		self.vy = self.vy - self.speed * dt
end
	end

		if self.dead == false then
		self.x, self.y = world:move(self, self.x+self.vx, self.y+self.vy)
	else
		world:remove(self)
	end
end

function Enemy.draw(self)
		love.graphics.push("all")
        love.graphics.setColor(self.c1,self.c2,self.c3,50)
    love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
	love.graphics.setColor(1, 1, 1)
        love.graphics.pop()

	love.graphics.rectangle("line",self.x - 20,self.y - 20 ,self.width + 40,self.height + 40)
end
Post Reply

Who is online

Users browsing this forum: Google [Bot], slime and 42 guests