Page 1 of 1
[Solved]For loop runnning longer than it's supposed to
Posted: Sat May 26, 2018 6:32 pm
by RandomUserName
This function is passed two Numbers, position is between 1 to 10,000 and range is between 1 to 10.
When I call the function the game locks up and eventually just closes, presumably because it fills up its Share of Memory.
The length of radiusold is 1, however when I put a "print(i)" in the for loop it shows that the loop increments i until it the Program closes.
Before you ask, the function is unfinished, I will wrap the loop in another one to extend the Range.
Code: Select all
function unitMovementRadius(position,range)
local radius={}
local radiusold={}
table.insert(radius,position)
radiusold=radius
for i,position in ipairs(radiusold) do
table.insert(radius,position+1)
table.insert(radius,position-1)
table.insert(radius,position+map.width)
table.insert(radius,position-map.width)
if position % 2 == 0 then
table.insert(radius,position+map.width+1)
table.insert(radius,position+map.width-1)
else
table.insert(radius,position-map.width+1)
table.insert(radius,position-map.width-1)
end
end
return radius
end
Am I missing something or is this an Issue with Lua?
Re: For loop runnning longer than it's supposed to
Posted: Sat May 26, 2018 6:45 pm
by zorg
What do you expect this code to do exactly? Only after helping us understand it can we help you, since anything could be an issue.
Re: For loop runnning longer than it's supposed to
Posted: Sat May 26, 2018 6:55 pm
by RandomUserName
I expect this code to define two local tables, insert the first Argument at the first position in the first table.
Copy the modified table onto the other one.
Right now the for loop should only go through one Interation, where it inserts a bunch of values into the first table and return it in the end.
Essentialy I want to supply a position and a range and get a table of the tiles within the specified range around the position
Re: For loop runnning longer than it's supposed to
Posted: Sat May 26, 2018 7:16 pm
by zorg
it won't copy it as you might believe; tables are references; if you want to have two separate tables still, then you need to "deepcopy" the keys and values from one table into the other, one by one (preferrably in a loop), although in this case, doing another table.insert on the other table, and getting rid of the assignment would work, i think.
Re: For loop runnning longer than it's supposed to
Posted: Sat May 26, 2018 7:31 pm
by RandomUserName
Ok so the following code does what I want:
Code: Select all
function unitMovementRadius(position,range)
local radius={}
local radiusold={}
table.insert(radius,position)
for i=1,range do
for j,position in ipairs(radius) do
table.insert(radiusold,position)
end
for j,position in ipairs(radiusold) do
table.insert(radius,position+1)
table.insert(radius,position-1)
table.insert(radius,position+map.width)
table.insert(radius,position-map.width)
if position % 2 == 0 then
table.insert(radius,position+map.width+1)
table.insert(radius,position+map.width-1)
else
table.insert(radius,position-map.width+1)
table.insert(radius,position-map.width-1)
end
end
end
return radius
end
However it takes horribly long to ecxecute and produces a lot of duplicate Entrys.
I will work on this myself, however I'd really appreciate any Suggestions.
Re: [Solved]For loop runnning longer than it's supposed to
Posted: Sat May 26, 2018 8:11 pm
by zorg
if it produces duplicates, then it doesn't do what you want it to; and again, to be honest i didn't 100% understood your intention, so if you want, you could Explain it Like I'm 5 (ELI5), maybe that way, we can help more.
Re: [Solved]For loop runnning longer than it's supposed to
Posted: Sun May 27, 2018 8:52 am
by RandomUserName
Ok, so my map is made up of Hexagonal Tiles that are offset in the y Direction (the flat sides are on the top and bottom).
The Tiles are identified by a single coordinate that increases by 1 for every Column and by 100 for every Row.
So basically:
Code: Select all
1 2 3 .. 99 100
101 102 103 .. 199 200
201 202 203 .. 299 300
The function is supposed to return a Table of all the coordinates within a certain Range around a Unit (Stick Figure) as shown in the Attachments.
love2d-movementRadius.PNG has a Radius of 5 while love2d-movementRadius2.PNG has a Radius of 1.
Re: [Solved]For loop runnning longer than it's supposed to
Posted: Sun May 27, 2018 3:17 pm
by bobbyjones
Code: Select all
function inRange( x, y, radius )
local points = {}
for i=x-radius,x+radius do
for j=y-radius,y+radius do
if ((i-x)^2 + (j-y)^2) < radius^2 then
table.insert(points, i)
table.insert(points, j)
end
end
end
return points
end
You will have to modify the function to work in your system but it will return items in the circle specified by the radius and position. an actual circle. I was writing and alternative method that would have been faster but it would have returned a diamond not a circle. I think there is a way to do it faster. just need to find the width of each row based off of the radius.
Edit: figured the other way out. The difference in performance is not much tbh. I thought one function was linear while the other is quadratic. I thought the time savings would be worth while but nope. both perform mostly the same. inRange has a couple wasteful checks and is slower but not by much.
Code: Select all
function horizRange(x,y,range,outTable)
for i=x-range, x+range do
table.insert(outTable,i)
table.insert(outTable,y)
end
end
function inRange2(x,y,radius)
local points = {}
for i=1,radius do
horizRange(x,y-i,findRadius(i,radius),points)
horizRange(x,y+i,findRadius(i,radius),points)
end
horizRange(x,y,radius,points)
return points
end
function findRadius(yoffset, radius)
return math.floor(math.sqrt(radius^2 - yoffset^2))
end
Re: [Solved]For loop runnning longer than it's supposed to
Posted: Mon May 28, 2018 9:11 am
by RandomUserName
Thank you for your Contribution.
I was trying to implement something similar, but your code does what I want it to.