Replies: 1 (Who?), Viewed: 271 times.
Test Subject
Original Poster
#1 Old 18th Jan 2021 at 1:28 PM
Default Script mod to run in interval
Hi all,

I'm very new in script modding for Sims4 and I am doing some experiments for now. Unfortunately, I couldn't find any good documentation from EA regarding this.

I'm writing a very simple python script to change how the game speed works. In the script, I'd like to run a function in every 3 real-time seconds (3 can be anything). But I couldn't find a way to do that so far.
Any ideas how to do this?

thanks,
Advertisement
Test Subject
#2 Old 18th Jan 2021 at 7:08 PM Last edited by lot51 : 18th Jan 2021 at 7:21 PM.
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)
Back to top