Hi there! You are currently browsing as a guest. Why not create an account? Then you get less ads, can thank creators, post feedback, keep a list of your favourites, and more!
Virtual gardener
staff: administrator
Original Poster
#1 Old 6th Feb 2020 at 9:01 PM Last edited by Lyralei : 6th May 2023 at 11:55 AM. Reason: Updated TOC
Default The Code Snippet Jar!
The sims 3, it's a bit of a confusing... thing when you first start making a script mod for it. What the hell is the difference between "Sim" and "SimDescription"? Or "What was the code again for a simple notification? *scrolls massively*". That's basically how the idea was created between Zoeoe and me. So most of the credits go to her too, for the creation of this thread!
Well, this thread will have covered with simple snippets as to what and when to use something, so both advanced, as well as beginners, can enjoy from it! Or even if you have any questions if EA's code simply allows something or you need an extra push into that coding error!

I've got some good ones to include! 
Make sure to list yours in such a way as shown below. Just so that everything is more organised and easier to find. If you've got an alternative way or even an easier way of doing a particular function, then it's totally fine to share this too, but preferably label these as "Alternative:" or "My way:" Or something like that

Make sure however, not to copy paste your entire script or class. A simple function is fine though!

What this thread isn't for:
This is not a thread about how to fix your code or errors. If there's any errors you encounter and you need help, make sure to create a thread to ask that particular question. Your reply will get deleted/moved as a new thread. Of course, exceptions are for if any snippets happen to have a spelling mistake and it needs to be pointed out 

This thread is also not about asking questions and your reply will get deleted if so.




Table Of Contents
To have a clear idea of what's been posted:

Advertisement
Senior Moderator
staff: senior moderator
#2 Old 29th Dec 2019 at 7:19 PM
Set game speed:


Adding an alarm to an object:


Remove an alarm on an object:



Check if sim is in active household:


Modify sim's household funds:


Add to sim's household bills:


Destroy an object in the game:
Space Pony
#3 Old 8th Feb 2020 at 9:12 PM Last edited by Battery : 8th Mar 2020 at 10:10 PM.
If you need to need to get a translation Key directly from the game (for Resourceallergics like myself) you can use this


If you need to Run an Action so that it doesnt block the main thread you can use this

thats all im willing to share for now (thats actually more than i originally wanted to share), since i still need to sell my Script Utility Mod where these snippets are from, to some unsuspecting Modders.. and i did post a whole class. lets see if Lyralei notices
Space Pony
#4 Old 16th Feb 2020 at 4:22 PM
Enable Extension Methods in SharpDevelop
Virtual gardener
staff: administrator
Original Poster
#5 Old 5th Mar 2020 at 10:45 AM
Standard Statemachine setup, which you can use in run():

Virtual gardener
staff: administrator
Original Poster
#6 Old 12th Apr 2020 at 1:18 PM Last edited by Lyralei : 18th Apr 2020 at 11:44 AM.
Make item stackable in inventory, this is also EA's default way of doing this:


Making the object an inventory-enabled (aka, being able to be stored in inventories)



Available types you can use for this:
  • Sim
  • SharedFamilyInventory
  • Your own Custom object that has an inventory (Make sure this is the first class of your script, so not the interaction classes)
  • Tomb chests, EA's inventory objects (Even the InventionBench if you like! )
Making object draggable from a code perspective:


Virtual gardener
staff: administrator
Original Poster
#7 Old 3rd May 2020 at 1:13 PM
A snippet that makes it easier to figure out how long it took to load a function, loop, method, or anything alike. (Credits to Tashiketh as well).


Usually, you'd use the StopWatch class for it. Unfortunately, EA took that out of their own System.Diagnostics version :/. So this is just a simple workaround! If you want to check multiple, then just do exactly the same repeatedly.
Space Pony
#8 Old 3rd May 2020 at 6:05 PM Last edited by Lyralei : 28th May 2022 at 12:24 PM.
Quote: Originally posted by Lyralei
A snippet that makes it easier to figure out how long it took to load a function, loop, method, or anything alike. (Credits to Tashiketh as well).


Usually, you'd use the StopWatch class for it. Unfortunately, EA took that out of their own System.Diagnostics version :/. So this is just a simple workaround! If you want to check multiple, then just do exactly the same repeatedly.


StopWatch was taken out of System.Diagnostics because EA implemented their own StopWatch class in SimIFace.

Basic functionality is pretty much the same as the System StopWatch. Here's an example:


"The Internet is the first thing that humanity has built that humanity doesn't understand, the largest experiment in anarchy that we have ever had." - Eric Schmidt

If you enjoy the mods I put out, consider supporting me on patreon: www.patreon.com/Gamefreak130
Senior Moderator
staff: senior moderator
#9 Old 17th May 2020 at 3:58 PM Last edited by zoe22 : 20th Oct 2020 at 8:05 PM.
Greyed out message for when you cannot do an interaction for some reason (in Test function of InteractionDefinition):

Space Pony
#10 Old 7th Jul 2020 at 5:44 AM Last edited by Lyralei : 28th May 2022 at 12:25 PM. Reason: added spoilers around the codeblock
This is a cleaned-up repost from another discussion thread.

All interactions in the game have a priority level assigned to them. When the game adds an interaction to a Sim's queue, all lower-priority interactions already in the queue are automatically canceled. Most interactions share the same default priority, but certain interactions (mainly reactions to burglars, mummies, simbots, etc.) are labeled by the game as high priority, meaning they will rather annoyingly cancel all user-directed interactions in the queue when they are assigned. If you want to work around this for a custom interaction (or make an interaction with the same functionality), you'll need to make it high priority by adding the following to your interaction's class:



There are priority levels higher than high, but they shouldn't be used unless you know what you're doing.

As an aside, if at any point in an interaction's Run() method you want it to become uncancellable (i.e. the user can't click to remove it from the queue), you can use the following property of InteractionInstance:

Code:
CancellableByPlayer = false;

"The Internet is the first thing that humanity has built that humanity doesn't understand, the largest experiment in anarchy that we have ever had." - Eric Schmidt

If you enjoy the mods I put out, consider supporting me on patreon: www.patreon.com/Gamefreak130
Virtual gardener
staff: administrator
Original Poster
#11 Old 18th Aug 2020 at 9:57 AM Last edited by Lyralei : 28th May 2022 at 12:25 PM. Reason: added spoiler around codeblock
Whenever your function happens to be somewhat heavy on the game, this method can help (Credits to gamefreak130 as well)



What it does, is it fires the function on a different thread whenever it can, meaning that there's less pressure onto the game for running said function. This function is an asynchronous function, as it waits till the game is ready to fire it on a different thread.  This is defined with using OneShotFunctionTask. Function, of course, just defines which function it should look at.

Simulator.AddObject(), just adds this function for the simulator's 'todo list' so to speak.  (The simulator is the system that also has sims do stuff automatically, etc)

There is a way to parse any needed parameters for the function, but that I've never gotten to work beside a whole workaround. So I'd recommend keeping your function within the interaction bit you're using this in. So under the Run() function if you will
Space Pony
#12 Old 11th Oct 2020 at 9:27 PM Last edited by gamefreak130 : 12th Oct 2020 at 3:02 AM.
This is by no means a "snippet", but I just over-engineered a thing that I wanted to post here.

PersistableStatic fields are very useful for pure script mods, since they can effectively serve as global variables whose values can be tied to a save game and automatically saved/loaded by the game. However, multiple worlds in a single save will maintain separate copies of these fields, and their current value will automatically become the default starting values in newly-exposed worlds. When you travel to China, attend university, start a new game after quitting to main menu, etc., the value of PersistableStatics at the time of world loading will be carried over, but any further changes in one world will not affect the values in another. This is useful in some situations (e.g. keeping track of the number of Sims in a world), but not in others (e.g. keeping track of how many sim-days have been simulated save-wide).

To work around this, I created a generic static class (an obscure feature of C#, but a helpful one in this case) that can "ferry" any PersistableStatic data declared in a class or struct across worlds when traveling, effectively allowing an entire save to share a single copy.








"The Internet is the first thing that humanity has built that humanity doesn't understand, the largest experiment in anarchy that we have ever had." - Eric Schmidt

If you enjoy the mods I put out, consider supporting me on patreon: www.patreon.com/Gamefreak130
Senior Moderator
staff: senior moderator
#13 Old 13th Dec 2020 at 2:05 PM
Replacing an existing interaction - Should go in OnPreLoad() NOT OnWorldLoadFinished()(Thanks to Battery for this one ):


Count number of objects on a lot:


Get List of certain type of object in an Inventory:


Route to an object's general area:


Create an object:
Virtual gardener
staff: administrator
Original Poster
#14 Old 10th Jan 2021 at 3:29 PM
Not a code snippet, since it's a pretty big code snippet by itself, so here's the link: https://www.modthesims.info/showthr...375#post5703375

This code snippet shows how to change the name of an object to rename the object's name. The name component is basically the functionality how we can name a sim's car whenever they're best friends with their car
Virtual gardener
staff: administrator
Original Poster
#15 Old 27th Feb 2021 at 12:57 PM Last edited by Lyralei : 28th May 2022 at 11:41 AM. Reason: Moved notification snippet here.
Try and catch method to figure out why your jazz script doesn't work
If your jazz script is not working as expected, you can use EA's debugging exceptions to catch it



Solo animations for testing
If you want to play just an animation without wanting to make a jazz script, or you want to check if your CLIP animation works before making a jazz script, you can also use:




NOTIFICATIONS:
Test Subject
#16 Old 12th Mar 2021 at 10:07 PM Last edited by Lyralei : 28th May 2022 at 12:06 PM. Reason: Added spoilers around the code blocks
From The Depths of The Sim State Mines...
Hi so as a new script modder and working on my Sim State mod for a while I decided (after many...MANY prods :P) to post about my findings that might be useful in a snippet type format. Hopefully I don't cover what has already been said I'm trying to do unique ones .

Query Objects Of a Certain Type
-Finds objects of that type either in general or a specific lot. - Can be GameObject, Sim, or a specific object within the world. Add a 'LOT' inbetween the () to narrow the search to particular lot.


Get Interaction Type
-Find whether an object has a specific interaction type, so for example if you created an interaction class called 'StartBusiness' (off the top of my head :P) you would do the following:


Get Object By Unique ID
-All objects in a world are unique so if you want to find out information about a set object you can do the following:


Find An 'Actors'/Sims Traits
-Check the basic pure script tutorial to find out the difference between Actors & Targets (I.E Sims & their Interaction Objects (another sim, object etc)).
-The trait names will list if you just press the . and then you can choose from a list of all the available traits in the game!


Household IDs
-You can check for the unique household ID of an active sim, or actor which you can query if storing data about that 'household':


Playing A Default EA Sound Effect
-You can find the strings by searching through ILSPY (and perhaps another way but I'm not sure), but for example below this will play the career promotion sound effect, but is used to play any sound. Keep in mind however most sound playback does checks to see if another sound is playing etc.


Removing An Interaction (By Type)
-Similar to the get type method above, you can also remove an interaction from an object that you have queried within a for/foreach: (Will remove SetForSale Interaction or whatever one you want!).


Persistable Data - Saving Your Stuff With EAs Code
-So if you're making anything more than a one shot kind of mod you're going to want to save data. Logically while the game is open anything you have stored in variables through the code will be saved but you need to explicitly save it along with The Sims 3 code base. Not 100% of exactly how this works but in short you can do the following above a class you wish to preserve:


Nested Pie Menu Options (The Real Estate... kind of ones)
-If you have a lot of interactions then you wont want to clutter up the object you're adding to so you can nest them, see the real estate option on the phone or computer for this example. In short it will add any interaction you add the 'path' too, under that path. Put the code in the interaction class, WITHIN the definition class, which contains the GetInteractionName & Test functions/methods.



Changing An 'InWorld' Objects Price
-At times you may want to adjust the price of a placed ingame item, for example I wanted 'bought' items from my business mod to be set to $0 until restocked, so they were basically worth nothing. Its VERY easy. This example uses a referenced object under an interaction but would work the same with any object queried too as you get access to the same list of methods :


Changing An Objects Color
-A little tempremental but in short the following code will 'tint' your objects a color, I haven't tested but in short it seems to work on a 0-100 basis. 100 being full color. The parameters are (r,g,b,transitiontime). Example:


Adding Moodlets to A Sim/Actor
-Simple really, you can add any moodlet in the game to a Sim with the following, and set its origin and length of time to show!


Find A Household By ID
-At times if you are storing the household ID to reference you may wish to find that household to manipulate various options you can do that with the following code:


Visual Effects!
-Okay so this is early days I don't know EXACTLY how this works but I feel confident that if you just change the name of the visual effect it would play automatically on any target (sim/object etc).


Move A Sim To An Object Radius and Check If In Use
-This one is easy as it says it in the method name lol:


Numbers In A String Dialogue
-At times you want user input but not always just a sting (or text), so you can do the following to ask for numbers ONLY and then convert the users text into an 'int' for using elsewhere:


That's all for now, there are a few other things I could post about but need a break, hope this helps someone! And turns down the volume on requests for me to do it finally haha.

Thanks!

SSD.

Creating Sim State Expansion for The Sims 3. :)
Inventor
#17 Old 3rd Apr 2021 at 4:22 AM Last edited by Lyralei : 28th May 2022 at 12:13 PM. Reason: Added Spoilers around codeblocks
Add an Inventory Interaction

This is when you want an interaction to show up when you click an item in inventory. Obvious to look at, but I had to dig to find an example .



Inventory interactions use RunFromInventory() instead of Run()

To check whether an inventory interaction has already been added to an object, use:


Echo Weaver's Simblr: http://echoweaver.tumblr.com/
A portrait in stubbornness - Playing the same legacy since 2009
Sample a Brave Legacy: http://sims3sample.illation.net
Inventor
#18 Old 12th May 2021 at 2:45 PM Last edited by Lyralei : 28th May 2022 at 12:16 PM. Reason: added spoiler around codeblock
To put up a pick list to let the user select from a group of objects using their name, you can use ObjectListPickerDialog.

Pickers are kind of weird -- you don't have to instantiate them. Just call the static Show() method, and they'll instantiate themselves and pop up to the user. This is the simplest picker subclass I've found, and it's the only one I've needed to use so far. Its Show method takes a parameter of type List<ObjectListPickerInfo>. ObjectListPickerInfo is just a class of two items: a string name and an object.

Here's an example that will pop up a picker asking the user to select a pet breed by its name (e.g. "Dalmation"). The picker returns the BreedOutfit object containing all the info about the breed.


Echo Weaver's Simblr: http://echoweaver.tumblr.com/
A portrait in stubbornness - Playing the same legacy since 2009
Sample a Brave Legacy: http://sims3sample.illation.net
Space Pony
#19 Old 5th Jul 2021 at 11:51 PM
As an alternative to the Ferry system I posted a while ago, I just found out there's a "LoadFromMainMenuOnly" property on the PersistableStatic attribute. When enabled, this means that when traveling to another world, the version of the PersistableStatic data from the starting world will always override the version in the destination, while retaining the default behavior when quitting to and loading from the main menu.



Given the extra memory required by the Ferry to store the data and information about where it goes, I see no reason to use it over this method...which is a shame, since I was rather proud of that code

"The Internet is the first thing that humanity has built that humanity doesn't understand, the largest experiment in anarchy that we have ever had." - Eric Schmidt

If you enjoy the mods I put out, consider supporting me on patreon: www.patreon.com/Gamefreak130
Virtual gardener
staff: administrator
Original Poster
#20 Old 29th Oct 2021 at 11:21 AM
In need of making your errors into an XML file, because it's too long for a notification or a dialogue? Then you can always write it to an XML file this way:

Virtual gardener
staff: administrator
Original Poster
#21 Old 22nd Mar 2022 at 3:42 PM
Because EA made a very confusing mood system... A quick few snippets to help any confusion!

Changing the level of a certain mood (Here, we're making the sim sleepier):


Checking the sim's mood:


Literally set the maximum value of a mood. (Will also freeze the mood!, not recommended to use this but if you got a VERY good usecase for it, here you go)
Virtual gardener
staff: administrator
Original Poster
#22 Old 11th Apr 2022 at 8:09 PM
Creating your own Notifications without having to fiddle around with UI resources. This specific example will handle a button click. (Just like the "Pay your bills" notification)

Virtual gardener
staff: administrator
Original Poster
#23 Old 28th May 2022 at 11:39 AM
Decided to put this here, as many people have been asking this question quite often on discord. Many thanks to Gamefreak for making this table!
ORIGINAL POST: https://modthesims.info/showthread....763#post5660763

Quote: Originally posted by Gamefreak130
Here's a comparison table for reference:

Alarm TypePreserved when owner object is reset?Preserved on saving and quitting?
AlwaysPersistedYesYes
DeleteOnResetNoYes
NeverPersistedNoNo


Owner objects are the GameObjects passed as the last argument to the alarm add methods. Typically they are null for global alarms, in which case DeleteOnReset and AlwaysPersisted are functionally equivalent. Global alarms almost never need to be persisted, though -- just readded on WorldLoadFinished. That way, there's a little bit less to save and load in an already-bloated game.
Field Researcher
#24 Old 26th Jun 2022 at 12:57 PM Last edited by KittyTheSnowcat : 27th Jun 2022 at 3:06 PM.
Setting the quality of ingredients:

Field Researcher
#25 Old 30th Jun 2022 at 6:02 PM
Setting a gameObject to a Preset by Code:

Page 1 of 2
Back to top