Post by andrew on Oct 28, 2018 22:13:20 GMT -5
Introduction:
This tutorial will show you how to install the required tools, extract EA python scripts, edit custom python scripts, and compile them to share as a mod. You will be using a free program called PyCharm to edit the python files. Please note, this tutorial assumes some familiarity with python and is not meant as a substitute for lessons/tutorials in python coding. It will show the Sims 4-specific steps of making a script mod. This tutorial requires your game to at minimum be patched with the 11/13/2018 game patch.
There is a Russian translation of this tutorial by Lamare available HERE.
You will not need Sims 4 Studio at all to complete this tutorial.
The programs used are also available on Mac OS, but I have never tried them on a Mac, so there will be differences.
What you will need:
Getting Started:
1. Install Python 3.7 from the link above.
2. Install PyCharm from the link above. The Community edition is 100% free. Note: You no longer need to use an old version of PyCharm since The Sims 4 now uses a modern version of Python.
3. Download and unzip the PyCharm starter project in your project folder.
4. Start PyCharm
5. Click Open and browse to your project folder where you unzipped the starter project.
6. When you first open the starter project, it will have the settings.py folder opened for you. Here, you will need to set your creator name, the location of your game, and the location of your mods folder. It will already be set to the default location for your game and mods folder, so set your creator name as shown below. Then click Configure Python interpreter in the upper right corner.
Click the small gear in the right corner. A box will pop up - choose Add Local:
Select System Interpreter on the left. Make sure that the Interpreter on the right is in your Python37 folder. If you have other versions of Python installed, it may default to another one. Click OK.
7. After setting your creator name and adding your Python interpreter, expand the project folder on the left. You will see some folders and a few python files in the main folder.
- EA - this is where the extracted game scripts will be stored. When you first open this project, the sub-folders will be empty.
- My Script Mods - this is where you can keep the python source files for the mods that you will make.
- Utilities - this folder contains python scripts included in the starter project to help automate things for you. You will not need to edit these files at all.
- decompile_all.py is a script file that will extract the in-game python scripts and decompile them so that you can use them.
- settings.py - is the settings file that you edited in step 6. If you ever need to change your creator name or game path, edit it here.
8. Before trying to create a script of your own, it is best to first extract the ones from the game. Not only can you look to them for examples, but also having them extracted will allow PyCharm to assist you while creating your own scripts. To extract the game scripts, right-click the decompile_all.py and select "Run 'decompile_all'". You will see a log at the bottom of the screen written for each decompiled file. Wait until it has finished before continuing to the next step.
9. You will now see that the EA sub-folders are full of more sub-folders with the extracted game scripts. Your PyCharm project is now fully set up to make a script mod. PyCharm has many features that will assist you with writing your scripts and detect errors in them, but to do so, it will need to have the game's script files available. The startup project provided configures PyCharm to look in the EA sub-folder and assist you when working with EA scripts.
Note: after each patch, you should run the decompile_all script again to keep your EA scripts up to date with the current patch.
Your first script mod:
1. Expand the subfolders of the "My Script Mods" folder. In this folder, will be a subfolder for each script mod that you make. Right now it will only have one mod that is called "Example Mod". Each mod folder will have a subfolder called "Scripts" that will contain all of your .py files for the mod. Open the example_mod.py file in the Scripts sub-folder of the example mod by double-clicking it.
2. In this script file is an in-game cheat code mod. This type is the simplest python mod that can be made.
import sims4.commands
@sims4.commands.Command('myfirstscript', command_type=sims4.commands.CommandType.Live)
def myfirstscript(_connection=None):
output = sims4.commands.CheatOutput(_connection)
output("This is my first script mod")
- The first line loads the sims4.commands module from EA's game scripts. This step is required to use the sims4.commands.Command in the next line.
- The next line defines the cheat code command. The first item passed to the cheat code is the name of the cheat code that will be typed into the in-game cheat console. The second part specifies that this cheat code will be used in Live mode in the game. After the sims4.commands.Command decorator (a python term), is a function that will run when the cheat code is called from the game. The first line of the cheat code function acquires a reference to the cheat UI's output window.
- The last line simply writes some text to the output window. In the game, you should see whatever you write in the script instead of "This is my first script mod".
Change this text in the last line to words of your choice. Click File> Save All or the Ctrl+S keyboard shortcut to save your changes.
3. Now that you have modified the example script, it is time to get it into the game. You should have a file called compile.py in your Example Mod folder. Running this python file will compile all of the .py files in your Scripts sub-folder into a .ts4script and copy it into your mods folder. It will be called [Your Creator Name]_[The folder name of your mod].ts4script. Right-click the compile.py and select "Run 'compile'". You should now have a script file in your mods folder. In my case, it is called andrew_Example Mod.ts4script.
4. Start your game and make sure that script mods are enabled. Create a new game or load a save game. Once in Live mode, press ctrl+shift+c to open the cheat console. Type the word "myfirstscript" (without quotes) and press enter. You should see the text that you entered in step 3.
Creating your own mod:
Now that you have setup your tools, edited a python script, and compiled it for your game, it is time to get started with your own python scripts.
1. Right-click the example mod folder.
2. Click copy.
3. Right-click the "My Script Mods" folder and click paste.
4. Enter the new name for your mod.
5. In the scripts folder of your new mod folder, rename the .py file to a name of your choice.
6. Add or edit the .py files in the new Scripts folder.
7. Right-click the compile.py and run it to compile the scripts and load them in your mods folder.
Further Reading:
Now that you have learned how to get a python script into the game, if you aren't already familiar, you should read up on how to work with python scripts. You may also want to read up on the documentation for PyCharm.
Python documentation
useful post on how to get started with Python
Credits:
The unpyc3 script included in the project folder is from the open source project unpyc37