Jump to content




[HELP] Coroutine Timer



  • You cannot reply to this topic
5 replies to this topic

#1 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 17 March 2013 - 12:39 AM

Well as the title says I need help with creating a coroutine timer since I haven't quite figured out how I should do one..
I need it for returning how long time it has passed after I've completed this game.
Spoiler

Spoiler


#2 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 17 March 2013 - 07:31 AM

If you just subtract the ending os.clock() with the os.clock() when the game started, you should get the sufficient time without having to create any coroutines or parallels.

local startTime = os.clock()

doGameStuff()

local endTime = os.clock()

local totalTime = endTime - startTime
print(totalTime)

If you want to find the current game running time, keep startTime recorded, and just subtract the current os.clock() from the startTime. A small example demonstrating this concept:

local startTime = os.clock()
local endTime

local updateTimer = os.startTimer(0.05)
while true do
  local ev, p1 = os.pullEvent()
  if ev == 'timer' and p1 == updateTimer then
    term.clear()
    term.setCursorPos(1,1)
    print('Current Time: ' .. (os.clock() - startTime))
    updateTimer = os.startTimer(0.05)
  elseif ev == 'key' then
    endTime = os.clock()
    break
  end
end

term.clear()
term.setCursorPos(1,1)
print('Your time is ' .. (endTime - startTime))


#3 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 17 March 2013 - 10:16 AM

View PostKingdaro, on 17 March 2013 - 07:31 AM, said:

If you just subtract the ending os.clock() with the os.clock() when the game started, you should get the sufficient time without having to create any coroutines or parallels.

local startTime = os.clock()

doGameStuff()

local endTime = os.clock()

local totalTime = endTime - startTime
print(totalTime)

If you want to find the current game running time, keep startTime recorded, and just subtract the current os.clock() from the startTime. A small example demonstrating this concept:

local startTime = os.clock()
local endTime

local updateTimer = os.startTimer(0.05)
while true do
  local ev, p1 = os.pullEvent()
  if ev == 'timer' and p1 == updateTimer then
	term.clear()
	term.setCursorPos(1,1)
	print('Current Time: ' .. (os.clock() - startTime))
	updateTimer = os.startTimer(0.05)
  elseif ev == 'key' then
	endTime = os.clock()
	break
  end
end

term.clear()
term.setCursorPos(1,1)
print('Your time is ' .. (endTime - startTime))
Thx so much!
I thought no one would respond.
I used the top one of those two..
I did'nt think it was that simple with that one, About the other one.. Can
that one run while it is using read() or what?

#4 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 17 March 2013 - 12:06 PM

And now I'm wondering how I should make it print words per min in the end
I think i need to do something like this...
wMin = totalTime / <something>


#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 17 March 2013 - 12:42 PM

Well, have a variable increment everything you type the correct word and then
wordsPerMin = correctWords / 60

I see this is the typer program you made a video about :P Nice

#6 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 18 March 2013 - 12:49 AM

Yep it is.. Why don't you test it and see how fast you are at typing..
The most updated link is always here.
Will be much more fun when multiplayer is done.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users