3

I've been trying hard to simply display the current time and couldn't find a solution anywhere...so sorry for the misplaced post.

All I want is to display the time on the user's pc or phone.

What I can do is get the time to show up in a box message but not have it just displayed and updated every second.

This is my current code

//store the current time
t=date_current_datetime();

//get the hour portion of that time
h=date_get_hour(t);

//get the minute portion of the time.
m=date_get_minute(t);

//get the second potion of the time
s=date_get_second(t);

//show the time
txt="The current time is:"+string(h)+":"+string(m)+":"+string(s);
show_message(txt); 

Hope this gamemaker code makes sense, thanks for any help.

Panga97
  • 81
  • 1
  • 8
  • What you want? (What is your goals?) Do you know that `show_message` is only for debugging? – Dmi7ry Jul 12 '15 at 17:42
  • I want the current time to show up on the screen when you run the game and have it update every second so that it displays the time. I did not know that show_message was only for debugging – Panga97 Jul 12 '15 at 18:01

1 Answers1

6

Ok, add to Create event:

txt = "";
alarm[0] = 1;

Move your code to Alarm 0 event:

//store the current time
t = date_current_datetime();

//get the hour portion of that time
h = date_get_hour(t);

//get the minute portion of the time.
m = date_get_minute(t);

//get the second potion of the time
s = date_get_second(t);

//show the time
txt = "The current time is: " + string(h) + ":" + string(m) + ":" + string(s);

alarm[0] = room_speed;

And last - Draw event:

draw_text(10, 10, txt);

Of course also you can use Draw GUI event or view coords, like

draw_text(view_xview[0] + 10, view_yview[0] + 10, txt);
Dmi7ry
  • 1,777
  • 1
  • 13
  • 25
  • 2
    Wow worked on the first go, and is exactly what I need! Thx a lot – Panga97 Jul 12 '15 at 21:33
  • Do you know what I would have to do to to make for example an object visible at a vertain time? I keep getting errors using h, m and s as variables..... – Panga97 Jul 14 '15 at 20:41
  • 1
    @Panga97 Many ways. For example, you can add to that `Alarm 0` after `txt = ....` something like: `if h = 13 and m = 00 { obj_lunch.visible = true; } else { obj_lunch.visible = false; }` where 'obj_lunch' is your object – Dmi7ry Jul 15 '15 at 03:48
  • Many people who learn GML forget to use the `var` keywords. It creates temporary variables which stop existing at the end of the code. You should add the line `var t, h, m, s;` at the beginning of the Alarm 0 script. – Domino Jul 15 '15 at 17:09
  • mm I've got this code //store the current time var t = date_current_datetime(); //get the hour portion of that time var h = date_get_hour(t); //get the minute portion of the time. var m = date_get_minute(t); //get the second potion of the time var s = date_get_second(t); //show the time txt = "The current time is: " + string(h) + ":" + string(m) + ":" + string(s); alarm[0] = room_speed; ////\\\\ if (m = 40) { obj_Logo.visible = true; } – Panga97 Jul 16 '15 at 18:04
  • I get: " ___________________________________________ ############################################################################################ FATAL ERROR in action number 1 of Alarm Event for alarm 0 for object Current_Time: Push :: Execution Error - Variable Get 100000.obj_Logo(100005, -2147483648) at gml_Object_Current_Time_ObjAlarm0_1 (line 22) - obj_Logo.visible = true; ############################################################################################ " – Panga97 Jul 16 '15 at 22:05
  • @Panga97 see [example](https://dl.dropboxusercontent.com/u/78963719/stackoverflow/gml/time.gmz) - here second object will showed/hided each 10 seconds. – Dmi7ry Jul 17 '15 at 09:55
  • How do i use this example? I get a gamemaker file downloaded and don't know what to do with it :( I tried using it as an object but nope – Panga97 Jul 17 '15 at 16:05
  • 1
    @Panga97 what version of GM you use? – Dmi7ry Jul 17 '15 at 16:14
  • 1
    @Panga97 File -> Import project – Dmi7ry Jul 17 '15 at 17:51