Timer Tutorial

Abstract

This shows how to make a countdown timer. It can be used for things such as cooldowns.

Prerequisites

Basic familiarity with oop in Python.

Code

class CountdownTimer():
  """
  created timers.  you cant stop or change them, make a new one instead
  """
  def __init__(self, stop):
    timer.duration = stop
    timer.start = bge.logic.getFrameTime()
    timer.stop = bge.logic.getFrameTime() + stop
 
  @property
  def current(self):  return bge.logic.getFrameTime()
 
  @property
  def elapse(self):  return timer.current - timer.start
 
  @property
  def elapsePercent(self):  return timer.elapse / timer.duration
 
  @property
  def remain(self):  return timer.stop - timer.current 
 
  @property
  def remainPercent(self):  return timer.remain / timer.duration

Usage

from somewhere import CountdownTimer as Timer
 
# Make the timer that stops in ten seconds.
timer = Timer(10)
 
# Number of seconds elapsed.
timer.elapse
 
# Percent of seconds elapsed.
timer.elapsePercent
 
# Number of seconds remaining.
timer.remain
 
# Percent of seconds remaining.
timer.remainPercent

Questions

Edit this page to ask or answer questions.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License