cover photo

RESOURCE · 21/7/2024

Introduction to writing script mods for the Sims 4

Here, we'll use PyCharm to create our own script mods for the popular video game franchise, The Sims 4!

Vibha H
Vibha H
OP
Introduction to writing script mods for the Sims 4
This Article is yet to be approved by a Coordinator.

TASK 8, Level 0

Introduction to writing script mods for the Sims 4 using Python

This will be an introductory article outlining the basic concepts and runnings-on behind the popular sandbox game, The Sims 4. The game has been created largely using Visual C++, STL, Python, and an internal framework by the company Maxis (called Gonzo). The core script was written in pure C++, similar to the game Minecraft. Modding the Sims, however, is a much more simple and straightforward task, in comparison to what the core code of the game looks like.

This particular article will use PyCharm (any version should work, assuming that EA keeps its game files updated).

Setup

Download and unzip the PyCharm starter project from the community resources website, in your project folder. Start PyCharm. Click Open and browse to your project folder where you unzipped the starter project.

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:

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.

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.

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 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.

Creating a script mod

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. 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.

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.

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 earlier.

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. Right-click the example mod folder. Click copy. Right-click the "My Script Mods" folder and click paste. Enter the new name for your mod. In the scripts folder of your new mod folder, rename the .py file to a name of your choice. Add or edit the .py files in the new Scripts folder. Right-click the compile.py and run it to compile the scripts and load them in your mods folder.

UVCE,
K. R Circle,
Bengaluru 01