Jump to content




Restart a count after reaching a certain value?


16 replies to this topic

#1 mistamadd001

  • Members
  • 76 posts

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.

#2 HometownPotato

  • Members
  • 62 posts

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

#3 mistamadd001

  • Members
  • 76 posts

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 OczkoSX

  • Members
  • 29 posts

Posted 03 May 2014 - 08:00 AM

You can use use:
for i=1,8 do
  -- what you want to do
end


#5 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 03 May 2014 - 01:59 PM

View PostOczkoSX, on 03 May 2014 - 08:00 AM, said:

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 by Engineer, 03 May 2014 - 01:59 PM.


#6 Sir_Mr_Bman

  • Members
  • 62 posts

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


#7 OczkoSX

  • Members
  • 29 posts

Posted 03 May 2014 - 10:31 PM

View PostSir_Mr_Bman, on 03 May 2014 - 10:18 PM, said:

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

#8 HometownPotato

  • Members
  • 62 posts

Posted 04 May 2014 - 04:13 AM

for i = 1, math.huge do
local counter = (i % 8) + 1;
--code
end

#9 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 04 May 2014 - 09:09 AM

View PostHometownPotato, on 04 May 2014 - 04:13 AM, said:

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

#10 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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:

local counter = bit.band(i,7) + 1


#11 HometownPotato

  • Members
  • 62 posts

Posted 04 May 2014 - 05:19 PM

It would stop after like, > 2^1000 something iterations, which is not really a big deal.

#12 blipman17

  • Members
  • 92 posts

Posted 04 May 2014 - 06:35 PM

View PostHometownPotato, 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 HometownPotato

  • Members
  • 62 posts

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.

#14 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 05 May 2014 - 02:07 AM

He didn't say you did - he was referring to himself.

#15 HometownPotato

  • Members
  • 62 posts

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 mistamadd001

  • Members
  • 76 posts

Posted 05 May 2014 - 06:02 AM

@lyqyd can you lock this please and thankyou :D

#17 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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