Variable change based on time

korukoru

New Member
Aug 13, 2020
11
2
I know there have been a couple of threads based on my question but none of them seem to have helped, I ran out of options and I'm not good at programming. What I want to do is basically to change the value of the variables based on hour. The issue that I have is that when I check the variables they have their initial value no matter the time, I made sure to call update_location_names() in all the places that were needed. Could it be that once the sub_place_data has take its values they don't change even if the variables do?


Python:
init python:
    class SubPlace(object):
        def __init__(self, ID, parent, name, x, y, iconPhoto, IsActive):
            self.ID = ID
            self.parent = parent
            self.name = name
            self.IsActive = IsActive
            self.x = x
            self.y = y
            self.iconPhoto = iconPhoto

    SubLocations = []
    Places = []
    t = 0

    while t < 500:
        SubLocations.append(SubPlace(t, -1, "",0, 0,"", False))
        Places.append(Place(t, 0, 0, "", False))
        t += 1


    Places[0] = Place(0, 1000, 700, "Entrance", True)
    #Places[1] = Place(1, 200, 700, "mall", True)
    #Places[2] = Place(2, 1200, 300, "lobby", True)
    #Places[3] = Place(3, 1700, 900, "office", True)

    sub_place_data = []
    SubLocations = [SubPlace(t, -1, "",0, 0,"", False) for t in range(500)]

    MyRoom = "My room"
    MomRoom = "Mom room"
    LilSisRoom = "lil sis room"
    Toilet = "toilet"
    LivingRoom = "livingroom"
    Kitchen = "kitchen"
    WashingRoom = "Washing Room"
    Bathroom = "Bathroom"
    Entrance = "Entrance"
    Housefront = "Housefront"
    Class1 = "Class1"
    Principal = "Principal"
    MedicRoom = "MedicRoom"
    LockerRoom = "LockerRoom"
    SchoolToilets = "toilets"
    Office = "Office"

    def update_location_names():
        global MyRoom, MomRoom, LilSisRoom, Toilet, Livingroom, WashingRoom, Bathroom, Entrance, Housefront, Class1, Principal, MedicRoom, LockerRoom, SchoolToilets, Office
        MyRoom = "My room" if -1 <= calendar.Hours < 12 else "My room evening" if 12 <= calendar.Hours < 18 else "My room night"
        MomRoom = "Mom room" if -1 <= calendar.Hours < 12 else "Mom room evening" if 12 <= calendar.Hours < 18 else "Mom room night"
        LilSisRoom = "Lil sis room" if -1 <= calendar.Hours < 12 else "Lil sis room evening" if 12 <= calendar.Hours < 18 else "Lil sis room night"
        Toilet = "Toilet" if -1 <= calendar.Hours < 12 else "Toilet evening" if 12 <= calendar.Hours < 18 else "Toilet night"
        Livingroom = "Livingroom" if -1 <= calendar.Hours < 12 else "Livingroom evening" if 12 <= calendar.Hours < 18 else "Livingroom night"
        WashingRoom = "Washing Room" if -1 <= calendar.Hours < 12 else "Washing Room evening" if 12 <= calendar.Hours < 18 else "Washing Room night"
        Bathroom = "Bathroom" if -1 <= calendar.Hours < 12 else "Bathroom evening" if 12 <= calendar.Hours < 18 else "Bathroom night"
        Entrance = "Entrance" if -1 <= calendar.Hours < 12 else "Entrance evening" if 12 <= calendar.Hours < 18 else "Entrance night"
        Housefront = "Housefront" if -1 <= calendar.Hours < 12 else "Housefront evening" if 12 <= calendar.Hours < 18 else "Housefront night"
        Class1 = "Class1" if -1 <= calendar.Hours < 12 else "Class1 evening" if 12 <= calendar.Hours < 18 else "Class1 night"
        Principal = "Principal" if -1 <= calendar.Hours < 12 else "Principal evening" if 12 <= calendar.Hours < 18 else "Principal night"
        MedicRoom = "MedicRoom" if -1 <= calendar.Hours < 12 else "MedicRoom evening" if 12 <= calendar.Hours < 18 else "MedicRoom night"
        LockerRoom = "LockerRoom" if -1 <= calendar.Hours < 12 else "LockerRoom evening" if 12 <= calendar.Hours < 18 else "LockerRoom night"
        SchoolToilets = "SchoolToilets" if -1 <= calendar.Hours < 12 else "SchoolToilets evening" if 12 <= calendar.Hours < 18 else "SchoolToilets night"
        Office = "Office" if -1 <= calendar.Hours < 12 else "Office evening" if 12 <= calendar.Hours < 18 else "Office night"

    sub_place_data = [
        (0, 0, MyRoom, 50, 900, "SubLocationIcons/MCBedroomIcon_%s.png", True),
        (1, 0, MomRoom, 230, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
        (2, 0, LilSisRoom, 410, 900, "SubLocationIcons/LittleSisterBedroom_%s.png", True),
        (3, 0, Toilet, 590, 900, "SubLocationIcons/ToiletIcon_%s.png", True),
        (4, 0, "Livingroom", 770, 900, "SubLocationIcons/livingroomIcon_%s.png", True),
        (5, 0, WashingRoom, 950, 900, "SubLocationIcons/WashingRoomIcon_%s.png", True),
        (6, 0, Bathroom, 1130, 900, "SubLocationIcons/Bathroom_%s.png", True),
        (7, 0, Entrance, 1310, 900, "SubLocationIcons/Entrance_%s.png", True),
        (8, 0, Housefront, 1490, 900, "SubLocationIcons/HousefrontIcon_%s.png", True),
        (9, 3, Class1, 50, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
        (10, 3, Principal, 230, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
        (11, 3, MedicRoom, 410, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
        (12, 3, LockerRoom, 590, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
        (13, 3, SchoolToilets, 770, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
        (14, 3, Office, 950, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True)
    ]
    update_location_names()

    for i, data in enumerate(sub_place_data):
        SubLocations[i] = SubPlace(*data)
 

osanaiko

Engaged Member
Modder
Jul 4, 2017
2,170
3,594
First thing to do is take a step back - you've fallen into the trap where you're digging down a hole... looking for the right type of clay... to make strong bricks... for your house... to keep the rain off your head... but all along, you already had an umbrella.

Okay, that's kind of a bad analogy and maybe not helpful. But I can guarantee there's a better way to proceed if you back up and reconsider what you are trying to achieve.

If I understand correctly form the small info you have provided: You are making a game with a bunch of locations and time of day. You're stuck on how to get the display name of the location to change based on the progress of time.

Looking at your code, every location + time variation just appends a string to the location name. So how about you have one function:

Code:
def location_with_time(location_name, time_in_hours):
    if time_in_hours  > 18:
        return location_name + " night"
    elif time_in_hours  > 12:
        return location_name + " evening"
    else:
        return location_name
And call this function whenever you need to ***display*** the location name. You don't care about having the full name internally to your logic - you only need to keep track of player location and time, because you can derive all the rest from that.

Now, this suggestion above is still not great, there's a lot of more structure to make a game logic model that will work consistently. It's just an example to show you there is more simple ways to go about stuff.

I'd recommend finding several games you like with the style you want and decompiling them to understand how the dev has implemented their model. And stay aware that some devs are also beginners or not "experienced coders", and so might not be good examples - either too poorly designed or too complex (i.e. summertime saga's quest / statemachine code is pretty amazing but is complicated to understand). Only by looking at multiple examples will you start to get the more general picture of how this type of game can be successfully implemented.
 
  • Like
Reactions: korukoru

korukoru

New Member
Aug 13, 2020
11
2
I didn't know you can decompile games, now that I've looked at a couple of games, mainly the ones from Mr. C, each room has a different screen and things are a lot more spread out. I was afraid of having to rethink my whole code and refactor it but I think that might be the best idea. Thank you for the suggestions and taking the time to respond
 
  • Love
Reactions: osanaiko

osanaiko

Engaged Member
Modder
Jul 4, 2017
2,170
3,594
I'm glad to help. I gave the advice because I had a similar experience to you when first trying to make some things. It required me to have a "paradigm shift" in how I thought about game internal code design. I'm still no expert of course.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,251
15,033
In addition to what Osanaiko said, I'll address the reason of your issue:


Python:
   def update_location_names():
        global MyRoom, MomRoom, LilSisRoom, Toilet, Livingroom, WashingRoom, Bathroom, Entrance, Housefront, Class1, Principal, MedicRoom, LockerRoom, SchoolToilets, Office
        MyRoom = "My room" if -1 <= calendar.Hours < 12 else "My room evening" if 12 <= calendar.Hours < 18 else "My room night"
Contrarily to what you think, this is working perfectly fine. Take a look at the value of "MyRoom", and you'll see it change accordingly to the time.


It's the values in "sub_place_data" that do not change. And they do not change because:

Python:
    sub_place_data = [
        (0, 0, MyRoom, 50, 900, "SubLocationIcons/MCBedroomIcon_%s.png", True),
You defined them once, and never changed them latter.
 

korukoru

New Member
Aug 13, 2020
11
2
In case someone has the same problem as me and wants to know how I somehow solved it.
Firstly, I just made an initial sub_place_data that just hold the initial values

Python:
init python:
    class SubPlace(object):
        def __init__(self, ID, parent, name, x, y, iconPhoto, IsActive):
            self.ID = ID
            self.parent = parent
            self.name = name
            self.IsActive = IsActive
            self.x = x
            self.y = y
            self.iconPhoto = iconPhoto

    SubLocations = []
    Places = []
    t = 0

    while t < 500:
        SubLocations.append(SubPlace(t, -1, "",0, 0,"", False))
        Places.append(Place(t, 0, 0, "", False))
        t += 1


    Places[0] = Place(0, 1000, 700, "Entrance", True)
    #Places[1] = Place(1, 200, 700, "mall", True)
    #Places[2] = Place(2, 1200, 300, "lobby", True)
    #Places[3] = Place(3, 1700, 900, "office", True)

    sub_place_data = [
        (0, 0, "My room", 50, 900, "SubLocationIcons/MCBedroomIcon_%s.png", True),
        (1, 0, "Mom room", 230, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
        (2, 0, "Lil sis room", 410, 900, "SubLocationIcons/LittleSisterBedroom_%s.png", True),
        (3, 0, "Toilet", 590, 900, "SubLocationIcons/ToiletIcon_%s.png", True),
        (4, 0, "Livingroom", 770, 900, "SubLocationIcons/livingroomIcon_%s.png", True),
        (5, 0, "Washing Room", 950, 900, "SubLocationIcons/WashingRoomIcon_%s.png", True),
        (6, 0, "Bathroom", 1130, 900, "SubLocationIcons/Bathroom_%s.png", True),
        (7, 0, "Entrance", 1310, 900, "SubLocationIcons/Entrance_%s.png", True),
        (8, 0, "Housefront", 1490, 900, "SubLocationIcons/HousefrontIcon_%s.png", True),
        (9, 3, "Class1", 50, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
        (10, 3, "Principal", 230, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
        (11, 3, "MedicRoom", 410, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
        (12, 3, "LockerRoom", 590, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
        (13, 3, "SchoolToilets", 770, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
        (14, 3, "Office", 950, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True)
    ]

    for i, data in enumerate(sub_place_data):
        SubLocations[i] = SubPlace(*data)
Then, in my calendar class I update the sub_place_data, or more exactly I give a brand new one depending on the hour, big thanks to anne O'nymous

Python:
    class Calendar(object):
        def __init__(self, Hours, Hour, Days, Day, Months, Month, WeekDays, MonthDays):
            self.Hour = Hour
            self.Hours = Hours
            self.Days = Days
            self.Day = Day
            self.Months = Months
            self.Month = Month
            self.WeekDays = WeekDays
            self.MonthDays = MonthDays
            self.period_index = 0

        @property
        def Output(self):
            return f"{self.WeekDays[self.Day]} {self.Months[self.Month]} {self.Days+1} {str(self.Hours).zfill(2)}:00"

        def AddTime(self, Hours):
            self.Hours += Hours
            if self.Hours > 20:
                self.Hours -= 21
                self.Day += 1
                self.Days += 1
            if self.Day > 6:
                self.Day = 0
            if self.Days == self.MonthDays[self.Month]:
                self.Month += 1
                self.Days = 0
            if self.Month > 11:
                self.Month = 0
            if self.Hours == 22:
                self.Hours += 3
            self.update_sub_place_data()

        def update_sub_place_data(self):
            base_sub_place_data = [
                (0, 0, "My room", 50, 900, "SubLocationIcons/MCBedroomIcon_%s.png", True),
                (1, 0, "Mom room", 230, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
                (2, 0, "Lil sis room", 410, 900, "SubLocationIcons/LittleSisterBedroom_%s.png", True),
                (3, 0, "Toilet", 590, 900, "SubLocationIcons/ToiletIcon_%s.png", True),
                (4, 0, "Livingroom", 770, 900, "SubLocationIcons/livingroomIcon_%s.png", True),
                (5, 0, "Washing Room", 950, 900, "SubLocationIcons/WashingRoomIcon_%s.png", True),
                (6, 0, "Bathroom", 1130, 900, "SubLocationIcons/Bathroom_%s.png", True),
                (7, 0, "Entrance", 1310, 900, "SubLocationIcons/Entrance_%s.png", True),
                (8, 0, "Housefront", 1490, 900, "SubLocationIcons/HousefrontIcon_%s.png", True),
                (9, 3, "Class1", 50, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
                (10, 3, "Principal", 230, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
                (11, 3, "MedicRoom", 410, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
                (12, 3, "LockerRoom", 590, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
                (13, 3, "SchoolToilets", 770, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True),
                (14, 3, "Office", 950, 900, "SubLocationIcons/MOMBedroomIcon_%s.png", True)
            ]

            time_periods = ["", " evening", " night"]
            if 12 <= self.Hours < 18:
                self.period_index = 1
            elif 18 <= self.Hours < 24:
                self.period_index = 2
            else:
                self.period_index = 0

            sub_place_data = [(item[0], item[1], item[2] + time_periods[self.period_index], item[3], item[4], item[5], item[6]) for item in base_sub_place_data]

            for i, data in enumerate(sub_place_data):
                SubLocations[i] = SubPlace(*data)
Now it was almost working,the sublocation button text changed after the desired hour and the location as well. The problem was that the location was changing only if I updated the location myself (meaning that I had to go to another or the same location again for the room to update). Please keep in mind that I am updating the hour with a button that does this

action [Function(calendar.AddTime, 1)]

I tried a lot of things and my main code ended up looking something like this

Python:
    while GameIsRunning:
     
        # Reset variables
        $ BlockToCall, clickType = "", ""
     
        # Check for events in the current location
        $ current_location = str(Location).lower()
        python:
            for event in EVENTS:
                if event.DateCheck(calendar) and event.location.lower() == current_location:
                    BlockToCall = event.Block

        # Call event block if one is found
        if BlockToCall:
            call expression BlockToCall

        # Print calendar output
        python:
            print(calendar.Output)
            print(sub_place_data)

        # Change location image if it exists
        python:
            if calendar.period_index == 1:
                Location_img = str(Location).lower() + " evening"
            elif calendar.period_index == 2:
                Location_img = str(Location).lower() + " night"
            else:
                Location_img = str(Location).lower()
        if renpy.has_image(Location_img, exact=True):
            scene expression Location_img

        # Check for location change and update the location if changed
        $ Location = renpy.call_screen("MainHud", _layer="screens")
        if Location == "map":
            $ Location = renpy.call_screen("MapScreen", _layer="screens")
        elif Location == "ChooseCharacterStats":
            $ Location = renpy.call_screen("StatsScreen", _layer="screens")

        # Update last location
        $ LastLocation = Location
Nothing worked, eventually I realized all I had to do is to make the skip time button run through my main code once pressed, ending up with it doing this:

action [Function(calendar.AddTime, 1), Call("start")]

Now I have to update a lot of code that has doors on the screen going from room to room and a few more things. In conclusion, if anyone is in this situation please realize you are just digging a shithole and you should just rethink your whole code, don't do it like me :)

P.S. thanks for all the help, I was banging my head on this 1 problem for a week
 
Last edited:
  • Haha
Reactions: osanaiko

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,251
15,033
[...] big thanks to anne O'nymous
You're welcome.


Now I have to update a lot of code that has doors on the screen going from room to room and a few more things. In conclusion, if anyone is in this situation please realize you are just digging a shithole and you should just rethink your whole code, don't do it like me :)
That's why I advice to always works on your code first.

You can write your story, starts to do some render/drawing, but all this stay out of the game until you've the code (and a working one of course) for all your game mechanisms. That way, the changes, because there will always be changes, even for me, will stay limited to the strict minimum.