Replies: 14 (Who?), Viewed: 2922 times.
Virtual gardener
staff: administrator
Original Poster
#1 Old 6th Feb 2020 at 10:01 PM Last edited by Lyralei : 13th Apr 2020 at 3:45 PM.
Default The cave of the code snippets!
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 



Snippet time!

NOTIFICATIONS:



All the notification Styles:
kSimTalking
kSystemMessage
kGameMessagePositive
kGameMessageNegative
kDebugAlert
kCustom
kCelebrityUpdate
Advertisement
Staff - Moderator
staff: moderator
#2 Old 29th Dec 2019 at 8: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:
Field Researcher
#3 Old 8th Feb 2020 at 10:12 PM Last edited by Battery : 8th Mar 2020 at 11: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
Field Researcher
#4 Old 16th Feb 2020 at 5:22 PM
Enable Extension Methods in SharpDevelop
Virtual gardener
staff: administrator
Original Poster
#5 Old 5th Mar 2020 at 11:45 AM
Standard Statemachine setup, which you can use in run():

Virtual gardener
staff: administrator
Original Poster
#6 Old 12th Apr 2020 at 2:18 PM Last edited by Lyralei : 18th Apr 2020 at 12:44 PM.
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 2: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.
Field Researcher
#8 Old 3rd May 2020 at 7:05 PM Last edited by gamefreak130 : 3rd May 2020 at 7:17 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:

Code:
using Sims3.SimIFace;
using Sims3.UI;

internal void TimedProcedure()
{
    StopWatch timer = StopWatch.Create(StopWatch.TickStyles.Microseconds).Start();
    // Things
    // TODO More things
    // Optional: Maybe some more things here?
    timer.Stop();
    SimpleMessageDialogue.Show("TimedProcedure() took " + timer.GetElapsedTimeFloat().ToString() + " microseconds to execute.");
    timer.Dispose();
}

"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
Staff - Moderator
staff: moderator
#9 Old 17th May 2020 at 4:58 PM Last edited by zoe22 : 20th Oct 2020 at 9:05 PM.
Greyed out message for when you cannot do an interaction for some reason (in Test function of InteractionDefinition):

Field Researcher
#10 Old 7th Jul 2020 at 6:44 AM
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:

Code:
using Sims3.Gameplay.Interactions;

public override void Init(ref InteractionInstanceParameters parameters)
{
    parameters.Priority = new InteractionPriority(InteractionPriorityLevel.High);
    base.Init(ref parameters);
}


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 10:57 AM
Whenever your function happens to be somewhat heavy on the game, this method can help (Credits to gamefreak130 as well)

Code:
public override bool Run()
{
   // when adding the function, never add the () to the end. This is because it expects a delegate
   Simulator.AddObject(new OneShotFunctionTask(new Function(yourFunctionhere)));
}
public void yourFunctionhere() 
{
   // code you want to fire here.
} 
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
Field Researcher
#12 Old 11th Oct 2020 at 10:27 PM Last edited by gamefreak130 : 12th Oct 2020 at 4: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
Staff - Moderator
staff: moderator
#13 Old 13th Dec 2020 at 3: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 4: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 DELETED POST
Original Poster
5th Feb 2021 at 7:14 PM
This message has been deleted by Lyralei. Reason: Not really necessary
Virtual gardener
staff: administrator
Original Poster
#15 Old 27th Feb 2021 at 1:57 PM
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:

Back to top