Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to make a time system?

Hi everyone, I'm having a lot of problems trying to make a time system for my game. I want a sturdy time system because I want to make a business managment simulator kind of game, so I have to keep track of the day of the week, the day of the month, the month and the year, so this are my variables.

$hour
$minute
$weekday
$monthday
$month
$year

I need the game to do the basic maths while you are playing, like not going over 59 minutes or over 23 hours. I tried setting up a conditinal.

<<if $minute > 59>>
<<set $hour += 1>>
<<set $minute -= 60>>
<<else>>
<</if>>

Its the only thing that I can think of. For days of the week, I think it would be cool to have like a string of the seven days of the week and that when $hour reaches more than 23, the hour should go down to 0 again and the day of the week and the day of the month should change.

I dont really know if this or something like this has been answered here, but i searched for a bit and didn't found anything useful.

Thank you for your time and I will be happy to listen to every piece of advice that you could give me.

Comments

  • Instead of using multiple variables to track the different units of game time and having to use logic to correctly update those units you could use a single variable containing a Javascript Date object to do it.

    note: I will use indentation and extra line-breaks in the following examples to format them so they are more readable, that formatting can be safely removed.

    1. Initialise the variable in your StoryInit passage.

    The Date object's documentation explains the different ways you can do this, my example is midnight on the first day of the first month of the year 2000.
    <<set $now to new Date(2000, 0, 1, 0, 0, 0)>>
    

    2. Add Javascript code to help with the changing of the game time.

    The following Javascript code creates a setup.changeDate(date, interval, units) function which you will used to change the date. The interval values it supports are 'years', 'quarters', 'months', 'weeks', 'days', 'hours', 'minutes', and 'seconds'.

    warning: Always use the largest interval that meets your needs, do not try to add multiple days in a single call using a large number of 'hours', 'minutes', or 'seconds' because this can cause the date to become incorrect if you traverse over a leap-year delimiter.

    Add the following to your story's Story Javascript area.
    /* A method to change a date using intervals */
    if (! setup.changeDate) {
    	setup.changeDate = function(date, interval, units) {
    
    		var d = new Date(date); // Don't change original date.
    
    		switch(interval.toLowerCase()) {
    			case 'years':
    				d.setFullYear(d.getFullYear() + units);
    				break;
    			case 'quarters':
    				d.setMonth(d.getMonth() + 3 * units);
    				break;
    			case 'months':
    				d.setMonth(d.getMonth() + units);
    				break;
    			case 'weeks':
    				d.setDate(d.getDate() + 7 * units);
    				break;
    			case 'days':
    				d.setDate(d.getDate() + units);
    				break;
    			case 'hours':
    				d.setTime(d.getTime() + units * 3600000);
    				break;
    			case 'minutes':
    				d.setTime(d.getTime() + units * 60000);
    				break;
    			case 'seconds':
    				d.setTime(d.getTime() + units * 1000);
    				break;
    			default:
    				break;
    		}
    
    		return d;
    	};
    }
    

    3. Example of using the setup.changeDate to change game time.
    original date: <<print $now>>
    
    <<set $now to setup.changeDate($now, 'minutes', 15)>>
    Added 15 minutes of game time: <<print $now>>
    
    <<set $now to setup.changeDate($now, 'hours', 2)>>
    Added 2 hours of game time: <<print $now>>
    

    You can use the Date object's in-built methods to access the different components (year, month, date, etc...) of the $now variable, and to change the format of the outputed date String. You could use SugarCube's <<widget>> macro or it's Macro API to make custom macros to help you with the calls to the setup.changeDate function.
  • edited July 2016
    This code doesn't seem to work for me (even though I copied and pasted it). When I try to:
    Today is <<print $now>>
    

    It just displays:
    Today is [object].

    Can anybody help?
  • Charlie76 wrote: »
    Can anybody help?
    That code was designed for SugarCube 2, unfortunately SugarCube 1's <<print>> macro does not support the Date object.

    The following <<widget>> macro uses the toLocaleString function of the Javascript Date object to output a formatted String version of the $now variable.
    <<widget "show-now">><<print $now.toLocaleString()>><</widget>>
    
    ... and you use the new <<show-now>> widget like so:
    original date: <<show-now>>
    
    <<set $now to setup.changeDate($now, 'minutes', 15)>>
    Added 15 minutes of game time: <<show-now>>
    
    <<set $now to setup.changeDate($now, 'hours', 2)>>
    Added 2 hours of game time: <<show-now>>
    
  • greyelf wrote: »
    Instead of using multiple variables to track the different units of game time and having to use logic to correctly update those units you could use a single variable containing a Javascript Date object to do it.

    note: I will use indentation and extra line-breaks in the following examples to format them so they are more readable, that formatting can be safely removed.

    1. Initialise the variable in your StoryInit passage.

    The Date object's documentation explains the different ways you can do this, my example is midnight on the first day of the first month of the year 2000.
    <<set $now to new Date(2000, 0, 1, 0, 0, 0)>>
    

    2. Add Javascript code to help with the changing of the game time.

    The following Javascript code creates a setup.changeDate(date, interval, units) function which you will used to change the date. The interval values it supports are 'years', 'quarters', 'months', 'weeks', 'days', 'hours', 'minutes', and 'seconds'.

    warning: Always use the largest interval that meets your needs, do not try to add multiple days in a single call using a large number of 'hours', 'minutes', or 'seconds' because this can cause the date to become incorrect if you traverse over a leap-year delimiter.

    Add the following to your story's Story Javascript area.
    /* A method to change a date using intervals */
    if (! setup.changeDate) {
    	setup.changeDate = function(date, interval, units) {
    
    		var d = new Date(date); // Don't change original date.
    
    		switch(interval.toLowerCase()) {
    			case 'years':
    				d.setFullYear(d.getFullYear() + units);
    				break;
    			case 'quarters':
    				d.setMonth(d.getMonth() + 3 * units);
    				break;
    			case 'months':
    				d.setMonth(d.getMonth() + units);
    				break;
    			case 'weeks':
    				d.setDate(d.getDate() + 7 * units);
    				break;
    			case 'days':
    				d.setDate(d.getDate() + units);
    				break;
    			case 'hours':
    				d.setTime(d.getTime() + units * 3600000);
    				break;
    			case 'minutes':
    				d.setTime(d.getTime() + units * 60000);
    				break;
    			case 'seconds':
    				d.setTime(d.getTime() + units * 1000);
    				break;
    			default:
    				break;
    		}
    
    		return d;
    	};
    }
    

    3. Example of using the setup.changeDate to change game time.
    original date: <<print $now>>
    
    <<set $now to setup.changeDate($now, 'minutes', 15)>>
    Added 15 minutes of game time: <<print $now>>
    
    <<set $now to setup.changeDate($now, 'hours', 2)>>
    Added 2 hours of game time: <<print $now>>
    

    You can use the Date object's in-built methods to access the different components (year, month, date, etc...) of the $now variable, and to change the format of the outputed date String. You could use SugarCube's <<widget>> macro or it's Macro API to make custom macros to help you with the calls to the setup.changeDate function.

    Hey! This is super helpful I only struggle with one little thing. I don't manage to only display date and most ideally I would like it to display Weekday, Day, Month, Year i.e. 'Tuesday 10th January, 2017'

    I found the javascript code but I can't manage to include it in such way that Twine actually takes it. I use the latest versions of SugarCube & Twine.

    If you could help me out with that, that would be awesome.
  • greyelf wrote: »
    Instead of using multiple variables to track the different units of game time and having to use logic to correctly update those units you could use a single variable containing a Javascript Date object to do it.

    note: I will use indentation and extra line-breaks in the following examples to format them so they are more readable, that formatting can be safely removed.

    1. Initialise the variable in your StoryInit passage.

    The Date object's documentation explains the different ways you can do this, my example is midnight on the first day of the first month of the year 2000.
    <<set $now to new Date(2000, 0, 1, 0, 0, 0)>>
    

    2. Add Javascript code to help with the changing of the game time.

    The following Javascript code creates a setup.changeDate(date, interval, units) function which you will used to change the date. The interval values it supports are 'years', 'quarters', 'months', 'weeks', 'days', 'hours', 'minutes', and 'seconds'.

    warning: Always use the largest interval that meets your needs, do not try to add multiple days in a single call using a large number of 'hours', 'minutes', or 'seconds' because this can cause the date to become incorrect if you traverse over a leap-year delimiter.

    Add the following to your story's Story Javascript area.
    /* A method to change a date using intervals */
    if (! setup.changeDate) {
    	setup.changeDate = function(date, interval, units) {
    
    		var d = new Date(date); // Don't change original date.
    
    		switch(interval.toLowerCase()) {
    			case 'years':
    				d.setFullYear(d.getFullYear() + units);
    				break;
    			case 'quarters':
    				d.setMonth(d.getMonth() + 3 * units);
    				break;
    			case 'months':
    				d.setMonth(d.getMonth() + units);
    				break;
    			case 'weeks':
    				d.setDate(d.getDate() + 7 * units);
    				break;
    			case 'days':
    				d.setDate(d.getDate() + units);
    				break;
    			case 'hours':
    				d.setTime(d.getTime() + units * 3600000);
    				break;
    			case 'minutes':
    				d.setTime(d.getTime() + units * 60000);
    				break;
    			case 'seconds':
    				d.setTime(d.getTime() + units * 1000);
    				break;
    			default:
    				break;
    		}
    
    		return d;
    	};
    }
    

    3. Example of using the setup.changeDate to change game time.
    original date: <<print $now>>
    
    <<set $now to setup.changeDate($now, 'minutes', 15)>>
    Added 15 minutes of game time: <<print $now>>
    
    <<set $now to setup.changeDate($now, 'hours', 2)>>
    Added 2 hours of game time: <<print $now>>
    

    You can use the Date object's in-built methods to access the different components (year, month, date, etc...) of the $now variable, and to change the format of the outputed date String. You could use SugarCube's <<widget>> macro or it's Macro API to make custom macros to help you with the calls to the setup.changeDate function.
    Using this method, how would I set the current hour to a specific hour without changing any other fields (days, years, months, etc.)?
  • Apologies for necro, but I didn't want to make a new thread for this.

    Trying to use the above code in SugarCube 2. Ideally I just want the hour & minutes and to alter it in game passage by passage, and spit out the time in a sidebar.

    However, I'm struggling to find a way to print *just* the hour and minutes, since I don't need the whole year thing up above, and not in the D/M/Y setting since it's a fantasy game. (If anything, it would be '7th day of Whitefall' or something)
  • edited March 2017
    ... Nobody? T_T

    I really don't want to have to make my own variables, because it'll make the back-end messy as hell. Especially since I want to include the time in <<if>> statements for when certain jobs can be performed.

    I think I need to get getDate() to work, but the correct syntax is eluding me.

    EDIT: Nvm, finally figured it out. <<print $now.getDate()>>
  • edited March 2017
    DairynGM wrote: »
    ... Nobody? T_T
    Less than 24 hours elapsed between your posts. That's not a lot, honestly.

    DairynGM wrote: »
    However, I'm struggling to find a way to print *just* the hour and minutes, since I don't need the whole year thing up above, and not in the D/M/Y setting since it's a fantasy game. (If anything, it would be '7th day of Whitefall' or something)
    You probably want the <Date>.getHours() and <Date.getMinutes()> methods. For example:
    The time is <<print $now.getHours() + ':' + $now.getMinutes()>>.
    
  • edited March 2017
    Less than 24 hours elapsed between your posts. That's not a lot, honestly.

    ... Okay, fair point. I was being a fairly entitled doosh.

    I guess after several hours of trying different syntax, I got frustrated, and I had blinders on to everything else but this particular issue.

    Thank you once again for your help.
  • edited March 2017
    I'm using harlowe

    How would i present the time so the person playing will know the time.
    So this is the variable..
    (set: $time to 9+1)

    I want it to say the time is 09:40
  • nads96 wrote: »
    How would i present the time so the person playing .... is 09:40
    That integer you are storing with the variable does not contain enough information to determine the number of minutes, only the number of hours.

    As I suggested in your other post in another thread related to this question, you will need to use type of value, some options being:

    1a. A Date value (recommended), like that shown in the examples within this threat.

    1b. A Decimal (float), where the number before the decimal point could represent the hours and the number after the minutes.
    9.39  could be 9 hours and 39 minutes.
    9.04 could be 9 houses and 4 minutes.
    9.4 could be 9 hours and 40 minutes.
    

    2. You could also use two variables ($hour, $minute) to store the relevant values.

    Once you choose how you want to store the hours and minutes in your story then someone can explain how to format the output.
Sign In or Register to comment.