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.
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.
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.
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.
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.
(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:
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.
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.