Howdy, Stranger!

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

How To Tell If A Day Has Passed

In my game, I want a system where every day you can get random loot. However, I don't know how to tell it when a day has passed. Is that possible? I use Harlowe.

Comments

  • Do you mean:

    1. A day of in-game time?

    You the Author control how much game-time passes for each action the Reader chooses, and what action ends the current game-day. There are a number of different methods to track the passing of a game-day and most of them use one or more $variables to do so.

    1a. Hours per day: (two variables $hour and $day)

    At the start of your game set the $day variable to the first day of your game-calendar. (set: $day to 1)

    At the start of the game-day you set the $hour variable to the first hour of your game's work-day. (set: $hour to 8) for 8am.

    For each game action that causes game-time to pass you increase the $hour variable by the number of hours that action takes. (set: $hour to it + 2) for action that takes two hours.

    After each increase of the $hour variable's value check if its current value is equal to or greater than the last hour of your game-day, and if it is then increment the $day variable by one and reset the $hour variable back to the start of the work-day. Also determine what random thing you want to happen.

    eg. If the end of your game work-day is midnight (24th hour)
    (if: $hour >= 24)[
    	(set: $day to it + 1)
    	(set: $hour to 8)
    	<!-- Place code to determine your random action after this comment. -->
    ]
    

    1b. Set number of action/turns per day: (two variables $turn and $day)

    This is very similar to 1a except you replace the $hour variable with the $turn variable. Because you are counting the number of actions/turns the Reader is taking the default value of the $turn variable at both the start of the game and after incrementing the $day variable should the zero. (set: $turn to 0)

    eg. If there are only 8 actions allow per day.
    (if: $turn >= 8)[
    	(set: $day to it + 1)
    	(set: $turn to 0)
    	<!-- Place code to determine your random action after this comment. -->
    ]
    

    2. A day of Real-world time?
  • Real world time. Sorry for wasting your time making all that code, I thought it was obvious that I meant real world. :disappointed:
  • You can get the current date and time like this:
    (set: $var to Date())
    
    $var
    

    From there it should be possible to use string manipulations to extract the day number and time (more tedious than difficult). Calling the built-in functions to extract these fields for you (the getday(), getHours()) is unfortunately not possible right now in Harlowe, as far as I know.
  • Sorry, I don't understand this. :confused:
  • thebigh is talking about a Javascript Date object, which is used to represent a particular date and time (also known as a Time-stamp).

    eg. To store an object that represents the current date and time in a variable.
    (set: $var to new Date())
    

    You can use the object's methods that are described in the linked MDN documentation to access the different parts of the date and time (year, month, day, hour, minutes, seconds, etc..)
    (set: $var to new Date())
    
    year: (print: $var.getFullYear())
    
    hour: (print: $var.getHours())
    
    note: Midnight is represented by zero, not twenty four.
  • greyelf wrote: »
    thebigh is talking about a Javascript Date object, which is used to represent a particular date and time (also known as a Time-stamp).

    eg. To store an object that represents the current date and time in a variable.
    (set: $var to new Date())
    

    You can use the object's methods that are described in the linked MDN documentation to access the different parts of the date and time (year, month, day, hour, minutes, seconds, etc..)
    (set: $var to new Date())
    
    year: (print: $var.getFullYear())
    
    hour: (print: $var.getHours())
    
    note: Midnight is represented by zero, not twenty four.

    Please, I don't understand. Could you tell me what to do step-by-step?
  • I am going to make some assumptions:
    . You have implemented both a 'save game' and 'load game' feature.
    . The day can change while the game is actively being played.
    . There can be multiple days between each time the game is played.
    . You want to check if the day has changed before showing the current passage.

    The following solution uses three variables:

    - $today which contains an Date object representing the current 'date'.

    - $newDay which is either true or false depending on if the day has change.

    - $now which is a temporary variable containing the date right this second, it is used in the checking code.

    1. Initialise the variables in your story's

    startup tagged passage.
    (set: $today to new Date())
    (set: $newDay to false)
    

    2. The Day Check passage.

    This passage will contain the both the code used to check if the day has changed, and your special event code. The passage need to be assigned a header tag.

    note: I have included extra line-breaks, indentation and comments to make the following more readable.
    <!-- What is the date right now! -->
    (set: $now to new Date())
    
    <!-- Has the Year, Month or Day change, if so then it is a different day. -->
    (if: $now.getFullYear() != $today.getFullYear() or
    	$now.getMonth() != $today.getMonth() or
    	$now.getDate() != $today.getDate())[
    
    	<!-- Change the newDay flag to indicate the day has change, and update today to be the new date. -->
    	(set: $newDay to true)
    	(set: $today to $now)
    ]
    
    <!-- If it is a new day do the special event. -->
    (if: $newDay)[
    
    	<!-- The code for your special event goes here. -->
    
    	<!-- Set the newDay flag back to false. -->
    	(set: $newDay to false)
    ]
    

    3. Hide the startup and header tagged passage's output.

    The following CSS hides the extra blank lines generated by these passages, it goes in your story's Story Stylesheet area. It assumes that you named the header tagged passage Day Check
    tw-include[type="startup"], tw-include[title="Day Check"] {
    	display: none;
    }
    
  • greyelf wrote: »
    I am going to make some assumptions:
    . You have implemented both a 'save game' and 'load game' feature.
    . The day can change while the game is actively being played.
    . There can be multiple days between each time the game is played.
    . You want to check if the day has changed before showing the current passage.

    The following solution uses three variables:

    - $today which contains an Date object representing the current 'date'.

    - $newDay which is either true or false depending on if the day has change.

    - $now which is a temporary variable containing the date right this second, it is used in the checking code.

    1. Initialise the variables in your story's

    startup tagged passage.
    (set: $today to new Date())
    (set: $newDay to false)
    

    2. The Day Check passage.

    This passage will contain the both the code used to check if the day has changed, and your special event code. The passage need to be assigned a header tag.

    note: I have included extra line-breaks, indentation and comments to make the following more readable.
    <!-- What is the date right now! -->
    (set: $now to new Date())
    
    <!-- Has the Year, Month or Day change, if so then it is a different day. -->
    (if: $now.getFullYear() != $today.getFullYear() or
    	$now.getMonth() != $today.getMonth() or
    	$now.getDate() != $today.getDate())[
    
    	<!-- Change the newDay flag to indicate the day has change, and update today to be the new date. -->
    	(set: $newDay to true)
    	(set: $today to $now)
    ]
    
    <!-- If it is a new day do the special event. -->
    (if: $newDay)[
    
    	<!-- The code for your special event goes here. -->
    
    	<!-- Set the newDay flag back to false. -->
    	(set: $newDay to false)
    ]
    

    3. Hide the startup and header tagged passage's output.

    The following CSS hides the extra blank lines generated by these passages, it goes in your story's Story Stylesheet area. It assumes that you named the header tagged passage Day Check
    tw-include[type="startup"], tw-include[title="Day Check"] {
    	display: none;
    }
    

    Thank you so much!!! You know everything about twine! Please write a book about it and I will buy it!
  • greyelf wrote: »
    I am going to make some assumptions:
    . You have implemented both a 'save game' and 'load game' feature.
    . The day can change while the game is actively being played.
    . There can be multiple days between each time the game is played.
    . You want to check if the day has changed before showing the current passage.

    The following solution uses three variables:

    - $today which contains an Date object representing the current 'date'.

    - $newDay which is either true or false depending on if the day has change.

    - $now which is a temporary variable containing the date right this second, it is used in the checking code.

    1. Initialise the variables in your story's

    startup tagged passage.
    (set: $today to new Date())
    (set: $newDay to false)
    

    2. The Day Check passage.

    This passage will contain the both the code used to check if the day has changed, and your special event code. The passage need to be assigned a header tag.

    note: I have included extra line-breaks, indentation and comments to make the following more readable.
    <!-- What is the date right now! -->
    (set: $now to new Date())
    
    <!-- Has the Year, Month or Day change, if so then it is a different day. -->
    (if: $now.getFullYear() != $today.getFullYear() or
    	$now.getMonth() != $today.getMonth() or
    	$now.getDate() != $today.getDate())[
    
    	<!-- Change the newDay flag to indicate the day has change, and update today to be the new date. -->
    	(set: $newDay to true)
    	(set: $today to $now)
    ]
    
    <!-- If it is a new day do the special event. -->
    (if: $newDay)[
    
    	<!-- The code for your special event goes here. -->
    
    	<!-- Set the newDay flag back to false. -->
    	(set: $newDay to false)
    ]
    

    3. Hide the startup and header tagged passage's output.

    The following CSS hides the extra blank lines generated by these passages, it goes in your story's Story Stylesheet area. It assumes that you named the header tagged passage Day Check
    tw-include[type="startup"], tw-include[title="Day Check"] {
    	display: none;
    }
    

    You. Are. The Best!
  • greyelf wrote: »
    I am going to make some assumptions:
    . You have implemented both a 'save game' and 'load game' feature.
    . The day can change while the game is actively being played.
    . There can be multiple days between each time the game is played.
    . You want to check if the day has changed before showing the current passage.

    The following solution uses three variables:

    - $today which contains an Date object representing the current 'date'.

    - $newDay which is either true or false depending on if the day has change.

    - $now which is a temporary variable containing the date right this second, it is used in the checking code.

    1. Initialise the variables in your story's

    startup tagged passage.
    (set: $today to new Date())
    (set: $newDay to false)
    

    2. The Day Check passage.

    This passage will contain the both the code used to check if the day has changed, and your special event code. The passage need to be assigned a header tag.

    note: I have included extra line-breaks, indentation and comments to make the following more readable.
    <!-- What is the date right now! -->
    (set: $now to new Date())
    
    <!-- Has the Year, Month or Day change, if so then it is a different day. -->
    (if: $now.getFullYear() != $today.getFullYear() or
    	$now.getMonth() != $today.getMonth() or
    	$now.getDate() != $today.getDate())[
    
    	<!-- Change the newDay flag to indicate the day has change, and update today to be the new date. -->
    	(set: $newDay to true)
    	(set: $today to $now)
    ]
    
    <!-- If it is a new day do the special event. -->
    (if: $newDay)[
    
    	<!-- The code for your special event goes here. -->
    
    	<!-- Set the newDay flag back to false. -->
    	(set: $newDay to false)
    ]
    

    3. Hide the startup and header tagged passage's output.

    The following CSS hides the extra blank lines generated by these passages, it goes in your story's Story Stylesheet area. It assumes that you named the header tagged passage Day Check
    tw-include[type="startup"], tw-include[title="Day Check"] {
    	display: none;
    }
    

    Thanks. However, I am afraid that this will mess up my saving system. Will it?
Sign In or Register to comment.