[Solved]For loop runnning longer than it's supposed to

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
RandomUserName
Prole
Posts: 7
Joined: Mon May 14, 2018 12:35 pm

[Solved]For loop runnning longer than it's supposed to

Post 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?
Last edited by RandomUserName on Sat May 26, 2018 7:53 pm, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: For loop runnning longer than it's supposed to

Post 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.
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.
RandomUserName
Prole
Posts: 7
Joined: Mon May 14, 2018 12:35 pm

Re: For loop runnning longer than it's supposed to

Post 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
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: For loop runnning longer than it's supposed to

Post 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.
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.
RandomUserName
Prole
Posts: 7
Joined: Mon May 14, 2018 12:35 pm

Re: For loop runnning longer than it's supposed to

Post 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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: [Solved]For loop runnning longer than it's supposed to

Post 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.
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.
RandomUserName
Prole
Posts: 7
Joined: Mon May 14, 2018 12:35 pm

Re: [Solved]For loop runnning longer than it's supposed to

Post 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.
Attachments
love2d-movementRadius2.PNG
love2d-movementRadius2.PNG (84.13 KiB) Viewed 5088 times
love2d-movementRadius.PNG
love2d-movementRadius.PNG (96.09 KiB) Viewed 5088 times
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: [Solved]For loop runnning longer than it's supposed to

Post 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
RandomUserName
Prole
Posts: 7
Joined: Mon May 14, 2018 12:35 pm

Re: [Solved]For loop runnning longer than it's supposed to

Post by RandomUserName »

Thank you for your Contribution.
I was trying to implement something similar, but your code does what I want it to.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 1 guest