Continue in repeat until loop causing crash

I need to get a neighboring position of a 2d array that has value “o”
To do so I use a repeat until loop so that if it choses an array position with value “l”, it will repeat, I also need it to continue if the chosen position is off the array, and to cancel if it has run 100 times so that it wont crash.

However, when the X position of neighboring position is not on the array, (5 or 0) then it makes an error as it tries to put the Z position on top of the nil. BUT this should have been taken care of when the code found that the position was off the array.

It seems as if, once continue (maybe break and return as well) is called, it runs the repeat condition. I don’t want this to happen as then the code makes an error.

Also when the Z position is off the array, it is fine as the until keyword sees that nil~=“0” and works as normal, if not intended.

Is there any way to repeat a repeat until loop without checking the repeat condition?

--Necessary excerpt from my code with IslandZoom1Layer
local IslandZoom1Layer={
	{"o","o","o","l"},
	{"o","o","o","o"},
	{"o","l","o","o"},
	{"o","o","o","o"}
}

for i, column in ipairs(IslandZoom1Layer) do
	for o,part in ipairs(column) do
		if part=="l" then
			local Chance=math.random()
			if Chance>0.25 then
				local randomneighborX
				local randomneighborZ
				local Count=0

				repeat
					Count+=1
					randomneighborX=((math.random(0,1)*2)-1)+i
					randomneighborZ=((math.random(0,1)*2)-1)+o
					if (randomneighborX>#IslandZoom1Layer or randomneighborX<1) or (randomneighborZ>#IslandZoom1Layer or randomneighborZ<1) then
						print("a")
						print(randomneighborX,randomneighborZ)
						continue
					else
						print("b")
					end
					if Count==100 then break end
				until IslandZoom1Layer[randomneighborX][randomneighborZ]=="o"
				print("c")
				IslandZoom1Layer[randomneighborX][randomneighborZ]="l"
				local Chance=math.random()
				if Chance>0.75 then --25% chance
					part="o"
				end
			end
		end
	end
end

print(IslandZoom1Layer)

Sorry, I don’t get your explanation. Are you trying to find all the cells around an o? Are you trying to find an l next to any "o"s?

you have to put wait on your repeat like this

repeat wait()

until

I am trying to find a random cell around “l” that has value “o”

I changed it to this, using recursion which fixed the error, but now sometimes the cells off the map get through leading to an error when it tries to switch the value of the cell.

local IslandZoom1Layer={
	{"o","o","o","l"},
	{"o","o","o","o"},
	{"o","l","o","o"},
	{"o","o","o","o"}
}
for i, column in ipairs(IslandZoom1Layer) do
	for o,part in ipairs(column) do
		if part=="l" then
			local Chance=math.random()
			if Chance>0.5 then
				local randomneighborX
				local randomneighborZ
				local count=0
				
				local function choserandomneighbor()
					count+=1
					if count==100 then return end
					randomneighborX=((math.random(0,1)*2)-1)+i
					randomneighborZ=((math.random(0,1)*2)-1)+o
					if (randomneighborX>#IslandZoom1Layer or randomneighborX<1) or (randomneighborZ>#IslandZoom1Layer or randomneighborZ<1) then
						choserandomneighbor()
					elseif IslandZoom1Layer[randomneighborX][randomneighborZ]~="o" then
						choserandomneighbor()
					end
				end
				choserandomneighbor()
				IslandZoom1Layer[randomneighborX][randomneighborZ]="l"
				local Chance=math.random()
				if Chance>0.5 then
					IslandZoom1Layer[i][o]="o"
				end
			end
		end
	end
end

I found the issue, it was colliding with the count=100 mark, removing it lead it to loop endlessly (as expected if the cell is surrounded by land and or edge). Adding a count~=100 mark at the end fixed it. Though I am still annoyed that the repeat until loop kept checking the repeat condition when I knew it would cause it to break.
I also put it in a module function

module.AddIsland=function(IslandLayer)
	for i, column in ipairs(IslandLayer) do
		for o,part in ipairs(column) do
			if part=="l" then
				local Chance=math.random()
				if Chance>0.5 then
					local randomneighborX
					local randomneighborZ
					local count=0

					local function choserandomneighbor()
						count+=1
						if count==100 then return end
						randomneighborX=((math.random(0,1)*2)-1)+i
						randomneighborZ=((math.random(0,1)*2)-1)+o
						if randomneighborX>#IslandLayer or randomneighborX<1 or randomneighborZ>#IslandLayer or randomneighborZ<1 then
							choserandomneighbor()
						elseif IslandLayer[randomneighborX][randomneighborZ]~="o" then
							choserandomneighbor()
						end
					end
					choserandomneighbor()
					if count~=100 then
						IslandLayer[randomneighborX][randomneighborZ]="l"
						local Chance=math.random()
						if Chance>0.5 then
							IslandLayer[i][o]="o"
						end
					end
				end
			end
		end
	end
	return IslandLayer
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.