Advanced Table Stuff[Solved][Thanks!]

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
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Advanced Table Stuff[Solved][Thanks!]

Post by baconhawka7x »

I am making some major progress on my game code(however we have taken a further step wayyy back on the art), viewtopic.php?f=5&t=7292

I just got the bullets working correctly. But I am unsure how to make them hurt the zombie. I know usually how to do collision. But I am going to need the x values from two different tables to do so. How would I do that? Obviously I just wanna say something like

Code: Select all

if the bullets x position > the zombies x position and
the bullets x position < the zombies x position + 48 and
the bullet y postition > the zombies y position and
the bullets y position < the zombies y position + 48 then
       table.remove(bullet, i)
       zombies health = zombies health - steve.damage(steve is the player)
EDIT: The reason I can't do this, is because I don't know how to use the bullets x(or y) variable, and the zombies x(or y) variable at the same time.

Also I noticed while testing my game, that the zombies tend to overlap alot. And I want to stop that from happening. But I can't just say...

Code: Select all

for i,v in ipairs(zombie) do 
        if v.x > v.x then
               v.x = v.x
        end
end
So how would I go about doing a collision between two identical objects in the same table?

Thanks In advanced:)
Attachments
Archive.love
The art Sucks haha.
(87.85 KiB) Downloaded 51 times
Last edited by baconhawka7x on Tue Jan 17, 2012 1:10 am, edited 2 times in total.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Advanced Table Stuff.

Post by MarekkPie »

I am confused by why you can't just do what you think you can for the first part.

For the second part, loop through that same table again, and compare one to the other, while skipping the currently selected one. This WILL slow your game down tremendously if you plan on having a significant number of zombies loaded at one time. (Haven't really learned the big O yet, but I think this would be O(n^2 - 1)).

Code: Select all

for i1,v1 in pairs(zombies) do
  for i2,v2 in pairs(zombies) do
    if v1 ~= v2 then  -- <== this works since it compares their location in memory
      -- compare the bounding rectangles and move accordingly
    end
  end
end
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Advanced Table Stuff.

Post by The Burrito »

I haven't actually looked at your code but the first can be done more or less how you wanted to, by using nested loops like this:

Code: Select all

for bIter, bullet in ipairs(bullets) do
	for zIter, zombie in ipairs(zombies) do
		if bullet.x > zombie.x  and
		bullet.x < zombie.x + 48 and
		bullet.y > zombie.y and
		bullet.y < zombie.y +48 then
		zombie.health = zombie.health - steve.damage
		break
		flag bullet for deletion
		end
	end
end
You shouldn't mess with the tables while you're iterating through them, so it's best to kill the inner loop, process the remaining bullets, and then remove them afterwards. (I think thats right, done from memory)

MarekkPie's solution for the second problem would work, but you should probably just make them incapable of walking on top of each other in the first place by checking if theres a zombie in front of them before updating their position.

As a side note, ipairs is generally the best way to iterate through tables, but its also slow, so consider alternatives especially when dealing with large tables.
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Re: Advanced Table Stuff.

Post by baconhawka7x »

Ok, well I temporarily printed the zombies health above the zombies head to make sure the bullet collision was working. And it is(I know because the zombies health goes down). But for some reason the bullets won't dissapear, I'll put up the download on this comment.
Attachments
Archive.love
DOWNLOAD
(87.91 KiB) Downloaded 48 times
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Advanced Table Stuff.

Post by The Burrito »

baconhawka7x wrote:for some reason the bullets won't dissapear
well for one i is not defined with your loop, but also like I said you don't want to mess with the table while you're iterating through it, bad things will happen.

Try this:

Code: Select all

local delTable = {}
	for bIter, bullet in ipairs(bullet) do
		for zIter, zombie in ipairs(zombie) do
			if bullet.x > zombie.x and
			bullet.x < zombie.x + 48 and
			bullet.y > zombie.y and
			bullet.y < zombie.y + 48 then
				table.insert(delTable, bIter)
				zombie.health = zombie.health - steve.damage
				break --this stops the inner loop so the bullet doesn't hit multiple zombies
			end
		end
	end
	for i = # delTable,1,-1 do
	table.remove(bullet,delTable[i])
	end
Note that there is a separate loop to delete the bullets, and it iterates through the table in reverse order, so it deletes bullets from the end of the table first. table.remove is like pulling something out of a stack, everything below the removed entry is now 1 closer to the top, if you start at the top the whole rest of the table is going to get shuffled around.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 184 guests