76 posts
Posted 03 May 2014 - 04:32 AM
I would like to setup a count system that only counts up to 8 then on the next count returns to 1, if you could put a reset counter in as well that would be great so for every 8 counts the counter resets to 1 and adds 1 to say globalCount
I have tried using while true do, but I seem to be using it in the wrong way.
guidance appreciated.
62 posts
Posted 03 May 2014 - 06:44 AM
globalCount = 0
while true do
globalCount = globalCount + 1
if globalCount == 8 then
globalCount = 0
print("Done")
end
sleep(0)
end
76 posts
Posted 03 May 2014 - 06:56 AM
yea I actually figured out why my program wasn't working, I kept forgetting to use the == instead of =, thanks anyway
28 posts
Posted 03 May 2014 - 09:00 AM
You can use use:
for i=1,8 do
-- what you want to do
end
1522 posts
Location
The Netherlands
Posted 03 May 2014 - 02:59 PM
You can use use:
for i=1,8 do
-- what you want to do
end
But that runs only once, use this:
while true do
for i = 1, 8 do
end
-- Make the loop yield here, there are different ways of doing that
end
Edited on 03 May 2014 - 12:59 PM
63 posts
Posted 03 May 2014 - 11:18 PM
Unless I'm thinking about it wrong, couldn't you just…
for i = 1, 8 do
-- Your code here.
if i == 8 then
i = 1
end
end
28 posts
Posted 03 May 2014 - 11:31 PM
Unless I'm thinking about it wrong, couldn't you just…
for i = 1, 8 do
-- Your code here.
if i == 8 then
i = 1
end
end
No, because it will repeat 8 times, var's value is unnecessary
62 posts
Posted 04 May 2014 - 05:13 AM
for i = 1, math.huge do
local counter = (i % 8) + 1;
–code
end
1522 posts
Location
The Netherlands
Posted 04 May 2014 - 10:09 AM
for i = 1, math.huge do
local counter = (i % 8) + 1;
–code
end
That will stop at some point, use a combination of a for loop and a while loop, where the while loop is infinite
7083 posts
Location
Tasmania (AU)
Posted 04 May 2014 - 10:40 AM
If you're keeping track of how many total iterations have been performed, eventually the counter variable's going to overflow anyways. Though quite frankly I'm surprised "math.huge" wouldn't crash "for" loops from the get-go.
I suppose this topic's horse has been thoroughly smushed into the ground by now, but rather than taking the modulus of "i" (an operation that gets slower and slower as "i" increases), you could run a binary AND on it:
local counter = bit.band(i,7) + 1
62 posts
Posted 04 May 2014 - 06:19 PM
It would stop after like, > 2^1000 something iterations, which is not really a big deal.
92 posts
Posted 04 May 2014 - 07:35 PM
It would stop after like, > 2^1000 something iterations, which is not really a big deal.
just pulled the following of the internet.
as compiled by default, the Number is a double, on most compilers that's an IEEE 64-bit floating point. that means 10bit exponent, so the maximum number is roughly 2^1024, or 5.6e300 years. that's a long time.
Edit: they were talking about incrementing a number once everey second.
Edited on 04 May 2014 - 05:36 PM
62 posts
Posted 04 May 2014 - 07:39 PM
What? I didn't pull anything "off the internet."
I just know from testing that it was about that number.
7083 posts
Location
Tasmania (AU)
Posted 05 May 2014 - 03:07 AM
He didn't say you did - he was referring to himself.
62 posts
Posted 05 May 2014 - 04:57 AM
Yeah, I realized that right after I posted after re-reading what he posted. Just forgot to edit.
76 posts
Posted 05 May 2014 - 07:02 AM
@lyqyd can you lock this please and thankyou :D/>
8543 posts
Posted 05 May 2014 - 03:25 PM
Threads in Ask a Pro generally don't get locked, they're simply allowed to age down off the page.