I suck at tables! Help!!

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.
Mangl
Prole
Posts: 8
Joined: Thu Aug 29, 2013 8:14 pm

I suck at tables! Help!!

Post by Mangl »

Hi guys, I'm new around these parts! I've been using Love for a few weeks and run into my first problem.. Which is my poor table comprehension.

To cut to the chase: I'm working on code for pickups.. and whilst I can successfully create pickups and have the player pick them up there's a problem! If I create 4 pickups, and have the player pick up #2, then #3 and #4 will also be picked up simultaneously.

Any pointers on tables, how to number them and remove them properly would be much appreciated!

Code: Select all

pickup = {}

	function pickup:create(id, x, y, number)
		table.insert(pickup, {id = id, x = x, y = y})
	end

function pickup:update()
	for i,v in ipairs(pickup) do
		if player.x > v.x - 16 and player.x < v.x + 16 and player.y > v.y - 16 and player.y < v.y + 16 then
			print("Picked up")
			--dostuff
			pickup:destroy()
		end
	end
end

function pickup:draw()
	for i,v in ipairs(pickup) do
		if v.id == 0 then
			love.graphics.draw(pickup0, v.x, v.y)
		elseif v.id == 1 then
			love.graphics.draw(pickup1, v.x, v.y)
		end
	end
end

function pickup:destroy()
	table.remove(pickup)
end
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: I suck at tables! Help!!

Post by micha »

To remove an entry from a talbe you need to call the table.remove with the tables name and the index of the entry to remove. You only handle the table name.

Code: Select all

function pickup:update()
  for i,v in ipairs(pickup) do
    if player.x > v.x - 16 and player.x < v.x + 16 and player.y > v.y - 16 and player.y < v.y + 16 then
      print("Picked up")
      --dostuff
      table.remove(pickup,i)
    end
  end
end
This will then cause another problem: If you have pickup 1,2 and 3 and remove the first one, then you are left with 2 and 3 which get rearrange to 1 and 2. That confuses the for-loop. To avoid this problem, traverse the table backwards:

Code: Select all

for i = #pickup,1,-1 do
  v = pickup[i]
  -- your suff
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: I suck at tables! Help!!

Post by Robin »

Or better:

Code: Select all

for i = #pickup,1,-1 do
  local v = pickup[i]
  -- your suff
end
This way you don't leak v outside of the relevant scope.
Help us help you: attach a .love.
Mangl
Prole
Posts: 8
Joined: Thu Aug 29, 2013 8:14 pm

Re: I suck at tables! Help!!

Post by Mangl »

Ahh! I knew I was missing something, this helps a lot thanks guys! Works perfectly now! :awesome:
Mangl
Prole
Posts: 8
Joined: Thu Aug 29, 2013 8:14 pm

Re: I suck at tables! Help!! :O

Post by Mangl »

Is there any way I can create a for statement that looks in more than one ipairs table simultaneously? I've thoroughly searched .lua documentation and intensively googled and haven't come up with anything.

Just for an example: If I use ipairs tables for both bullets and enemies, and I want to check for collisions, how would I go about that? I've tried using one for statement within another.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: I suck at tables! Help!!

Post by Nixola »

That's what you should do if you want to check each element of one table agains each of the other
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Mangl
Prole
Posts: 8
Joined: Thu Aug 29, 2013 8:14 pm

Re: I suck at tables! Help!!

Post by Mangl »

Thanks for clarification!
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: I suck at tables! Help!!

Post by substitute541 »

Mangl wrote:Is there any way I can create a for statement that looks in more than one ipairs table simultaneously? I've thoroughly searched .lua documentation and intensively googled and haven't come up with anything.

Just for an example: If I use ipairs tables for both bullets and enemies, and I want to check for collisions, how would I go about that? I've tried using one for statement within another.
You would use a double for-loop (of course.)

Code: Select all

for i, enemy in ipairs(enemies) do
    for j, bullet in ipairs(bullets) do
        if collides(enemy, bullet) then
            enemy.explode()
        end
    end
end
Edit: Didn't read the bottom paragraph, nevermind.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: I suck at tables! Help!!

Post by davisdude »

You just have to have different iterator names. Iterators are the things before the comma (usually i).
Then it's just a matter of the tables.

Code: Select all

for i, b in ipairs( bullet ) do
    for ii, e in ipairs( enemy ) do
        -collisions
    end
end
Two things:
A lot of people have trouble with remembering which letter goes with which table. If that's the case, make names that make sense to you! :awesome:
I personally, find it easiest to go by vowels for my iterators, and first letter of table. For example:

Code: Select all

for a, b in ipairs( bullet ) do
    for e, enemy in ipairs( enemy ) do
        --collision detection
    end
end
Ultimately, it's up to you, though.

The other thing:
If you're super detail oriented like I am, check out my circle-rectangle collision for your bullet collision.
That was my first post, so the coding is a bit messy (sorry about that :P )
Post: http://www.love2d.org/forums/viewtopic.php?f=4&t=33493

Edit: beat by substitute. :P
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: I suck at tables! Help!!

Post by Robin »

davisdude wrote:iterators
Nope, what you're talking about is not an iterator. They are indices (plural of index). An iterator is the thing returned by ipairs.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests