Jump to content


Photo

Timer


  • Please log in to reply
13 replies to this topic

#1 lord stewie

lord stewie

    GMC Member

  • New Member
  • 51 posts

Posted 18 November 2011 - 03:26 AM

Ok so I'm using GM lite, trying to make a game to where it will count of how many hour's(if they are on there that long),min's,and sec's the game counts and then on the very last level after they beat it, their time will be logged in a score caption box (per say). Anyone have any ideas of how this can be accomplished?

Edited by lord stewie, 18 November 2011 - 03:45 AM.

  • 0

#2 Noele

Noele

    GMC Mentor

  • GMC Member
  • 3383 posts
  • Version:GM:Studio

Posted 18 November 2011 - 11:53 AM

GM contains several time and date functions.

date_current_time() returns the current time (in the form of a timestamp) from the user's real time clock (system clock).
date_create_time(hour,minute,second) creates a timestamp from the time specified.
date_compare_time(date1,date2) Compares the two date-time values only taking the time part into account.
date_time_string(date) Returns a string indicating the given time in the default format for the system.

Using these you can create and display your timer.

At the start of the game:
global.start_time = date_current_time(); // create a start time

At the end of the game (Draw Event):
display_time = date_time_string(date_current_time()-global.start_time);
show_text(x,y,"You played for "+display_time);

Note: Timestamps are stored as a real number. The integral part is the number of days that have passed since 12/30/1899 and the fractional part of the date-time value is the fraction of a 24 hour day that has elapsed. For this reason displaying a timestamp as a real value will not work as expected, thus the use of the date_time_string() function to convert it to a string value which can be displayed.
  • 1

#3 lord stewie

lord stewie

    GMC Member

  • New Member
  • 51 posts

Posted 18 November 2011 - 10:00 PM

ok so when i try to use the
show_text(x,y,"You played for "+display_time)
it keeps saying "ERROR at line 2 pos 48:Wrong number of arguments to function or script". What do i put at line 2 pos 48
  • 0

#4 Noele

Noele

    GMC Mentor

  • GMC Member
  • 3383 posts
  • Version:GM:Studio

Posted 18 November 2011 - 10:13 PM

So sorry it should have been draw_text instead of show_text.
(I can't think where show_text came from)

My apologies
  • 0

#5 troido

troido

    GMC Member

  • GMC Member
  • 160 posts
  • Version:GM8

Posted 18 November 2011 - 10:14 PM

you should use "draw_text" instead of "show_text"
  • 0

#6 lord stewie

lord stewie

    GMC Member

  • New Member
  • 51 posts

Posted 20 November 2011 - 04:41 PM

ok so now when i try running the program here is what i get
___________________________________________
FATAL ERROR in
action number 1
of Draw Event
for object adsfa:

COMPILATION ERROR in code action
Error in code at line 2:
   show_text(x,y,"You played for: " +display_time)
                                                 ^
at position 48: Wrong number of arguments to function or script.
What do i do?
  • 0

#7 Noele

Noele

    GMC Mentor

  • GMC Member
  • 3383 posts
  • Version:GM:Studio

Posted 20 November 2011 - 04:55 PM

Change the draw event:

Draw Event:
display_time = date_time_string(date_current_time()-global.start_time);
draw_set_color(c_black);
draw_text(x,y,"You played for "+display_time);
You may also want to set the font you want to use before drawing the text:
draw_set_font(your_font_resource_name);
  • 0

#8 lord stewie

lord stewie

    GMC Member

  • New Member
  • 51 posts

Posted 20 November 2011 - 05:31 PM

ok so it works but its showing what the current time is not how long they played for
  • 0

#9 Noele

Noele

    GMC Mentor

  • GMC Member
  • 3383 posts
  • Version:GM:Studio

Posted 20 November 2011 - 05:41 PM

Where have you put this line?
global.start_time = date_current_time(); // create a start time

It needs to be called once at the game start. You can use a Create event or The Game Start event.
  • 0

#10 lord stewie

lord stewie

    GMC Member

  • New Member
  • 51 posts

Posted 20 November 2011 - 06:18 PM

i still get the current time
  • 0

#11 Noele

Noele

    GMC Mentor

  • GMC Member
  • 3383 posts
  • Version:GM:Studio

Posted 20 November 2011 - 08:23 PM

Here is a demo of an elapsed timer I ran up for you:
Timer.zip Direct Download

It only has 1 room and one object which you can copy the code from.
  • 0

#12 lord stewie

lord stewie

    GMC Member

  • New Member
  • 51 posts

Posted 20 November 2011 - 10:05 PM

is there anyway to stop it from saying the AM?
  • 0

#13 Noele

Noele

    GMC Mentor

  • GMC Member
  • 3383 posts
  • Version:GM:Studio

Posted 21 November 2011 - 09:16 AM

The current time display uses whatever local clock settings the PC is set to use. Thus to remove the AM/PM you can set your system clock to 24hr and the AM/PM will be gone. Alternatively you can decode the time yourself to get the hours, minutes and seconds but you will have to handle the formatting yourself.

You can replace the Draw Event code with the following to show the system format as well as your own:
draw_set_font(fnt_normal); // select display font to use
draw_set_color(c_black); // set color to black
// Calculate the elapsed time
time_used = date_current_time()-global.start_time;
elapsed = date_time_string(time_used);
draw_set_halign(fa_center); // center the text
// display the time using the 
draw_text(floor(room_width/2),200,"Time elapsed: "+elapsed);
//
// Create our own time format
hours = date_get_hour(time_used);
minutes = date_get_minute(time_used);
seconds = date_get_second(time_used);
hrs = string(hours);
if(hours < 10) {hrs = "0"+hrs;}
mins = string(minutes);
if(minutes < 10) {mins = "0"+mins;}
secs = string(seconds);
if(seconds < 10) {secs = "0"+secs;}
// Draw our own time format as hh:mm:ss
draw_text(floor(room_width/2),400,"Own Time Format: "+hrs+":"+mins+":"+secs);

Formatting your own time display will work uniformly regardless of the users current real-time clock settings but it will still adhere to local time for that system - I.E. any daylight saving times for that system will still be taken into account.
  • 0

#14 lord stewie

lord stewie

    GMC Member

  • New Member
  • 51 posts

Posted 21 November 2011 - 11:03 PM

ok thanks man
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users