#2
18th Jan 2021 at 6:08 PM
Last edited by lot51 : 18th Jan 2021 at
6:21 PM.
Posts: 61
Thanks: 1195 in 25 Posts
7 Achievements
Howdy, I think you're looking for alarms, within
simulation > alarms.py. There is an add_alarm_real_time function that will run your callback function based on real seconds, add_alarm will follow the in-game clock. While looking for alarm info I found an EA class that's similar to what you're trying to do in
simulation > simulate_to_time.py.
I chopped up the SimulateToTime class into something that may work for you, I didn't have a chance to test but let me know if it doesn't work.
Code:
import services
from alarms import add_alarm_real_time, cancel_alarm
from clock import ClockSpeedMode, interval_in_real_seconds
from date_and_time import TimeSpan
class MyCustomAlarm:
def __init__(self):
self._alarm_handle = None
self._time_span = None
# - this method will start the alarm and make sure only one instance of the alarm is running
# - pass the number of seconds to wait before each tick, otherwise 3 seconds is the default
def start(self, seconds:int=3):
if self._alarm_handle != None:
self.cancel()
self._time_span = TimeSpan(interval_in_real_seconds(seconds))
self._alarm_handle = add_alarm_real_time(self, self._time_span, self._tick, repeating=True, use_sleep_time=True, cross_zone=False)
if self._alarm_handle is None:
# alarm failed to start
return False
return True
# this method will stop the alarm
def cancel(self):
if self._alarm_handle is not None:
cancel_alarm(self._alarm_handle)
self._alarm_handle = None
self._time_span = None
# this method will set the clock speed to 3 if it does not equal 3
def _set_clock_speed(self):
clock_service = services.game_clock_service()
if clock_service.clock_speed != ClockSpeedMode.SPEED3:
clock_service.set_clock_speed(ClockSpeedMode.SPEED3)
# this method will run everytime the alarm runs the callback
def _tick(self, alarm_id):
if self._alarm_handle is None:
return True
self._set_clock_speed()
return True
# intialize your alarm class once, then start it
# with number of seconds to wait between each tick
# you can import this variable from another file and start/cancel it there
my_alarm = MyCustomAlarm()
my_alarm.start(3)