This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KaBooMa's profile picture

Running a timer with my code

Started by KaBooMa, 06 August 2012 - 03:57 AM
KaBooMa #1
Posted 06 August 2012 - 05:57 AM
Hey so I am porting tetris over the CC and currently I have a problem that I just can't seem to fix. I got a bunch of code running and when you hit play, it is supposed to start running a timer every second or whatever I need it to run at. I need it so this timer every second, runs a function to update the screen so the blocks that need to fall, will fall, and you can rotate your block and everything. Is there a way to run a timer with all my code? I need it so you can play the game, rotate and everything, but every second the block physics move the block down basically. Any ideas?
kotorone1 #2
Posted 06 August 2012 - 05:59 AM
can we see the code? we can't help unless we can see what's going on
ChiknNuggets #3
Posted 06 August 2012 - 07:37 AM
at the start of the game do

starttime = os.clock()
and then when you want to print the time thats been elapsed or use it for what ever do something like this
elapsedtime = starttime - os.clock()
KaBooMa #4
Posted 06 August 2012 - 12:00 PM
Basically all I need is a timer that runs behind the game that every second runs a function. I want you to play and stuff, but every second the timer will run the UpdateBlocks() function
Pharap #5
Posted 06 August 2012 - 12:28 PM
How about, instead of doing a time caclulation (as there could be issues if the date rolls over), why not set the timeout to the current time, plus 1 second, then get the keydown event, then run the update logic and on timeout, print to screen then let it loop back to start.

Something like: (pseudocode)

while true do
os.starttimer(1) --fiddle with this time until it does something better than one frame a second whilst still making the game playable)
getkeydown()  --function for taking the keydown (this is where selections and  happen, like exiting the program)
updatelogic() --update the data (
timeoutevent() -- draw the data
end  -- loop to start

I've been learning XNA stuff lately, so I sort of know the basic structure of your average computer game, hopefully that will work for you.
I'm assuming you can do events since you are attempting to port an event drive game *insert decent happy emoticon here*
MysticT #6
Posted 06 August 2012 - 07:43 PM
Try with something like this:

local updateTimer = os.startTimer(1)

while true do
  local evt, arg = os.pullEvent()
  if evt == "timer" then
    if arg == updateTimer then
	  UpdateBlocks()
	  updateTimer = os.startTimer(1)
    end
  elseif evt == "key" then
  -- etc.
  end
end
Just make sure to start the timer when the game starts.
KaBooMa #7
Posted 06 August 2012 - 10:00 PM
Ok so I need to do the while true do above all my code or under it. I know I tried before under my code and it seems to freeze me up into a inf. loop
MysticT #8
Posted 06 August 2012 - 10:05 PM
Well, it depends on how your code works. If you use a loop that handles events, you need to combine both loops into one.
KaBooMa #9
Posted 06 August 2012 - 10:13 PM
I actually figured it out. This is a bit different from the datastream, timer.create, and such I am used to in lua from gmod :P/>/> I actually have it working now. Time to get the actually gameplay in now ;)/>/> Is there a tetris port yet? I actually haven't seen one. Would be cool to see some others work and stuff too.