Forum Discussion

ZerbuTabek's avatar
ZerbuTabek
Rising Hotshot
11 years ago

@SimGuruModSquad: How do you override a default Python file? [Updated]

The official documentation said you could override default Python files:

You can override Maxis Scripts, however to avoid breaking the game your Scripts should preserve the existing Maxis APIs where possible. To override a Script in a sub-package (a Script in a subdirectory containing an “__init__.pyo” file), your Mod must include all other files in the same directory. This type of Mod may need to be updated when the game is updated so it is recommended that this is avoided where possible.


So, I've been trying to override the buff.pyo file. I did exactly what it said, and put all files in the "buffs" folder in with my mod.

However, the game still won't acknowledge any changes I make. It appears in the "Script Mods" list, but nothing happens.

To see if the game was acknowledging the scripts at all or if it was just a problem with my changes, I tried intentionally breaking the code to the point it would normally cause the game to crash or glitch severely. It still didn't do anything, it just ran normally as if the script wasn't there.

UPDATE:

I tried creating a subdirectory within the .zip file, and that got me somewhere. It caused the game to crash upon trying to load a lot. I tried it with unedited copies of recompiled PYO files, but it still crashed. At least it's a step further than the game ignoring them.

UPDATE 2:

I tried it with just the default PYO files, not recompiled or anything, and got no crash. I think I can safely assume it's an issue with the decompiler, unless there's special steps that need to be taken to compile an override (but I'm not going to get my hopes up on that :().

7 Replies

Replies have been turned off for this discussion
  • SimGuruEugi's avatar
    SimGuruEugi
    Icon for EA Staff (Retired) rankEA Staff (Retired)
    11 years ago
    How exactly does your modded script differ? Maybe it's just an issue with the code.
  • The crash occurred even when I just decompiled a file then immediately recompiled it, so I suspect that the decompiler isn't decompiling the existing Python code correctly. It doesn't look like it will be fixable unless the decompiler gets updated. :(
  • SimGuruEugi's avatar
    SimGuruEugi
    Icon for EA Staff (Retired) rankEA Staff (Retired)
    11 years ago
    "Zerbu;12915474" wrote:
    The crash occurred even when I just decompiled a file then immediately recompiled it, so I suspect that the decompiler isn't decompiling the existing Python code correctly. It doesn't look like it will be fixable unless the decompiler gets updated. :(


    I know I have seen some modded code where 'if' statements were being converted to infinite 'while' loops. buff.py is a pretty beefy file, but maybe you can double check that on some smaller module?
  • I looked through buff.py for "while" loops, and there was indeed some that looked like they should be if loops. I'll try changing those, and see if it works. The problem makes a lot of sense. Since lastException.txt didn't mention any error, it's easily possible that the crash was actually an endless loop repeating itself over and over.

    I may as well go straight to updating the file. If it doesn't work, I'll test with a smaller file.
  • Instead of overriding an entire pyo file, you can override individual classes or functions.

    I can't really post sample code from another project - but I do recommend checking out the Reduce Bills script mod on ModTheSims - two files, injector.pyo (which seems to be a decent base for doing this) and the actual mod itself.

    If I'm reading the decompiled code correctly, the mod is a copy and paste of Maxis code with * 0.1 at the end and it simply replaces the function Bills.get_bill_amount.

    I couldn't help but notice however that instead of "function ( self )", it's function (original,self). If you had access to the 'original' object, you don't need to paste the Maxis code. Pretty sure that Bill Reduction mod can actually be done in 4 lines - something like:


    @
    def political_joke_function_name_but_it_made_me_laugh( original, self)
    bills = original.get_bill_amount()
    return bills*0.1


    Using that method, assuming it works (as I said, I've been lazy), would mean multiple mods could modify the same class and/or even the same function plus Maxis updates would still be applied. Cruicial if you were working with something like sim_info or sim.py!

    (this post is intended as food for thought - taken from observations)
  • Wow, I didn't know that SpidersWeb. That's amazing! I'll try it next time I'm script modding. :mrgreen:

    I need to ask though, what's in place of "@"? Making script mods for The Sims 4 is the first experience I have with Python, so I'm not used to the terminology.
  • Sadly I couldn't remember off the top of my head, I was at work, and now I'm at work again lol
    The missing line calls the injector, and tells it what function to replace in the system.

    Part of my problem here is I have it decompiled, have read it, and want to share it and encourage it's use. But I haven't been in contact with the author, or found a decompiled 'free to use' version - so it'd be a bit bad form to post it up. The best I can say is to decompile that mod (use the unpy3 script - not a generic Python decompiler) and see how they did it/try to learn from it. Alternatively you could contact the creator.

    I'd love to be able to post up a working example, but just haven't had enough time to focus on this to write my own, but thought it'd be worthwhile posting my thoughts/what I did know at least :)


    Nevermind, I found the .py version. Check out the author's ('scripthoge') mods at http://modthesims.info/download.php?t=534028

    Included are uncompiled .py files which include his injector and an example mod showing usage as well, I'll paste a little here to show you how it works:


    import sims.sim_info
    import injector

    @injector.inject_to(sims.sim_info.SimInfo, "_set_fit_fat")
    def my_set_fit_fat(original, self):
    # backup fat
    // mod does stuff here

    # call original
    original(self) <--- NB, it's calling the original function we are replacing here, no need to overwrite Maxis code

    # rollback fat
    // mod makes final changes here



    His approach is what I'd recommend. If he had overwritten sim_info.py (or in your case buff.py) then any other mod wanting to do the same, or any additions/adjustments from Maxis would conflict. This method just edits the little bit you need to - and you could have multiple mods working on the same object.

About The Sims 4 Mods & Custom Content

Find expert tips, troubleshooting help, tutorials for mods and custom content, and The Sims 4 patch files in our forum.15,589 PostsLatest Activity: 30 minutes ago