Jump to content




Running a timer with my code


8 replies to this topic

#1 KaBooMa

  • New Members
  • 29 posts

Posted 06 August 2012 - 04: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?

#2 kotorone1

  • New Members
  • 20 posts

Posted 06 August 2012 - 04:59 AM

can we see the code? we can't help unless we can see what's going on

#3 ChiknNuggets

  • Members
  • 130 posts

Posted 06 August 2012 - 06: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()


#4 KaBooMa

  • New Members
  • 29 posts

Posted 06 August 2012 - 11:00 AM

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

#5 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 06 August 2012 - 11:28 AM

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*

#6 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 06 August 2012 - 06: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.

#7 KaBooMa

  • New Members
  • 29 posts

Posted 06 August 2012 - 09: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

#8 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 06 August 2012 - 09: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.

#9 KaBooMa

  • New Members
  • 29 posts

Posted 06 August 2012 - 09: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users