Loose Leaf Scripting Examples

This section holds loose leaf scripting examples that other creators have written, such as code pieces with no files, etc. You are free to use the codes here to create your mods; just remember to rename to your preference.

If you are looking for the main page to Scripting Examples, look here.

Making a Configuration File for Your Mods (Frankk)
(The following was a transcript from the Discord Server: Creator Musings)

After battling this process for multiple hours, I finally figured out how to make and use a config file in a mod. If you don't know, a config file can be used to store settings and allow users to edit them on their own. Here are my tips if you're looking to do the same: 1. Create a  file in your mod's directory. You can name it whatever you want, but  is probably a good idea. 2. Read about the syntax of the config file here if you aren't already familiar with it: https://docs.python.org/3/library/configparser.html (NOTE: Don't bother reading the configparser documentation. The Sims team wrote their own configparser.py that overrides this one. Just read the syntax of the config file, as it's the same for both) 3. Parse your config file in a script with the following: And there you go! Now you have the config file loaded into your script. If your config file looks like this: You can get the values like this: Just as an additional example to show you what this can actually be used for, here is the config file that I will be including in Language Barriers: If a user wants to make Oasis Springs speak Selvadoradian, then they just need to replace that line with: This can be super helpful for people looking to give users customization options!

Getting Client Cheat Commands to Run in XML (TwelfthDoctor1)
(The following was a transcript from the Discord Server: Creator Musings)

I just found out that inside, there is a output type called. is basically the console you use to type cheats in. Unlike,   is used to execute commands that are not in Simulation such as. I found that in, commands like   and   aren't found in Simulation. It could be in the Core, but I know that some files are not fully decompiled (well, based on one of my scripts when I decompile it from TS4SCRIPT it only got 2 of 4 commands.).

So based on the  code, I wrote up this: Also there needs to be a list with a tuple along with it: To make things easy, we start at the Command. has 2 params: identifier and state. identifier is the string command that is used to check against the enable command inside the list-tuple. (i.e. bb.enablefreebuild contains enablefreebuild). state is to determine if it is enabled or disabled. Now the list-tuple has 3 things: the enable command, disable command and the modifier to tell if it is a client command (basically not in Simulation). Each one will be used to test and be applied accordingly. Example: You want to run the , and based from the list, it is a client_cheat. So the identifier (bb.showhiddenobjects) will be matched against enable_cmd, it will continue doing this until it matches for one or fails, in this case it finds it. So we check if it needs client_cheat based on is_client, if yes we check the state, True for enable, False for disable and then it will execute the command. (Note that non client is still the same just that the mode of execution is different, thats all.)

Actually if you want it SIMPLE, this will suffice: Well just saying that this quick command script here cannot tell you whether the command has worked. So you will need to double check before using it, most likely in XML.

ConfigParser - Auto Making, Appending and Changing Values In-Game (TwelfthDoctor1)
The reader should know some understanding of Python File Handling and ConfigParser functions before attempting this example.

Config files is something commonly used by games and even less common by mods, unless it has certain functions that can be toggled. Originally, my "config file" was the Module Tuning, but with users needing S4S just to change an option, I had to figure out how to write my own config handler for my big mod.

Note: As the basic parts have been covered by Frankk, I will only cover the deeper components.

Auto Writing a Config File
I am not really fond of self creating a config file and shipping that out with the mod, I want it to be auto created so that it makes it easier for me and the users. It is possible to create a file from a script, you will need to know Python's File Handling functions in order to implement it. In the case of a config file, you need the section or header and the options with their values.

As a time saver, it is marginally easier to use lists with a for loop to write each option with their value. Having the write function in each line can be wasteful, confusing and not efficient, especially in the long run.

Appending and Auto Checking
I also had to consider each update, especially if I were to add new options to toggle. Not only that, with updates, people may not have that option available, and I'd rather not ship a config file update. So, config_init_check will run and with sieve through all the options to see if it exists on the list. If it doesn't, it will call append_option which will append the missing option into the config file.

Now my method here does not deal with duplicates and those out of order, because my presumption is that no one would modify the config file in terms of changing the position of each option. But you are always welcome to include that option in your scripts.

Changing an Option In-Game
The reason why you want users to change the config options in game is that it makes it easier for them to understand, you would rather not ask them to edit the config file, they'd be confused if there isn't anything to help them understand what they are changing. By far, this is the biggest hurdle to get through, as instead writing directly to the config file, you NEED to use ConfigParser itself to write (denoted as config.write) to the file as the regular write has no clue on what to change and write. Also, the set function only sets the option, it does not set it directly, you still need to use the write function to change the option value or no changes will take effect.

Now don't ask me why why it had to be done this way, its how its done in the Python Docs.

At the end of all this, you could have a code like this: Well...I cannot fully explain this as ConfigParser is not within my territory of full understanding. But hopefully whoever reads this may understand a little bit more on config files and ConfigParser.

Also Yes I know there is the RawConfigParser, it still depends on what you want to use, but you also note that RawConfigParser uses older and unsafe methods of changing values, it is recommended you use the ConfigParser instead.