Hi there! You are currently browsing as a guest. Why not create an account? Then you get less ads, can thank creators, post feedback, keep a list of your favourites, and more!
Lab Assistant
Original Poster
#1 Old 16th May 2022 at 11:17 AM
Default Best practice to inject code that needs services to be initialized
Hi modders!

I'm wondering: what's the best practice inject code if one has to be sure that services have already been initialized?

My usecase: I want to set up an alarm and for this purpose I also need the TimeService.

The only way that worked for me so far is a solution based on this tutorial :

Code:
def run_once(function):
    def wrapper(*args, **kwargs):
        if not wrapper.has_run:
            wrapper.has_run = True
            return function(*args, **kwargs)

    wrapper.has_run = False
    return wrapper


@run_once
def setup_alarm():
    now = services.time_service().sim_now
    # setup alarm stuff


sims4.callback_utils.add_callbacks(sims4.callback_utils.CallbackEvent.PROCESS_EVENTS_FOR_HOUSEHOLD_ENTER, setup_alarm)


What I like about this solution is that it's cleaner in the sense of that it uses a native method for injecting code without me having to inject it manually into some function. And that it works, because setup_alarm only gets called when the services have actually been initialized.
What I don't like is that it somehow get's executed a bunch of times, therefore the run_once hack...

Is there a better/cleaner way to achieve this?
Advertisement
Field Researcher
#2 Old 19th May 2022 at 1:33 AM
Hi, I'm not aware of any scripters who visit these forums so I'd recommend asking in my server Creator Musings, as it's got several who are active and may be able to answer your question: https://discord.gg/qxz5Kn5

Creator Musings is a Sims 4 modder, poser/animator, and CC creator hangout server (though everyone is allowed) with a tutorial/resource directory, help channels, and mod/cc/sims 4 news channels!
My Discord | Twitter | Tumblr | Patreon
Lab Assistant
Original Poster
#3 Old 23rd May 2022 at 8:37 AM
Quote: Originally posted by MizoreYukii
Hi, I'm not aware of any scripters who visit these forums so I'd recommend asking in my server Creator Musings, as it's got several who are active and may be able to answer your question: https://discord.gg/qxz5Kn5


Yes, probably that wasn't the right place...

I have to correct myself though: the solution I tried and mentioned in my first post does not work at all. Scumbumbo clarified here that the PROCESS_EVENTS_FOR_HOUSEHOLD_ENTER and PROCESS_EVENTS_FOR_HOUSEHOLD_EXIT events run everytime a sim enters or leaves the lot, so that was a misunderstanding. And of course this explains why the callbacks get called so often.

The run_once hack makes it run only once, but the problem is that it is only run once per game start, so if you switch households/saves it won't be run again.

I will now use on_loading_screen_animation_finished as an injection point, which I currently assume is what most people do.
Back to top