Page 1 of 2

Removing Specific Subtables?

Posted: Fri Nov 23, 2012 10:26 pm
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

Re: Removing Specific Subtables?

Posted: Fri Nov 23, 2012 11:00 pm
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.

Re: Removing Specific Subtables?

Posted: Fri Nov 23, 2012 11:00 pm
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.

Re: Removing Specific Subtables?

Posted: Sat Nov 24, 2012 4:46 am
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

Re: Removing Specific Subtables?

Posted: Sat Nov 24, 2012 4:54 am
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.

Re: Removing Specific Subtables?

Posted: Sat Nov 24, 2012 5:13 am
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 :/

Re: Removing Specific Subtables?

Posted: Sat Nov 24, 2012 5:37 am
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?

Re: Removing Specific Subtables?

Posted: Sat Nov 24, 2012 6:19 am
by WolfNinja2
so

if v.remove == true then

..... How would i end it ( exactly )

Re: Removing Specific Subtables?

Posted: Sat Nov 24, 2012 6:53 am
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?

Re: Removing Specific Subtables?

Posted: Sat Nov 24, 2012 6:11 pm
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