for loop seems to not work quite right

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.
Delta9
Prole
Posts: 11
Joined: Mon Nov 12, 2012 5:01 am

for loop seems to not work quite right

Post by Delta9 »

What is intended here is that each animal in the table spawned moves once, either left, right, up, or down. Seems simple, code works fine with a couple animals, but once I get to a larger number, say 30 animals, they will begin moving at diagonals and skipping spaces. `test` was something I threw in to track this problem, but it has not helped. Hoping some more experienced eyes can see me through this one.

Code: Select all

function aimove()
  if state == "map" then	
	for i in ipairs(spawned) do
	an = spawned[i]	
	
		if an.x > 2 and an.y > 2 and an.x < mapWidth - 3 and an.y < mapHeight - 3 then
			dir = math.random(1,4)
			if dir == 1 then an.x = an.x-1 test = test+1 end
			if dir == 2 then an.x = an.x+1 test = test+1 end
			if dir == 3 then an.y = an.y-1 test = test+1 end
			if dir == 4 then an.y = an.y+1 test = test+1 end
			 
			
			elseif an.x > 2 and an.y > 2 and an.x < mapWidth - 3 and an.y == mapHeight - 3 then
			dir = math.random(3)
			if dir == 1 then an.x = an.x-1 end
			if dir == 2 then an.x = an.x+1 end
			if dir == 3 then an.y = an.y-1 end
			 test = test+1
			
			elseif an.x > 2 and an.y == 2 and an.x < mapWidth - 3 and an.y < mapHeight - 3 then
			dir = math.random(3)
			if dir == 1 then an.x = an.x-1 end
			if dir == 2 then an.x = an.x+1 end
			if dir == 3 then an.y = an.y+1 end
			 test = test+1

			
			elseif an.x > 2 and an.y > 2 and an.x == mapWidth - 3 and an.y < mapHeight - 3 then
			dir = math.random(3)
			if dir == 1 then an.x = an.x-1 end
			if dir == 2 then an.y = an.y-1 end
			if dir == 3 then an.y = an.y+1 end
			 test = test+1

			
			elseif an.x == 2 and an.y > 2 and an.x < mapWidth - 3 and an.y < mapHeight - 3 then
			dir = math.random(3)
			if dir == 1 then an.x = an.x+1 end
			if dir == 2 then an.y = an.y-1 end
			if dir == 3 then an.y = an.y+1 end
			 test = test+1
			
			
			an.oldx = an.x
			an.oldy = an.y
			end
		end
	end
end
Seems simple. Am I wrong? Thanks in advance, you guys are all awesome.
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: for loop seems to not work quite right

Post by substitute541 »

---------------------------------------------------------------------[--][O][X]
Debugging code.......
Error!

Expected "," after "i" in for i in ipairs(spawned) do.

Closing debugger model 541...
Closed.
-------------------------------------------------------------------------------

Simple...
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: for loop seems to not work quite right

Post by Boolsheet »

substitute541 wrote:Expected "," after "i" in for i in ipairs(spawned) do.
What kind of debugger is that? It's completely valid to only have one thing for the namelist in the for statement.

The only problem I can see is that things might get stuck in the corners. :?
Shallow indentations.
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

Re: for loop seems to not work quite right

Post by Przemator »

OK, but why do you do this:

Code: Select all

for i in ipairs(spawned) do
   an = spawned[i]   
When it could be done like this:

Code: Select all

for index, value in ipairs(spawned) do
   -- no need for an, as value stores spawned[i]   
Delta9
Prole
Posts: 11
Joined: Mon Nov 12, 2012 5:01 am

Re: for loop seems to not work quite right

Post by Delta9 »

Didn't know that one thanks, this is my first project working with Lua, learning on the fly. I am actually pretty certain that whole block could be shrunken a lot and coded much nicer. I still have not found my solution, it seems to become almost random movement within a few tiles each move. Most evident is the fact I did not allow for any diagonal movement (though I plan to) and yet they uniformly move diagonally after 10 or so animals spawn.

It acts as if it is allowing more than one move for each call. Just based on the code I posted is that even possible? Looks airtight to me, but as I said, I'm new. Thanks again for the help.
Delta9
Prole
Posts: 11
Joined: Mon Nov 12, 2012 5:01 am

Re: for loop seems to not work quite right

Post by Delta9 »

Okay, so I think I have targeted the problem. Not at home, so I don't have my current code available, but it looks kinda like this. I have a table of animals.

Code: Select all

animals = {
          [0] = {blah blah blah},
          [1] = {blah blah blah}
                }
I am pretty sure my problem lies in building my second table containing the spawned animals. Can someone give me an example of inserting an instance of something to a table? I mean an instance like if I have 2 sheep in play each one moves independantly and changes are NOT reflected back to the original copy in my Animals table.

I am reasonably sure this is where I am getting the extra moves, if more than one instance of an animal is out. This would also explain why my test variable stays constantly correct even though animals are clearly moving too much.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: for loop seems to not work quite right

Post by Robin »

I can't tell without the actual code, but is seems like you do this:

Code: Select all

instance = prototype
(instance and prototype are example names, I don't know what you used exactly)
That makes instance a "reference" to the same table as prototype. What you probably want is something like:

Code: Select all

instance = setmetatable({}, {__index = prototype})
Help us help you: attach a .love.
Delta9
Prole
Posts: 11
Joined: Mon Nov 12, 2012 5:01 am

Re: for loop seems to not work quite right

Post by Delta9 »

That does look promising, going to put it to work in a few minutes, see if that does it. If I need further help, code will be supplied. Thanks a lot Robin, I see you trying to help on almost every post I read, obviously a very kind and generous person.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: for loop seems to not work quite right

Post by Robin »

Delta9 wrote:Thanks a lot Robin, I see you trying to help on almost every post I read, obviously a very kind and generous person.
Glad to help. :)
Help us help you: attach a .love.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: for loop seems to not work quite right

Post by miko »

Delta9 wrote:What is intended here is that each animal in the table spawned moves once, either left, right, up, or down. Seems simple, code works fine with a couple animals, but once I get to a larger number, say 30 animals, they will begin moving at diagonals and skipping spaces.
How do you know that? Have you labelled the objects, maybe you are comparing different items?
Anyways, here is attachment with your (version 1 - press "1") and my (version 2 - press "2") implementation of aimove. Entities move each second, each entity is labelled with a number, and the line is drawn from their last position.
Attachments
aimove.love
(1.02 KiB) Downloaded 121 times
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 7 guests