Iteration

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
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Iteration

Post by mangadrive »

I've watched several videos and tried multiple tutorials, but I just don't understand how to use iteration to a better advantage. The problem I keep facing is that:

Code: Select all

for i,v in ipairs(enemies) do
love.graphics.rectangle("fill", v.x, v.y, v.width, v.height)
end

I understand this would draw a rectangle for EACH enemy in that list. ie: 5 enemies = 5 rectangles.

I do not understand how to iterate a list/table/array that would look for a single value to be present then act based upon that. Example

Code: Select all


enemies = {}

enemy = {
x = 0,
y = 0,
table.insert (enemies, enemy)
}

enemy2 = {
x = 32,
y = 0,
table.insert (enemies, enemy2)
}

for i,v in ipairs(enemies) do

if v.x == enemy.x + 32 then
enemy.y = enemy.y + 32
end

In this basic example since there was an enemy at 32, it moves enemy's y + 32 , but it does it twice because there are 2 enemies in the list?

Perhaps I'm trying to use this all wrong, but in mind I want to be able to look in the Enemies table, and if an enemy is @ 32 , then perform another action ONCE. Not for each enemy..

If this makes any sense.
scutheotaku
Party member
Posts: 235
Joined: Sat Dec 15, 2012 6:54 am

Re: Iteration

Post by scutheotaku »

I'm not exactly sure what you're trying to do...but I wrote an example based on your code. In the example, it creates two enemies (storing them directly into the enemies table). It then iterates through the enemies table. If, during the iterator loop, it finds that the x position of one of the entries/instances in the enemies table is equal to 32, it moves that instance (and only THAT one) along the y-axis by 32 pixels.

Code: Select all

--create enemies table
enemies = {}

--create first instance of the enemy, storing it directly into the enemies table
enemies[1] = {
	x = 0,
	y = 0
}

--create the second instance of the enemy, also storing it directly into the enemies table
enemies[2] = {
	x = 32,
	y = 32
}

--iterate through enemy instances
for i,v in ipairs(enemies) do
	--if this instance's x equals 32...
	if (v.x == 32) then
		--then move it, and only it, 32 pixels down the y-axis
		v.y = v.y + 32
	end
end
As a side note, the "table.insert" function is slow. Instead of using table.insert to add a new entry to a table, try this...

Code: Select all

--instead of this...
table.insert(myTable, foo)

--...do this:
myTable[#myTable + 1] = foo

--they have exactly the same result, except that the latter is much faster.
--also, just so you know, #myTable returns the number of entries in the table myTable. 
User avatar
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Re: Iteration

Post by mangadrive »

Thanks for the perspective. I'm going to see if this works better for me.
scutheotaku
Party member
Posts: 235
Joined: Sat Dec 15, 2012 6:54 am

Re: Iteration

Post by scutheotaku »

mangadrive wrote:Thanks for the perspective. I'm going to see if this works better for me.
Ok, great. Just post here if you have any other questions :)
User avatar
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Re: Iteration

Post by mangadrive »

Ok, so now..I can trigger this for an "if' only once,

but If I use an Else or else if, I have the same problem of the action performing for each enemy in the list.

Example

Code: Select all

if (enemy.x + 96 <= 384 and v.x == enemy.x + 96 and v.y == enemy.y) then -- Check Horizonal , if occupied -- Move Vertical
checkVerticalEnemy()  --- This only performs once (yay!)
else
enemy.x = enemy.x + 96  <------- This performs as many times as the the number of enemies in the list (??)
end
Again I'm not even certain I'm using this right, but I see examples in other sources and tutorials and what not using this type of logic.

I'm trying to make a simple "checkers" type board game. So i need some decent AI to look at each tile if it wants to move... thus I'm trying to use the enemy list to see if the grid is taken up by enemies before it does move.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Iteration

Post by Taehl »

Your problem is that you keep referring to "enemy", which is one specific enemy in the table. When you iterate, you should be looking only at "v", which means "the thing I'm iterating on right now".
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
mangadrive
Prole
Posts: 32
Joined: Sat Nov 17, 2012 10:26 pm

Re: Iteration

Post by mangadrive »

Thanks, I finally did realize that after working in the console a bit.

I've been over thinking loops to a great degree.

I fixed most of these problems by simply creating an array with each tile's x/y on it and just flagged it occupied/non occupied. It will get it's current position and compare with a longer list of possibilities. It seems the longer way to do it, but it's working the way i need/want it to and it's a bit more modular for logic later.

(Also I'm sorry this post is in the wrong place.. Just realized)
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 82 guests