Clock.js (system script)

From Spheriki

Jump to: navigation, search

Clock.js is a system script written by WIP that enables you to create a game clock object. It can be used to keep track of the elapsed playtime of a savegame.


Contents

Usage

RequireScript("clock.js");


Variables and functions of clock.js

  • Object constructor Clock()
    • Properties of objects of Clock type:
      • hours, minutes, seconds - define a time offset. All zero by default.
      • started - internal property.
    • Methods of objects of Clock type:
      • start() - starts the clock.
      • getTime(a) - returns the elapsed time since the clock has been started plus the time offset stored in the hours, minutes and seconds properties of the object. If 'a' is unspecified or false, the return value will be a string of this format: hours+":"+minutes+":"+seconds, for example "001:32:05". If 'a' is true, an array of strings will be returned. It'll have this format: [hours, minutes, seconds], for example: ["001", "32", "05"].


Example

This displays the elapsed time as soon as a key is pressed:

 RequireSystemScript("clock.js");
   
 Font = GetSystemFont();
 MyClock = new Clock();
   
 MyClock.start();
 Font.drawText(0, 0, "Clock has been started. Press any key...");
 FlipScreen();
 GetKey();
 
 Font.drawText(0, 0, "Time elapsed since clock started: " + MyClock.getTime());
 FlipScreen();
 GetKey();


Notes

  • You must start the clock before it will give the correct time.
  • If you are loading a game, you should load the time as a time offset into the clock object. That way, it can keep correct time with a loaded game.
  • There is a doc written by the author of the script in the docs/system_scripts/ directory of Sphere.


Known bugs

  • A bug that occurs when you change the time offset has been reported by tunginobi. See this forum thread for more information. A fixed version of the script might come with a new Sphere version.
Personal tools