# For loop "bug"

So I have experienced a minor problem, easy fix. But I just wanted to get it out before I forget it.

I have been experiencing problems with loops lately, espesually for loops. The most recent one I have located was that the draw() function runs too often(?!).
This occurs when the

``````for index,element in pairs(myTable) do
``````

loop has an index above 2^24 (16’777’216).
This usually is not a problem, but I stumbled upon it and thought I would share it.

Ex:

``````myTable = {
[2^24-1] = "foo",
[2^24] = "bar",
[2^24+1] = "ChickenStewBoiledInRawPlastic",
}
``````

Poor example but I hope you get the gist.
I do not know how to report this so I report it as a bug, I am mostly telling you this so you can watch out for it too.

Come on, you have to tell us why you are using numbers this big :-?

LOL @ Ignatz!

I think you’ve hit the numerical range of how Lua stores integer numbers (2^24) which gives a +/- 16,777,216 range.

I’m sure somewhere on this forum someone mentioned that Codea doesn’t support ‘double’ precision (i.e. 64 bit - 2^53) ?

Its not really an error, just the way this data type has defined limits.

Haha well I have tried making my first “game” wich includes making circles at the touch of the user. Very simple and for the people I have viewed this for they could sit with it for a good 15min.
Anyways, I got a variable called TotalRings, wich is as its name prefers: it holds the number of rings ever created since the app started. (When a ring is created: `TotalRings = TotalRings + 1`)

I also used this for my index of each ring.

``````function touched(touch)
TotalRings = TotalRings + 1
table.insert(circles,TotalRings,{"ring, wich x,y pos and more"})
end
``````

(The touched() function is a bit more complex in the original code)

This makes so when I draw it using a “for each pair” loop it draws them, as requested.
But if TotalRings is above 2^24 then each index will also be, so the for loop apperently fucks up, in one way or another.
This makes the rings instead of expanding like they should they just show up as dots, dosent have time to expand. Or maybe they expand faster then I can see. It’s really hard to test.

Now using the console I could easily change the TotalRings as I wanted (`TotalRings=anynumber`).
And I found out that this acting started when TotalRings are above 16’777’216 (found that out with a lot of testing ).

The big questions is…(drumroll)… if this is based on touch events, how do you ever get above 16 million rings? :-/

@andymac3d Thanks! Well I have a hard time defining true errors, I mostly call them bugs but I don’t even know if they’re worth that title.

Yes I am using thhe built in interpeter right under the parameters, so for example

``````TotalRings = 16777000
``````

And then tap a little and so the “bug” begins.

It must be something with either the start number of total rings or how you increment it. If as you say the rings are created on touch, and let’s say for each touch you create 100 rings.

So some simple maths would suggest I can create 100 rings per frame at 60 frames per second for just over 44 minutes before this would blow up. I’m sure your app could be refactored to avoid this even, assuming you don’t leave all rings ever on screen, but I’m sure your increment is happening a lot more than just on touch.

@spacemonkey This will clear some things up for ya!
http://youtu.be/XKdoV3md5UU
The video is currenly being controlled, but when it’s finished you can see how the mechanics works.

The game creates a ring at the touch of the user with a color generated with the touch id as seed. The ring got a certain variable of life left until it evaporates (disappears). As this and the opacity decreases the size expands. When the “life” left is below zero, it’s destroyed.

I also added a pause function, just for fun. I will soon add stuff like color selection or to choose the speed of wich the rings destroy in.

I got loads of plant, but this is a bug report, so I’ll leave this subject a bit.

@akaJag so just to check, you’ve flagged a bug, do you still have an issue with this in your app? If so I could suggest some strategies based on your video, if not… keep on coding

@spacemonkey I have not yet fixed it, so if someone sits there and makes circles for a little more then 2 days then the game will start to act differently, therefore the “bug”. And that is with 1 finger.

Minor problem, but I would love some help! :)>-

In my particle fireworks thing here:
http://twolivesleft.com/Codea/Talk/discussion/2516/particle-effect-in-a-shader#Item_7 I have a table, and I don’t actually remove the items, so at the start of allocating a new item, rather than adding it to the end, I scan for an old particle and reuse that entry.

Alternately, if you do destroy the items out of the table, which I assume you do otherwise your performance would die before the integer does, just reset the integer at 1 million back to 1, it’ll start filling the table from entry 1 again. And by the time it reaches a million I’m sure your early circles will already be gone.

Oh no! Then I cant have like a million fingers at the screen at once