Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(1 edit)

Bug: Nested "for x times loops" not working

Basically, next nested for x times loops don't work as you'd expect in decent for-loop coding, as if the increment variable is shared between the two.

Likely culprit: When the exterior loop loops once, the 'loop count' is set to whatever loop count is the first one, but said value is overwritten by for x times loop(s) within said for loop, either leading to a bodged forever loop or a loop 1 times loop as the exterior loop.

Java example:

//Java "loop forever" for loop example
for (int i = 0; i < 10; i++) {
  for (int i = 0; i < 6; i++) {
    //The variable i is overwritten, reaches 6 and then the main loop is run again, and again, etc.
    do(x);
  }
}
//Java "loop for 1 times" for loop example
for (int i = 0; i < 10; i++) {
  for (int i = 0; i < 12; i++) {
    //The variable I is overwritten, reaches 12 and then the main loop is skipped
    do(x);
  }
}

Hope the description and possible reason helps; I don't know very much C, but Java is a close-enough language for it to be fairly easily translatable into C.

Great analysis! Yep, you're exactly right, the inner loop is overwriting the outer loop's counter. This is a known bug and will be fixed in the next release. Thanks

(3 edits)

i is overwritten in this Java construct? Interesting. I'm only (a little) familiar with Lua and Python and both would treat the two i variables as distinct local variables.

Python:

text = ""
for i in range(3):
    text += "\n{}: ".format(i)
    for i in range(2):
        text += "{} ".format(i)
else:
    print(text)

outputs:

0: 0 1
1: 0 1
2: 0 1

Lua:

text = ""
for i = 0,2 do
    text = text.."\n"..tostring(i)..": "
    for i = 0,1 do
        text = text..tostring(i).." "
    end
end
print(text)

outputs:

0: 0 1
1: 0 1
2: 0 1

Cool a fellow Lua coder!

But you don't necessarily have to use tostring() here, the following should work as well.

text = ""
for i = 0,2 do
    text = text.."\n"..i..": "
    for i = 0,1 do
        text = text..i.." "
    end
end
print(text)

Lua always tries to convert the i here into a string when possible. I think nil wouldn't get converted, and obviously lists can't get converted.

The same should be possible when adding a string that only contains numbers to an integer, the string gets converted to a number automatically.

local ans = 12+"012"
print(ans)

The 12+"012" returns 24 as a result, and so this prints in the next line.
The "012" get's converted to 12 as if you would do tonumber("012").
This usually saves some time when coding for me, so just a tip (I'm way to happy to see other people using lua XD)

(+1)

true - conversion works without "tostring()"

Nevertheless, the Lua guys recommend using it. That way, the interpreter doesn't have the extra task of finding out if a conversion is required or not. You state that it is.

The string.format() command would have been another option. Actually the nicest way to combine text and data into one string.


to Denki:
My apologies for abusing you forum for this off-topic talk :-)