[solved]how to use "i" properly with nested for loops

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
ITISITHEBKID
Prole
Posts: 18
Joined: Tue Apr 24, 2018 7:37 pm

[solved]how to use "i" properly with nested for loops

Post by ITISITHEBKID »

This is the code for my collisions(everything in the game is a circle so far)

Code: Select all

	for i=#bullets,1,-1 do
			if bullets[i].y > love.graphics.getHeight() then 
						table.remove(bullets,i)
						break
			elseif bullets[i].y  < 0 then 
						table.remove(bullets,i)
						break
			end

			if bullets[i].x > love.graphics.getWidth() then 
				table.remove(bullets,i)
				break

			elseif bullets[i].x < 0 then 
				table.remove(bullets,i)
				break
			end
			
			bulleti = i

			for i=#enemies,1,-1 do
				print(i)
				if circliision(bullets[bulleti].x,bullets[bulleti].y,enemies[bulleti].x,enemies[bulleti].y) then
					print("HITTY TITTY TATOR TOT")
					table.remove(bullets,bulleti)
				end
			end
	end
The problem is that most of the time collision works fine, but sometimes bullets pass right through the enemy and I don't know why.
Attachments
Game.love
(482.31 KiB) Downloaded 48 times
Last edited by ITISITHEBKID on Wed Apr 25, 2018 11:04 pm, edited 3 times in total.
ITISITHEBKID
Prole
Posts: 18
Joined: Tue Apr 24, 2018 7:37 pm

Re: Why is collision spottty in my game?

Post by ITISITHEBKID »

I forgot to add this in the original: Here is the code where the above function is called.

Code: Select all

	for i=#bullets,1,-1 do
			if bullets[i].y > love.graphics.getHeight() then 
						table.remove(bullets,i)
						break
			elseif bullets[i].y  < 0 then 
						table.remove(bullets,i)
						break
			end

			if bullets[i].x > love.graphics.getWidth() then 
				table.remove(bullets,i)
				break

			elseif bullets[i].x < 0 then 
				table.remove(bullets,i)
				break
			end
			

			for i=#enemies,1,-1 do
				print(i)
				if circliision(bullets[bulleti].x,bullets[bulleti].y,enemies[bulleti].x,enemies[bulleti].y) then
					print("HITTY TITTY TATOR TOT")
					table.remove(bullets,bulleti)
				end
			end
	end
	


EDIT: I have figured out the problem: I am nesting two for loops, and when I call "i" for the bullets it uses the i for the enemies. Anyone know how to fix this?

EDIT 2: Updated the above code, which brings the error "attempt to index a nil value" when their is more than one bullet
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: how to use "i" properly with nested for loops

Post by ivan »

The code is too repetitive, you can clean it up like so:

Code: Select all

-- step one: remove bullets if off the screen
local w, h = love.graphics.getDimensions()
for i=#bullets,1,-1 do
  local b = bullets[i]
  if b.x > w or b.x < 0 or b.y > h or b.y < 0 then 
    table.remove(bullets,i)
  end
end
-- step two: check collision pairs
for i=#bullets,1,-1 do
  local b = bullets[i]
  for j=#enemies,1,-1 do
    local e = enemies[j]
    if circliision(b.x,b.y,e.x,e.y) then
      print("bullet:",i, "enemy:",j)
      table.remove(bullets,i)
      table.remove(enemies,j)
    end
  end
end
As you can see - you can use any variable name for the loop, it doesn't have start with: "for i="
Also note that we iterate in reverse only when we want to remove several elements without breaking the loop.
ITISITHEBKID
Prole
Posts: 18
Joined: Tue Apr 24, 2018 7:37 pm

Re: how to use "i" properly with nested for loops

Post by ITISITHEBKID »

Thank you, that worked perfectly! It's people like you that make this such a welcoming community :)
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: [solved]how to use "i" properly with nested for loops

Post by Jasoco »

"i" has always just been an example. You can use anything you want. "a", "b", "n", "bullet", "foo", "chopsuey". We just use "i" because it's been used so long. I'm sure there's an origin story.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: [solved]how to use "i" properly with nested for loops

Post by zorg »

i for index, most likely; k is key and v is value...
same with elem being short for element in specific places, and of course self
s string, sz zero terminated or asciiz was also used when utf-8 wasn't widespread / hasn't existed yet...
and from math lectures, n is number, usually ordinal (read: natural number, positive integers and maybe zero, that has the set symbol N) while x is any kind of number, including fractions (since it's like the first default letter people used to solve for) ... then again, x,y,z,w could be coordinates, and u and v being texture coordinates.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: baconhawka7x, Google [Bot] and 47 guests