1 people viewing
Quick Reply
Search this Thread
Replies: 10 (Who?), Viewed: 10134 times.
Test Subject
Original Poster
#1 Old 14th Sep 2014 at 3:22 AM Last edited by fetusdip : 14th Sep 2014 at 8:34 AM. Reason: Updated the code to handle errors when loading
Default Script Reloading
I managed to get my scripts to reload without restarting the sims. Add this to your mod in a new file and call it with "reload <modulename>". I'm going to start working on getting auto-reloading up. Also, there seems to be a service defined for reloading scripts (sims4/reload_service.py) for anyone who wants to research how to access/use it since it's not in the services module.

Code:
import sims4.commands
import sims4.reload as r
import os.path

@sims4.commands.Command('reload', command_type=sims4.commands.CommandType.Live)
def reload_maslow(module:str, _connection=None):
    output = sims4.commands.CheatOutput(_connection)
    try:
        dirname = os.path.dirname(os.path.realpath(__file__))
        filename = os.path.join(dirname, module) + ".py"
        output("Reloading {}".format(filename))
        reloaded_module = r.reload_file(filename)
        if reloaded_module is not None:
            output("Done reloading!")
        else:
            output("Error loading module or module does not exist")
    except BaseException as e:
        output("Reload failed: ")
        for v in e.args:
            output(v)


old code:
Advertisement
Deceased
#2 Old 16th Sep 2014 at 6:08 PM
Quote:
Originally Posted by fetusdip
I'm going to start working on getting auto-reloading up.


Already very useful as is! Thanks!
Lab Assistant
#3 Old 16th Sep 2014 at 6:49 PM
Thanks for this. I've been wondering how many times I'd have to restart the game before I throw my keyboard at the monitor. If I had any hair, I'd have had bald patches by now from pulling them out.
Test Subject
#4 Old 18th Sep 2014 at 4:25 PM
Thank you very much for this! Saves so much time!

One change I made:

I added
Code:
 		dirname = dirname.replace("\\ReloadScriptFolder\\", "\\" + module + "\\"

after the original dirname line in your code, and moved the reload script to it's own folder. If you have your scripts properly set up in their appropriate folders, you can use this to reload any file you're running, rather than having a copy of this reload script in every folder.

Thanks!
Test Subject
#5 Old 21st Sep 2014 at 1:41 AM Last edited by ahahfld86 : 21st Sep 2014 at 2:13 AM.
i've got error in this message
this is what i do...
1) paste reload code paste in test.py
2) generate test.py and test.pyo
3) these files zip to test.zip
4) copy to (...)Mods
5) run sims 4
6) input - reload test
--------------------------------
reloading (...)\Mods\test.zip\test.py
Error loading module or module does not exist
why can't recognize test.py?
how to apply this? i need help
(already try to another files (reload.py / test.py) in test.zip. but same errors occur)
Test Subject
#6 Old 21st Sep 2014 at 4:57 AM
Quote:
Originally Posted by ahahfld86
i've got error in this message
this is what i do...
1) paste reload code paste in test.py
2) generate test.py and test.pyo
3) these files zip to test.zip
4) copy to (...)Mods
5) run sims 4
6) input - reload test
--------------------------------
reloading (...)\Mods\test.zip\test.py
Error loading module or module does not exist
why can't recognize test.py?
how to apply this? i need help
(already try to another files (reload.py / test.py) in test.zip. but same errors occur)


I believe this works for the sctipt only, you have to run it not in the zip file.

The format is
Mods/moduleName/Scripts/moduleName.py

I don't think it works compiled and/or zipped, though I haven't tried it personally

Its actually better this way for mod development cause you dont have to compile/zip each time you make a change, you just save and reload and you're good to go.
Test Subject
#7 Old 21st Sep 2014 at 5:21 AM
Quote:
Originally Posted by lizsatoshi
I believe this works for the sctipt only, you have to run it not in the zip file.

The format is
Mods/moduleName/Scripts/moduleName.py

I don't think it works compiled and/or zipped, though I haven't tried it personally

Its actually better this way for mod development cause you dont have to compile/zip each time you make a change, you just save and reload and you're good to go.


thanks for reply.
im already try my script files just run, not compile or make a zip.
in Sims4 console input reload <modulename>
the out message's reloading path is (...)mods\<reloadscript filename>.zip\<modulename>.py
and "Error loading module or module does not exist"
Deceased
#8 Old 21st Sep 2014 at 5:57 AM
Quote:
Originally Posted by ahahfld86
thanks for reply.
im already try my script files just run, not compile or make a zip.
in Sims4 console input reload <modulename>
the out message's reloading path is (...)mods\<reloadscript filename>.zip\<modulename>.py
and "Error loading module or module does not exist"


I had a similar problem last night -- my problem was that the python script I was attempting to reload never got loaded in the first place, apparantly as I reused the function name "usage()" which is already in one of my other scripts. So basically there is probably an error in your script that is causing it to never be initially loaded by the game, so it can't be reloaded. HTH!
Test Subject
#9 Old 21st Sep 2014 at 6:35 AM
Quote:
Originally Posted by scumbumbo
I had a similar problem last night -- my problem was that the python script I was attempting to reload never got loaded in the first place, apparantly as I reused the function name "usage()" which is already in one of my other scripts. So basically there is probably an error in your script that is causing it to never be initially loaded by the game, so it can't be reloaded. HTH!


thanks for reply!! and i've another question.
what i really wonder thing is
how to set reload script and your own modules?
make reload script to zip file and put in Mods folder? and how to set own moudles?
compile and make zip files? or include reload script's zip file? or put any folder just py files?
or script code paste in own module file?
i try all things in reply.. but same err occur.
Lab Assistant
DELETED POST
21st Jun 2015 at 9:01 PM
This message has been deleted by Vinathi.
Deceased
#10 Old 5th Jan 2019 at 8:10 AM
I've written a nice script reloader based on fetusdip's script. Easier to use, will reload modules from any folder, including a Python package subfolder. reload module_name or use 'r' for an alias, e.g. r package.module_name.

Finds the file automatically from the module name and has some better error messaging.

Enjoy!
Download - please read all instructions before downloading any files!
File Type: zip Script Reloader.zip (1.9 KB, 104 downloads) - View custom content
Test Subject
#11 Old 24th Jan 2019 at 9:52 PM
I wanted to include support for additional python files under my script folder. I added the following to my script so that I could call <modulename>.refresh and it would reload every file with a .py extension. I am sure this could be added to your package to quickly reload any dependencies.

Code:
@sims4.commands.Command('mytest.refresh', command_type=sims4.commands.CommandType.Live)
def reload_submodules(_connection=None):
    output = sims4.commands.CheatOutput(_connection)
    output('reloading')
    
    base_dir: str = os.path.dirname(os.path.realpath(__file__))

    for root, dirs, files in os.walk(base_dir):
        for file in files:
            if file.endswith(".py"):
                if os.path.join(root, file) != __file__:
                    output(os.path.join(root, file))
                    sims4.reload.reload_file(os.path.join(root, file)
Back to top