So I had a go at making this just now and it seems to work. I've added comments in the code but I'll do more explanation further down.
Code: Select all
# check all tickers on game load
label after_load:
$ check_all_tickers()
return
init python:
config.layers.append("ticker")
from datetime import datetime, timedelta
# ticker class
class ticker:
def __init__(self, start_tick, minutes_per_tick, function):
self.last_tick = start_tick
self.minutes_per_tick = minutes_per_tick
self.function = function
def check_tick(self):
minutes_lapsed = (datetime.now() - self.last_tick).total_seconds() / 60 # calculate minutes since last tick
loop, remainder = divmod(minutes_lapsed, self.minutes_per_tick) # work out how many ticks since last tick and remainder
self.last_tick += timedelta(minutes=loop * self.minutes_per_tick) # update last tick
while loop > 0:
self.function()
loop -= 1
# function to check all tickers in ticker_list
def check_all_tickers():
global ticker_list
for ticker in ticker_list:
ticker.check_tick()
# your functions to update stats
def func_hunger():
global hunger
hunger -= 1
if hunger < 0:
hunger = 0
def func_happiness():
global happiness
happiness -= 1
if happiness < 0:
happiness = 0
# game start time - set it as a variable to synchronise all tickers
default start_tick = datetime.now()
# your stats
default hunger = 10
default happiness = 10
# create ticker objects
default ticker_hunger = ticker(start_tick, 1, func_hunger)
default ticker_happiness = ticker(start_tick, 2, func_happiness)
# a list containing all your tickers
default ticker_list = [ticker_hunger, ticker_happiness]
# just a screen to show you the stats
screen stats:
frame:
xalign 0.0 yalign 0.0
vbox:
text "Hunger: [hunger]"
text "Happiness: [happiness]"
# the invisible ticker screen - updates every 1 second
screen ticker_screen:
timer 1.0 action Function(check_all_tickers) repeat True
label start:
show screen ticker_screen(_layer="ticker")
show screen stats
while True:
"Hi!"
Since you said you're not familiar with programming, I've hopefully created something that's easy enough to use, using object orientation.
Picture there being little ticker people in this code, they're ticker objects (their properties is inside the class ticker). Each one is responsible for a different stats - so in this example I've created one for hunger, one for happiness. Each ticker takes 3 variables when you create it:
- the start_tick: which is the start time of the game, just use the start_tick variable for this
- minutes_per_tick: how many minutes should pass before the ticker executes changes to your stats
- function: what function should your ticker run when it's due to trigger a tick
The first two should be self explanatory, but the third, you'll need to create a python function to be passed into the ticker. I'll explain later, but essentially whatever function you pass into your ticker when you create it, it'll run that function when it's due a tick.
Now as mentioned in the last post, I'm using an invisible screen to trigger the tickers. The way I've done this is, I've put all the tickers into a list. The invisible screen will then call a function that goes through the entire list of tickers, and ask them to check whether they're due a tick - and if they are, they'll run their tick function. The screen runs once every second, which should be more than suitable for the purpose of this.
Now, if you want to create more tickers, you first need to create the ticker's function. Follow the examples of the existing functions - copy and paste them before renaming the relevant names and variables. Let's say we'll do a new one for the stats boredom:
Code: Select all
def func_boredom():
global boredom
boredom = min(boredom + 1, 100)
I'm using a shorthand here, but basically I'm adding 1 to boredom, then if it's larger than 100, boredom exceeds 100. It works by comparing boredom + 1 and 100, and choosing the smaller (minimum) value. You can use hunger = max(hunger - 1, 0) to achieve the same effect when subtracting, but I kept it with if statements above.
Now that we've got the function, we just need to create the ticker and add it to the ticker_list:
Code: Select all
default ticker_boredom = ticker(start_tick, 10, func_boredom) # note there's no bracket to the function name here
default ticker_list = [ticker_hunger, ticker_happiness, ticker_boredom]
Note that I've added ticker_boredom to the end of the ticker_list here. Also I've put in 10 as the second parameter, so the ticker will only run the func_boredom function every 10 minutes.
Test it out yourself, I've used low values here so it's easy to test it. Try saving the game and then loading it a few minutes later to see the difference.
Note that no persistent variables are needed, because your saved game will automatically keep track of the last tick for every ticker, and since each tick would update the last_tick attribute for the ticker - you don't need to do anything on quit to record anything at all. The only thing I've done is to add an after_load label to run all the tickers again on load - otherwise it'd take 1 second before the tickers are triggered and update your stats.
Let me know if it makes sense.