Page 1 of 1

[Tutorial] Using a real Datetime to control flow of your game

Posted: Tue Jul 31, 2018 7:10 am
by enrohk
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 :

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() 
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 :

Code: Select all

text(game_date .strftime("%H : %M"))
text(game_date .strftime("%A, %d/%m"))
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 :

Code: Select all

	$ change_hour(5)
	$ change_minute(23)
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 ?

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)
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.

Code: Select all

	#monday
        if game_date .weekday == 0:
            do_something_in_monday()
        #sunday
        elif game_date .weekday == 6:
            do_something_in_sunday()
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 ;)

Re: [Tutorial] Using a real Datetime to control flow of your game

Posted: Sat Aug 04, 2018 5:37 am
by SuzuAyaGT
thank you so much for this!

Re: [Tutorial] Using a real Datetime to control flow of your game

Posted: Sat Aug 04, 2018 4:58 pm
by enrohk
Hi !
I Just found how RenPy works saving variables hahaha
Probably this code WON'T save the date if you save the game. To solve this simply put the "game_date" variable at "start" label

Code: Select all

label start:
	$ game_date = datetime.datetime(2018,7,31, hour=12, minute=47, second=0)
I think with this approach should be saved correctly , I gonna test myself because I need that :lol:
Maybe is better put a demo when is all finished and working good haha

EDITED Yes. it get saved if you set in the start label , I do some testings and when you modify the date or time and save the game it get save/loaded correctly
Btw I'm thinking of making it a class with more methods for compare dates , return time in days , etc...

Re: [Tutorial] Using a real Datetime to control flow of your game

Posted: Sun Aug 05, 2018 2:46 am
by trooper6
That way will also give you problems. The current best practices is to declare your variables outside of any block using the “default” keyword.

Re: [Tutorial] Using a real Datetime to control flow of your game

Posted: Sun Aug 05, 2018 3:04 am
by enrohk
trooper6 wrote: Sun Aug 05, 2018 2:46 am That way will also give you problems. The current best practices is to declare your variables outside of any block using the “default” keyword.
U mean this part of documentation ?
https://www.renpy.org/doc/html/python.h ... -statement
Sorry for this noobish question , but that means the method I was using before of putting all the variables and classes constructor in a file outside a label was a good pratique but only need to add "default" to those classes ?
Gonna test this off . Thank you very much for that info :)

Re: [Tutorial] Using a real Datetime to control flow of your game

Posted: Sat Feb 05, 2022 12:19 am
by Trafagal
enrohk wrote: 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.
This looks like very useful. I wonder if you will still update the code. Thanks for sharing this!