[Tutorial] Using a real Datetime to control flow of your game
Posted: Tue Jul 31, 2018 7:10 am
Hi !
I'm already know so much content from this forum and I find myself bad for not sharing myself also what I'm discovering
At the start of my project it was a mesh when I put the factor "time" in the game. It was a mesh, Ints that counts for days , that read from a list of days for names , moments that pass from Morning to evening etc... Read a book ? pass a moment ,cut a flower? pass a moment? ... no
After reading about python datetime and some answers/threads in this forum , I end with a solution that is very simple , compact , easy to translate and allow to control every aspect of your game simply from the date.
First off , I'm learning python myself so probably this code is the most rude and dumb thing you can read. So don't laugh please and try to improve if you have a better method I'm trying to improve here.
First in our script.rpy we put the next :
With this now we have a fully working datetime system with hour included and the methods for change the time (and restart the interaction of the GUI, if you have the time in the guy you want that) and with a date fixed to the time I was writing this.
Now we can create a new screen and add this :
Those two lines show hour and minutes
The second says the Name of the day , number of day and number of month this is easy customizable.
For customize what the clock or day format says , you have the reference in this page :
https://docs.python.org/3.5/library/dat ... e-behavior
For change the time we can use anywhere :
The new days are added automatically , so you don't need to check or change nothing more. simple add time when something happens.
How we can control what kind of backgrounds or things appear at determined hour ?
One important question related to this kind of games is .... Day number , i want to know if is Monday or Saturday . Gladly python already has this method.
For me it was a very lifesaver because it add the option to customize very well your game , add determined events at determined days or determined hours.
It's also works very well with the garden game I implemented (it uses the same datetime to determine when the plant is ready). And also you don't need to put events that happen when a day ends , simply can put "timestamps" to those events and react when compare the timestamp with the game date.
I hope people find this useful. I can upload a demo if people don't get clear this.
Also , I made a modification of the choice system to allow show the usage of time a determined option can have (or not) but also it shows a energy usage , so i need to adapt to show here . Told if people are interested .
Sorry for my bad english and code
I'm already know so much content from this forum and I find myself bad for not sharing myself also what I'm discovering
At the start of my project it was a mesh when I put the factor "time" in the game. It was a mesh, Ints that counts for days , that read from a list of days for names , moments that pass from Morning to evening etc... Read a book ? pass a moment ,cut a flower? pass a moment? ... no
After reading about python datetime and some answers/threads in this forum , I end with a solution that is very simple , compact , easy to translate and allow to control every aspect of your game simply from the date.
First off , I'm learning python myself so probably this code is the most rude and dumb thing you can read. So don't laugh please and try to improve if you have a better method I'm trying to improve here.
First in our script.rpy we put the next :
Code: Select all
init -3 python:
#required imports
import datetime
import locale
from datetime import timedelta
#We create the variable holding the game time and create not from NI , but with manual data , i hate that .now thing.
game_date = datetime.datetime(2018,7,31, hour=12, minute=47, second=0)
#we get the locale from your system, so no need to translate day names , and we work with day numbers , not strings.
locale.setlocale(locale.LC_ALL, '')
#Methods for change time
def change_hour(hour):
global game_date
#timedelta only handles seconds or days so we must convert
segundos = hour * 3600
game_date = game_date + timedelta(seconds=segundos)
renpy.restart_interaction()
def change_minute(minute):
global game_date
segundos = minute * 60
game_date = game_date + timedelta(seconds=segundos)
renpy.restart_interaction()
Now we can create a new screen and add this :
Code: Select all
text(game_date .strftime("%H : %M"))
text(game_date .strftime("%A, %d/%m"))
The second says the Name of the day , number of day and number of month this is easy customizable.
For customize what the clock or day format says , you have the reference in this page :
https://docs.python.org/3.5/library/dat ... e-behavior
For change the time we can use anywhere :
Code: Select all
$ change_hour(5)
$ change_minute(23)
How we can control what kind of backgrounds or things appear at determined hour ?
Code: Select all
if game_date .hour >= 1:
background Frame("bg example_midnight",0,0)
elif game_date .hour >= 8:
background Frame("bg example_day",0,0)
Code: Select all
#monday
if game_date .weekday == 0:
do_something_in_monday()
#sunday
elif game_date .weekday == 6:
do_something_in_sunday()
It's also works very well with the garden game I implemented (it uses the same datetime to determine when the plant is ready). And also you don't need to put events that happen when a day ends , simply can put "timestamps" to those events and react when compare the timestamp with the game date.
I hope people find this useful. I can upload a demo if people don't get clear this.
Also , I made a modification of the choice system to allow show the usage of time a determined option can have (or not) but also it shows a energy usage , so i need to adapt to show here . Told if people are interested .
Sorry for my bad english and code