- Site Map >
- Modding and Creation >
- Sims 4 Creation >
- Modding Discussion >
- Script Reloading
- Site Map >
- Modding and Creation >
- Sims 4 Creation >
- Modding Discussion >
- Script Reloading
Replies: 11 (Who?), Viewed: 14608 times.
#1
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
Posts: 8
Thanks: 769 in 3 Posts
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:
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) dirname = os.path.dirname(os.path.realpath(__file__)) filename = os.path.join(dirname, module) + ".py" output("Reloading {}".format(filename)) r.reload_file(filename) output("Done reloading!")
Advertisement
#2
16th Sep 2014 at 6:08 PM
Posts: 2,671
Thanks: 62739 in 190 Posts
Quote: Originally posted by fetusdip
I'm going to start working on getting auto-reloading up. |
Already very useful as is! Thanks!
#3
16th Sep 2014 at 6:49 PM
Posts: 155
Thanks: 7894 in 135 Posts
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
18th Sep 2014 at 4:25 PM
Posts: 17
Thanks: 246 in 4 Posts
Thank you very much for this! Saves so much time!
One change I made:
I added
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!
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
21st Sep 2014 at 1:41 AM
Last edited by ahahfld86 : 21st Sep 2014 at 2:13 AM.
Posts: 4
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)
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
21st Sep 2014 at 4:57 AM
Posts: 17
Thanks: 246 in 4 Posts
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
21st Sep 2014 at 5:21 AM
Posts: 4
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"
#8
21st Sep 2014 at 5:57 AM
Posts: 2,671
Thanks: 62739 in 190 Posts
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
21st Sep 2014 at 6:35 AM
Posts: 4
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.
#10
5th Jan 2019 at 8:10 AM
Posts: 2,671
Thanks: 62739 in 190 Posts
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!
Finds the file automatically from the module name and has some better error messaging.
Enjoy!
Attached files:
Script Reloader.zip (1.9 KB, 354 downloads) - View custom content | ||
1805 2019-01-03 16:09 MTS_Scumbumbo_script_reloader.ts4script --------- ------- 1805 1 file |
Test Subject
#11
24th Jan 2019 at 9:52 PM
Posts: 10
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))
Test Subject
#12
27th Feb 2021 at 8:02 PM
Posts: 2
Not sure if this thread is still being watched but I'm wondering if it is still valid?
I have followed the following, however my script is never loaded. It doesn't get loaded in game unless it's inside the *.ts4script file. I know things have changed over the years and so I'm just trying to see if there is an updated method? Thanks!
`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 have followed the following, however my script is never loaded. It doesn't get loaded in game unless it's inside the *.ts4script file. I know things have changed over the years and so I'm just trying to see if there is an updated method? Thanks!
`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`
Who Posted
|