0
Restart a count after reaching a certain value?
Started by mistamadd001, May 03 2014 03:32 AM
16 replies to this topic
#1
Posted 03 May 2014 - 03: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.
I have tried using while true do, but I seem to be using it in the wrong way.
guidance appreciated.
#2
Posted 03 May 2014 - 05:44 AM
globalCount = 0
while true do
globalCount = globalCount + 1
if globalCount == 8 then
globalCount = 0
print("Done")
end
sleep(0)
end
while true do
globalCount = globalCount + 1
if globalCount == 8 then
globalCount = 0
print("Done")
end
sleep(0)
end
#3
Posted 03 May 2014 - 05:56 AM
yea I actually figured out why my program wasn't working, I kept forgetting to use the == instead of =, thanks anyway
#4
Posted 03 May 2014 - 08:00 AM
You can use use:
for i=1,8 do -- what you want to do end
#5
Posted 03 May 2014 - 01:59 PM
OczkoSX, on 03 May 2014 - 08:00 AM, said:
You can use use:
for i=1,8 do -- what you want to do end
while true do for i = 1, 8 do end -- Make the loop yield here, there are different ways of doing that end
Edited by Engineer, 03 May 2014 - 01:59 PM.
#6
Posted 03 May 2014 - 10: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
#8
Posted 04 May 2014 - 04:13 AM
for i = 1, math.huge do
local counter = (i % 8) + 1;
--code
end
local counter = (i % 8) + 1;
--code
end
#10
Posted 04 May 2014 - 09: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:
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
#11
Posted 04 May 2014 - 05:19 PM
It would stop after like, > 2^1000 something iterations, which is not really a big deal.
#12
Posted 04 May 2014 - 06:35 PM
HometownPotato, on 04 May 2014 - 05:19 PM, said:
It would stop after like, > 2^1000 something iterations, which is not really a big deal.
just pulled the following of the internet.
Quote
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 by blipman17, 04 May 2014 - 06:36 PM.
#13
Posted 04 May 2014 - 06:39 PM
What? I didn't pull anything "off the internet."
I just know from testing that it was about that number.
I just know from testing that it was about that number.
#14
Posted 05 May 2014 - 02:07 AM
He didn't say you did - he was referring to himself.
#15
Posted 05 May 2014 - 03:57 AM
Yeah, I realized that right after I posted after re-reading what he posted. Just forgot to edit.
#16
Posted 05 May 2014 - 06:02 AM
@lyqyd can you lock this please and thankyou
#17
Posted 05 May 2014 - 02:25 PM
Threads in Ask a Pro generally don't get locked, they're simply allowed to age down off the page.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users