Removing Specific Subtables?

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
WolfNinja2
Party member
Posts: 150
Joined: Wed Oct 24, 2012 10:10 pm

Removing Specific Subtables?

Post by WolfNinja2 »

How can I do this, when I add the subtable it is set up with and X,Y,Direction and ID.
I was thinking maybe something like this

Code: Select all

table.remove(enemy)(ID)
????
I have no idea though :/

-Wolf
User avatar
keharriso
Citizen
Posts: 92
Joined: Fri Nov 16, 2012 9:34 pm

Re: Removing Specific Subtables?

Post by keharriso »

If you mean to say that enemy.ID is set to some value, then all you have to do is:

Code: Select all

enemy.ID = nil
Just set a field to nil to get rid of it!

If this isn't what you meant, it'd be useful to get a more specific description. Show the code where you create and add the subtable.
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Removing Specific Subtables?

Post by Qcode »

I don't see a good reason to use table.remove in this situation. Table.remove is mainly meant for tables where you're not defining each value with a string key. When you use table.remove, it removes the value in the table specified, then moves all the other tables down one to compensate. Since you don't need to do this, as you're using keys such as x, y, direction and id it would be much easier to just set id to nil.
Try and see if this bit of code works:

Code: Select all

for i = #enemy, 1, -1 do
     enemy[i].ID = nil
end
~Qcode

Edit: Ninja'd by keharriso. Oh well.
User avatar
WolfNinja2
Party member
Posts: 150
Joined: Wed Oct 24, 2012 10:10 pm

Re: Removing Specific Subtables?

Post by WolfNinja2 »

When the enemy is created it looks like this, in fact here is the whole enemy code.

Code: Select all

enemy = {}

function enemycreate(x,y,direction,id)
	table.insert(enemy,{x=x,y=y,direction=direction,id=id,speed = 240,h = 32,w = 32})
end

function enemyupdate(dt)
	for i,v in ipairs(enemy) do
   if v.direction == "right" then
      v.x = v.x + v.speed * dt
   end
   if v.direction == "left" then
     v.x = v.x - v.speed * dt
		end
	end
end

function enemycollide(dt)
	for i,v in ipairs(enemy) do
	
		E_timedspeed = v.speed * dt
	
		if E_mapCollide(v.x - E_timedspeed, v.y + 1) or B_mapCollide(v.x - E_timedspeed, v.y + v.h - 1) then
		v.x = math.floor(v.x / tiledivisor) * tiledivisor
		v.direction = "right"
		v.x = v.x + 32
		end
		if E_mapCollide(v.x + E_timedspeed + v.w, v.y + 1) or B_mapCollide(v.x + E_timedspeed + v.w, v.y + v.h - 1) then
		v.x = (math.floor((v.x + v.w + E_timedspeed) / tiledivisor) * tiledivisor) - v.w
		v.x = v.x - 32
		v.direction = "left"
		end
	end
end

function enemy_draw()
for i,v in ipairs(enemy) do
	g.setColor(255,255,255,255)
	g.draw(E, v.x, v.y, 0, 1, 1)
	end
end

function enemy_die(dt)
	for i,v in ipairs(enemy) do
		if B_x > v.x and
			B_x < v.x + v.w and
			B_y > v.y and
			B_y < v.y + v.h then
			print "hyeap"
			if v.id == 1 then
			table.remove(enemy,1)
			end
			if v.id == 2 then
			table.remove(enemy,2)
			end
		end
	end
end
And here is where they are spawned.

Code: Select all

if gamestate == "lvl 3" and
	enemyloaded == false then
	enemycreate(128,32,"right",1)
	enemycreate(256,32,"left",2)
	enemyloaded = true
	end
Sorry I was so unspecific :/

EDIT: This coding removes one of the enemies but the other doesn't die.... Just thought it may be helpful to know.

-Wolf
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Removing Specific Subtables?

Post by Qcode »

Have you tried both of our solutions? Because that error sounds like something that would happen without using a reverse loop. Try my code if you haven't.
User avatar
WolfNinja2
Party member
Posts: 150
Joined: Wed Oct 24, 2012 10:10 pm

Re: Removing Specific Subtables?

Post by WolfNinja2 »

Qcode wrote:Have you tried both of our solutions? Because that error sounds like something that would happen without using a reverse loop. Try my code if you haven't.
Now niether remove :/
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Removing Specific Subtables?

Post by Qcode »

Are you trying to remove the id or the actual enemy table? If you're trying to remove the table why don't you just have an v.remove value?
User avatar
WolfNinja2
Party member
Posts: 150
Joined: Wed Oct 24, 2012 10:10 pm

Re: Removing Specific Subtables?

Post by WolfNinja2 »

so

if v.remove == true then

..... How would i end it ( exactly )
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Removing Specific Subtables?

Post by Qcode »

I'd do the remove function like this:

Code: Select all

for i = #enemy, 1, -1 do
     if enemy[i].remove then
          table.remove(enemy, i)
     end
end
Is that what you're looking for?
User avatar
WolfNinja2
Party member
Posts: 150
Joined: Wed Oct 24, 2012 10:10 pm

Re: Removing Specific Subtables?

Post by WolfNinja2 »

Qcode wrote:I'd do the remove function like this:

Code: Select all

for i = #enemy, 1, -1 do
     if enemy[i].remove then
          table.remove(enemy, i)
     end
end
Is that what you're looking for?
I'll try it, now what does all that mean? for i = #enemy, (this calls the table right? ) but then after that what does 1,-1 mean
and enemy.remove is that just checking if v.remove is true?
Sorry for being a nuisance ;/

-Wolf
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 55 guests