How to make DYI digital clock (NOT DateTime variable)

Started by Thanyx, Sat 01/10/2022 00:26:21

Previous topic - Next topic

Thanyx

Hi guys,

I am trying to create digital clock in the game which is going to be stopped, and will pass time only if certain actions take place. That means the usual "DateTime" global variable is not an option.


Example:
- Time: 10:00
- You click on Hotspot
- Time: 10:05

The premise is simple, but I don't know how to make a function that will recognize the time logic. For example if I have 11:59, and you click on a hotspot, I want to add +1 second, how do I make the variable go to 12:00, instead of 11:60.

Because I am able to just add +1 to the int value.


Crimson Wizard

#1
Store the "total minutes" value in a global variable for easier addition and subtraction, but when displaying on a clock convert from "total minutes" to hours and minutes.

Code: ags
int hours = total_minutes / 60;
int minutes = total_minutes - (hours * 60);

EDIT: Or, if you need to have a second-precision, then store "total_seconds", and convert from them:
Code: ags
int hours = total_seconds / 3600;
int minutes = (total_seconds - (hours * 3600)) / 60;
int seconds = total_seconds - (hours * 3600) - (minutes * 60);

PS. The above idea is valid, but I could make some math mistake, so need to double check that above works right.


UPD: In overall, in the software design, when you have a similar situation, the idea is to separate the data and the visual representation. The data is chosen in such way that makes it easier to store and change. The visual representation may be anything; or there may even be several different representations, all converted from same data.

Snarky

Yup, what CW says (though he could have used % instead of all the multiplications and subtractions). I'll just add that if you need the hours to wrap around as well, you'll have to add a further step. Exactly how to do that depends on the clock you use:

-On a 24-hour clock it's easy. You go from 23:59 to 00:00 simply by doing % 24:

Code: ags
int days = total_minutes / 1440; // 1440 = 60*24
int hours = (total_minutes / 60) % 24;

(Or adjust as necessary if you measure the time in seconds. The days variable isn't used for the clock, of course, but you can use it for a calendar or internally in your game logic.)

-On a 12-hour clock with AM/PM (where you go from 11:59 AM to 12:00 PM), you need to convert the 24-hour time by doing % 12, figuring out if it's AM or PM, and displaying 12 if the number is 0. I would probably do this as a separate step based on a 24-hour hours variable:

Code: ags
int amOrPm = hours / 12; // 0 if AM, 1 if PM
int displayHours = hours % 12;
if(displayHours == 0)
  displayHours = 12;

Thanyx

Thanks guys! Appreciate your help!

I have a new problem though... When I want to change the global variable during a dialogue, nothing happens. Only after I leave the entire dialogue. So basically, I need to change the variable every time you click on a dialogue option to -10. What happens, is that the variable stays the same, and only after I leave the dialogue it calculates all the clicks I did during the dialogue, so it goes like -50.

In other words, it calculates my Time = Time -10; , but it doesn't print the values out when inside a dialogue.

My code is simple like this.

Code: ags
@1
  Time = Time - 10;
You: So what do we have here?


Thanyx

NEVERMIND guys, I solved it like a second after I made the post.

I original had the Label1.Text = String.Format("%02d", Time); in repeatedly execute, instead of repeatedly execute ALWAYS :)


Khris

Since the label text needs to be updated only after the time has changed, you can write a function:

Code: ags
function MoveClock(int by) {
  Time += by;
  Label1.Text = String.Format("%02d", Time);
}

Now use
Code: ags
  MoveClock(-10);
wherever you want.

You can also put the display logic inside the function obviously.

Crimson Wizard

Quote from: Thanyx on Sat 01/10/2022 11:54:52NEVERMIND guys, I solved it like a second after I made the post.

I original had the Label1.Text = String.Format("%02d", Time); in repeatedly execute, instead of repeatedly execute ALWAYS :)

Note also that you may let "repeatedly execute" run during dialogs too, if you enable this in General Settings.

SMF spam blocked by CleanTalk