Page 1 of 2

Time/day system

Posted: Sun Mar 01, 2020 9:00 pm
by iDweadith
Hello guys!

First of all I want to say sorry for my bad english I hope you understand.

So, I'm new renpy user, also I'm studying Python but I think that the code I'm going to use is pretty hard for my knowledges.

So what I need is a time/day/ system

I want the time changes when an event is triggered, something like (if go to bed time set to 00:00)
I've solved this point by using a code made by trooper6 viewtopic.php?f=51&t=21978#p279023 found in the cookbook, I'm using the digital clock, that's very cool, also I can see the animation of the time flowing.

The problem is that I want that everytime the time is 00:00 a new day starts.
So I want a variable called day that everytime the time is 00:00 increases by 1.

So I can count the days

I'm pretty sure that I need a loop (while) but I don't know how to set that

Can you please help me?

Re: Time/day system

Posted: Mon Mar 02, 2020 5:11 am
by trooper6
Is your game going to be running in real time, or will you be adding time whenever the player does an action?

Re: Time/day system

Posted: Mon Mar 02, 2020 5:17 am
by iDweadith
Hi trooper6 thanks for that code in the cookbook :)

Yes I will add time when the player does action

Re: Time/day system

Posted: Mon Mar 02, 2020 8:22 am
by Potato0095
You could use a division floor rounded number + 1 as the day counter. It will depend on your code, however, the logic is the same nonetheless.

Code: Select all

python:
    a = 0			# "a" being the number of hours, you'll set this variable at any moment inside your code.
    day = int(a / 24) + 1	# you'll get a float number. You'll get the day you're in if the integer rounds the number to the floor and adds 1.
e.g: 71 hours divided by 24 will give you 2.95833... Rounding to the floor number and adding 1, 3rd day. 73 hours will give you 3.04166... It's the 4th day.

Edit:
Of course, you can remove the python block and use $ before both of them as this code is very tiny.

If you want to check Python statements inside Ren'Py, check the Documentation:
https://www.renpy.org/doc/html/python.html

Edit 2:
Fixed the logic, instead of "day = a / 24", it's "day = a / 24 + 1"

Re: Time/day system

Posted: Mon Mar 02, 2020 8:28 pm
by trooper6
I don't think it would be that hard to change the code to work for your needs.
I have a big deadline this coming Friday. Let me look into the code and see what sort of changes I can make over the weekend.

Re: Time/day system

Posted: Mon Mar 02, 2020 9:06 pm
by iDweadith
Thanks for the answer Potato0095 and trooper6

Right now I'm trying something like that under, but that doesn't work :(

Code: Select all

self.days = 1 #This is the day I want to start with

	@property
        def day(self):
                return self.days #This method gives me the day

        def advance(self, i=0):
            if h == 0: # H stands for hours
                i = i + 1 # i is the increment I used in the while loop

            while i > 0:
                    self.days = self.days + 1 # if i = 1 self.days increas by 1
                    i -= 1 # the I set (i) back to 0 to restart the cycle
I made this code but probably I'm doing something wrong ç_ç because the day doesn't increase, that is always 1
Thanks trooper6 I'm studying a lot of python and renpy code cause I'm new to programming

Re: Time/day system

Posted: Tue Mar 03, 2020 3:02 am
by Potato0095
First, I want to apologize. My previous logic had a fault. It didn't give the int = day, it was giving "yesterday", so you would need to use:

Code: Select all

day = int(a / 24) + 1
Now, onto the second matter. It's untested, but you could work something with this if needed:

Code: Select all

init python:
    import renpy.store as store

    class Day(store.object):
        def __init__(self, day):
            self.day = day

    def checkday(h):  # $ checkday(h)
        day = int(h / 24) + 1
        return day
The usage inside Ren'Py of this code would be:

$ checkday(the_number_of_hours) # this will automatically return the day to you, no other action is needed, just using the counter.

You could assign a variable to each action, and then, when needed, get the day through this function. In other words, you could have a variable called "hours", and replacing this:

Code: Select all

def checkday(h):  # $ checkday(h)
        day = int(h / 24) + 1
        return day
With this:

Code: Select all

def checkday():  # $ checkday()
        day = int(hours / 24) + 1
        return day
Then assigning the correct number of hours in this variable and running the function like "$ checkday()" would save you a lot of time.

Edit1:
E.g: If you open the console on Ren'Py and use checkday(24) on the first code, it will give you "2". If you assign hours and use checkday() on the latter, it will give you the correct day automatically.

Re: Time/day system

Posted: Tue Mar 03, 2020 3:23 am
by Potato0095
iDweadith wrote: Mon Mar 02, 2020 9:06 pm Thanks for the answer Potato0095 and trooper6

Right now I'm trying something like that under, but that doesn't work :(

Code: Select all

self.days = 1 #This is the day I want to start with

	@property
        def day(self):
                return self.days #This method gives me the day

        def advance(self, i=0):
            if h == 0: # H stands for hours
                i = i + 1 # i is the increment I used in the while loop

            while i > 0:
                    self.days = self.days + 1 # if i = 1 self.days increas by 1
                    i -= 1 # the I set (i) back to 0 to restart the cycle
I made this code but probably I'm doing something wrong ç_ç because the day doesn't increase, that is always 1
Thanks, trooper6 I'm studying a lot of python and renpy code cause I'm new to programming
Hm... maybe it's on "if h == 0", it'll only run i = i + 1 if it is 0. I don't think this is the correct logic, but you're getting there.

A note, however. You should start to write "i += 1" (just like you did with "i -=1") instead of "i = i + 1". It adds 1 no matter what, it's just esthetic and the "normal" thing to do when you code.

Re: Time/day system

Posted: Tue Mar 03, 2020 8:08 pm
by iDweadith
Ok, thanks :) :)
I have a problem using your code, because on the clock system that I'm using, once h > than 23 it resets back to 0.
Something like:

Code: Select all

if h > 23:
	h = h%24

So I can't do like (48/24 + 1) for the day 3, and I get stuck on day 2

Re: Time/day system

Posted: Wed Mar 04, 2020 3:25 am
by Potato0095
iDweadith wrote: Tue Mar 03, 2020 8:08 pm Ok, thanks :) :)
I have a problem using your code, because on the clock system that I'm using, once h > than 23 it resets back to 0.
Something like:

Code: Select all

if h > 23:
	h = h%24

So I can't do like (48/24 + 1) for day 3, and I get stuck on day 2
Oh, you're using trooper6's code, I kind of forgot about that, this would've made this easier (or not). Sorry, both of you. Now, let's get to work. I won't add the whole code here, you can use ctrl + f to find specific parts of the code where I add things and change there. I assume you're using the digital clock (please say you do or said this before and I don't remember), so I'll work with that.

Trooper6 can correct me if I'm wrong (I usually am), but we could add a self.hours and a self.days to the variables, just for the sake of precise control over our self.days (I mess up when I use minutes or seconds to count days).

Code: Select all

#Variables for handling time
            self.days = 1
            self.hours = self.minutes/60
            self.minutes = (h*60)+m            # max minutes in a day are 1440
From here, we can add an if-else statement in the render function, just so it updates every second, checking for the number of hours passed and if it went over 24.

Code: Select all

def render(self, width, height, st, at):

            # How many days have passed
            if self.hours > 23:
                self.days += int(self.hours / 24.1 + 1)  # As I said before, the logic is the same nonetheless

Re: Time/day system

Posted: Wed Mar 04, 2020 7:51 pm
by iDweadith
Thanks a lot for the help
So I tried your code the logic I think is right but dunno why it isn't working for me.

I mean, I did some test and self.hours always gives me 0
While self.minutes gives me the expected value but never reaches 1440 due to the reset

Re: Time/day system

Posted: Thu Mar 05, 2020 2:40 am
by Potato0095
Did you misspell anything, or didn't put anything in the right part of the document? Check if your "if self.hours > 23:" statement is inside the "def render". You don't need to create one, use the one already in the code. Check your indentation, your code should be like this:

Code: Select all

def render(self, width, height, st, at):
            if self.seconds_target:
                if self.forward:
                    #Advances the seconds variable until it is the same as the seconds_target variable
                    if self.seconds_target > self.seconds:
                        self.seconds += self.step
                    #Resets the seconds_target variable
                    if self.seconds_target <= self.seconds:
                        self.seconds_target = 0
                else:   
                    if self.seconds_target < self.seconds:
                        self.seconds -= self.step
                    if self.seconds_target >= self.seconds:
                        self.seconds_target = 0
                        self.forward = True
             
            if self.hours > 23:
                self.days += int(self.hours / 24.1 + 1)  # As I said before, the logic is the same nonetheless
    
            #Makes sure the minutes variable is always in sync with the seconds variable
            if self.minutes != self.seconds//60:
                self.minutes = self.seconds//60
                
            # Calculating how many seconds have passed today.
            if self.seconds >= 86400:
                self.seconds -= 86400

                if self.seconds_target:
                    self.seconds_target -= 86400
                    
            elif self.seconds < 0:
                self.seconds += 86400
                
                if self.seconds_target:
                    self.seconds_target += 86400

If this doesn't work, it's out of my scope, I wouldn't understand why it isn't working and would start to put that if statement anywhere I would think it would work, like on the digital render (below else) and such.

Re: Time/day system

Posted: Thu Mar 05, 2020 11:34 am
by iDweadith
My code is the same as yours up there
I'm still trying but nothing

Re: Time/day system

Posted: Thu Mar 05, 2020 2:00 pm
by trooper6
IDwedith, I’m going to look into recoding my clock to include a day marker, but I can’t do it until this weekend. But I’ll look into it!

Re: Time/day system

Posted: Thu Mar 05, 2020 3:55 pm
by iDweadith
Thanks trooper6 I'll wait then. :D :D

Also I'm learning many things these days.