Welcome to
Mod The Sims
Online: 1075
News:
Have an account? Sign in:
pass:
If you don't have an account, why not sign up now? It's free!
Other sites: SimsWiki
Reply  Replies: 13 (Who?), Viewed: 2363 times.
Search this Thread
Old 6th Feb 2020, 10:01 PM DefaultThe cave of the code snippets! #1
Lyralei
Original Poster

Virtual gardener



Join Date: Dec 2013
Posts: 3,093
Thanks: 3434 in 21 Posts
24 Achievements


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
Last edited by Lyralei : 13th Apr 2020 at 3:45 PM.
Old 29th Dec 2019, 8:19 PM #2
zoe22
Trainee Moderator

Join Date: Aug 2014
Posts: 321
Thanks: 987 in 7 Posts
12 Achievements


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:
Old 8th Feb 2020, 10:12 PM #3
Battery
Field Researcher

Join Date: Dec 2008
Posts: 385
Thanks: 825 in 5 Posts
8 Achievements


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
Last edited by Battery : 8th Mar 2020 at 11:10 PM.
Old 16th Feb 2020, 5:22 PM #4
Battery
Field Researcher

Join Date: Dec 2008
Posts: 385
Thanks: 825 in 5 Posts
8 Achievements


Enable Extension Methods in SharpDevelop
Old 5th Mar 2020, 11:45 AM #5
Lyralei
Original Poster

Virtual gardener



Join Date: Dec 2013
Posts: 3,093
Thanks: 3434 in 21 Posts
24 Achievements


Standard Statemachine setup, which you can use in run():

Old 12th Apr 2020, 2:18 PM #6
Lyralei
Original Poster

Virtual gardener



Join Date: Dec 2013
Posts: 3,093
Thanks: 3434 in 21 Posts
24 Achievements


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:


Last edited by Lyralei : 18th Apr 2020 at 12:44 PM.
Old 3rd May 2020, 2:13 PM #7
Lyralei
Original Poster

Virtual gardener



Join Date: Dec 2013
Posts: 3,093
Thanks: 3434 in 21 Posts
24 Achievements


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.
Old 3rd May 2020, 7:05 PM #8
gamefreak130
Field Researcher

Join Date: Apr 2013
Posts: 268
Thanks: 2643 in 18 Posts
17 Achievements


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
Last edited by gamefreak130 : 3rd May 2020 at 7:17 PM.
Old 17th May 2020, 4:58 PM #9
zoe22
Trainee Moderator

Join Date: Aug 2014
Posts: 321
Thanks: 987 in 7 Posts
12 Achievements


Greyed out message for when you cannot do an interaction for some reason (in Test function of InteractionDefinition):

Last edited by zoe22 : 20th Oct 2020 at 9:05 PM.
Old 7th Jul 2020, 6:44 AM #10
gamefreak130
Field Researcher

Join Date: Apr 2013
Posts: 268
Thanks: 2643 in 18 Posts
17 Achievements


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
Old 18th Aug 2020, 10:57 AM #11
Lyralei
Original Poster

Virtual gardener



Join Date: Dec 2013
Posts: 3,093
Thanks: 3434 in 21 Posts
24 Achievements


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
Old 11th Oct 2020, 10:27 PM #12
gamefreak130
Field Researcher

Join Date: Apr 2013
Posts: 268
Thanks: 2643 in 18 Posts
17 Achievements


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
Last edited by gamefreak130 : 12th Oct 2020 at 4:02 AM.
Old 13th Dec 2020, 3:05 PM #13
zoe22
Trainee Moderator

Join Date: Aug 2014
Posts: 321
Thanks: 987 in 7 Posts
12 Achievements


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:
Old 10th Jan 2021, 4:29 PM #14
Lyralei
Original Poster

Virtual gardener



Join Date: Dec 2013
Posts: 3,093
Thanks: 3434 in 21 Posts
24 Achievements


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
Reply


Section jump:


Powered by MariaDB Some icons by http://dryicons.com.