Help trigger event based on time - Renpy

pobrecito007

Newbie
Dec 18, 2019
21
1
Hi,
I have a problem triggering an event based on time. The scene is the following:

1)A phone needs to be charged- It was completely empty,
2)The phone is charging and has a little of battery
3) After 6 hours the battery is half charged.
4) after additional 6 hours the phone is finally completely charged and useable.


I have a problem concerning step 3. I dont know how to set time that after 6 hours the battery is half charged.
The player should be able to charge the phone anytime but 12hrs should pass until he can use it.

Can someone please help me?
Thanks a lot in advanced
 
Last edited:

rayminator

Engaged Member
Respected User
Sep 26, 2018
3,041
3,140
are you do a 12hrs or 24hrs with either one you would have to do some calculations
whats your code looks like for the battery?
there is a lot of way of doing it like when he/she gets out of bed use variable like $ cell_battery = 100
I'm guessing cause I don't know what your code looks like
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,382
15,290
Can someone please help me?
Well, without knowing how you handle the time passing in your game, it will be difficult.

But globally speaking, every single hour you should have something like :
Python:
default phoneBattery = 100
default phoneInCharge = False

label yourHourPassingLabel:
    [...]
    # Will only happen if the phone is in charge.
    if phoneInCharge is True:
        # 12/100 is 8.3, but using 9 still lead to a 12 hours charge
        # for 0% to 100% without having to use floats.
        $ phoneBattery += 9
        if phoneBattery > 100
            $ phoneBattery = 100
    [...]
Then, when the player put his phone in charge, you write this $ phoneInCharge = True, and when he stop charging the phone, you use this $ phoneInCharge = False.
 

moskyx

Forum Fanatic
Jun 17, 2019
4,005
12,961
Aside from technical issues, I hope your story explains in a reasonable way why a phone needs 12 hours to be fully charged and why you can't use it if it's not fully charged. Suspension of disbelief has its limits, you might want to rethink this time slots or even come up with a different item instead of a phone
 

pobrecito007

Newbie
Dec 18, 2019
21
1
This is my time code:
Code:
label world_time:
    if world_time_minute >=60:

        $ world_time_minute = world_time_minute -60
        $ world_time_hour = world_time_hour + 1

        while world_time_minute >= 60:
            $ world_time_minute = world_time_minute -60
            $ world_time_hour = world_time_hour + 1

        if world_time_hour >=8 and world_time_hour <=20:
            $ world_day_night = "Day"
        else:
            $ world_day_night = "Night"

        if world_time_hour >=24:
            $ world_time_hour = world_time_hour -24
            $ world_daycount = world_daycount + 1

            if world_day <=6:
                $ world_day = world_day + 1

            else:
                $ world_day = 1
                

    return
I have set an default, well actually 3 (phoneincharge, phoneinchargehalf, phoneinchargefull) half and full are the amounts of the battery level. I have also put the first default when the phone gets charged and it works perfectly.
I only having problems that after 6 hrs of charging the screen of the display should change into half full battery level and after another 6 hours the phone is charged.
 

pobrecito007

Newbie
Dec 18, 2019
21
1
I tried to manage to make the phone fully charged on the next day.
No more ideas how i could do it based on hours?
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,583
2,223
I have set an default, well actually 3 (phoneincharge, phoneinchargehalf, phoneinchargefull) half and full are the amounts of the battery level. I have also put the first default when the phone gets charged and it works perfectly.
I only having problems that after 6 hrs of charging the screen of the display should change into half full battery level and after another 6 hours the phone is charged.

To expand on Anne's solution, based on your example...

Python:
default battery_level = 100

label world_time:
    if world_time_minute >=60:

        while world_time_minute >= 60:
            $ world_time_minute = world_time_minute -60
            $ world_time_hour = world_time_hour + 1
            if phoneincharge:
                call recharge_cellphone
            else:
                call discharge_cellphone

        if world_time_hour >=8 and world_time_hour <=20:
            $ world_day_night = "Day"
        else:
            $ world_day_night = "Night"

        if world_time_hour >=24:
            $ world_time_hour = world_time_hour -24
            $ world_daycount = world_daycount + 1

            if world_day <=6:
                $ world_day = world_day + 1

            else:
                $ world_day = 1
              
    return


label recharge_cellphone:

    $ battery_level += 9  # full charge battery from 0 to 100 in 12 hours.

    if battery_level >= 100:
        $ battery_level = 100
        $ phonefullcharge = True
        $ phonehalfcharge = False
    elif battery_level >= 50:
        $ phonehalfcharge = True
        $ phonefullcharge = False
    else:
        $ phonehalfcharge = False
        $ phonefullcharge = False

    return


label discharge_cellphone:
    # assuming no other battery use mechanic, like increased battery drain while watching video, etc.

    $ battery_level -= 4  # full discharge of battery from 100 to 0 in a little over 24 hours.

    if battery_level <= 0:
        $ battery_level = 0
        $ phonehalfcharge = False
        $ phonefullcharge = False
    elif battery_level <= 50
        $ phonehalfcharge = True
        $ phonefullcharge = False
    else:
        $ phonefullcharge = True
        $ phonehalfcharge = False

    return

This code holds the battery level as a percentage in the variable battery_level. It starts at full charge.

If the phone is on charge, it is increased by 9%. While charging the battery level will show as empty until it reaches 50% and won't show full until it reaches 100%.

If the phone is not on charge, I'm assuming a nominal discharge rate. I've picked 4%, since that will drain the battery from 100% to 0% in a little over 24 hours. You may or not want to use this mechanic. However I'm treating the battery level indicators different during discharge - showing full battery from 100% to 51% and half battery until the battery reaches 0%. I figure this is better than showing the "half battery" indicator when the battery is at 96%... though obviously you may view that differently. It will look odd though if the phone is put on charge (e.g. battery at 80% on charge will show half battery, but will show full while not on charge).

A better way would be to not use the batteryhalfcharge or batteryfullcharge values to pick static images, and instead use a as part of your to show the actual value of battery_level.

Also, I removed a duplicated bit of code in your example. You advance the hour by 1 when the minutes exceed 60 and then enter a loop to see if the hour needs increasing again. You only need the loop, since it's doing that already. I assume you're adding minutes in 15, 30, 60, 120, 180, etc blocks - allowing time to advance by any number of hours up to 24.
 
Last edited:
  • Like
Reactions: Madmanator99

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,382
15,290
Python:
label discharge_cellphone:
    # assuming no other battery use mechanic, like increased battery drain while watching video, etc.

    $ battery_level -= 4  # full discharge of battery from 100 to 0 in a little over 24 hours.
Perhaps that -4 is a little high for a natural discharge. Perhaps even the idea of a natural discharge should be forgotten.
It all depend of the needed use of the phone in the game, but I think that if the player have to charge it too often, and for half a day, it should not be over used. Else it will because something working against the game, and not anymore a "cool addition".
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,583
2,223
Perhaps that -4 is a little high for a natural discharge. Perhaps even the idea of a natural discharge should be forgotten.

[...] Else it will because something working against the game, and not anymore a "cool addition".
I agree.
If I'm honest, I added the code to show it could be done and hint that it should not be done at all (or at least a smaller number used).

Like those games that include eating/drinking/body heat mechanics - it often makes the game annoying and a chore, especially when it is not a core mechanic of the game.

It'll very much depend on the game and how time is handled.
The thing that threw me was that time was being measured in minutes. It's such a precise measurement when the battery was measured as purely empty/half/full.

I'm sure the author has thought it through. 12 hrs to charge a cellphone seems excessive, but perhaps it is needed to stop the player using the cellphone too often. I guess we'll see when the game sees it's initial release. Good luck.
 
  • Like
Reactions: Madmanator99

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,382
15,290
Like those games that include eating/drinking/body heat mechanics - it often makes the game annoying and a chore, especially when it is not a core mechanic of the game.
I agree. It can add something to the game, but it should stay discrete.
In place of a starving bar, by example, something like that would be enough :
Code:
label startOfTheDay:
    if eaten is False :
        starving += 1
        if starving >= 3:
           "You're too weak to do anything today, eat or die, your choice."
        else:
           "You feel weaker today, you shouldn't have forgot to eat yesterday."
    else:
        starving = 0
It would also feel less like a constraint, especially since the starving bar generally imply that you've to eat three time a day, when not sometimes more than that.
There's no need to effectively count the repletion. Despite being less realist, eating an apple having the same result than going to a banquet would be more enjoying.


[...] but perhaps it is needed to stop the player using the cellphone too often.
It's how I saw it myself. There's games where the MC can put tons of camera then look at them on his phone. Taking in count the battery charge is a good way to limit the abuse.
 
  • Like
Reactions: Madmanator99

pobrecito007

Newbie
Dec 18, 2019
21
1
Woooow you guys are incredible!!
First of all thank you soooooo much for the effort you guys invested....I really do appreciate that!! I havent tried the code yet but tomorrow i will!!

I am not going to implement sth like drink/food and for sure not 3 times a day. I am trying to do some things a bit different, like the thing with charging the phone. I have implemented a energy bar and after 1 hour of whatever you do you loose energy.

@79Flavours: Yes you are right. I should maybe bring it down to 6 hrs, but i wanna try it first with 12 hrs. If not i will tweak it to the right hours. Kind of when you go to bed dont forget to charge your phone, if not waster 6 hrs of less whatever. :)

Thanks a again guys!!!