How to inject an interaction with a pure script mod by Nona Mena
*** Read more of this thread in http://www.simlogical.com/ContentUploadsRemote/uploads/1596/How_inject_interactio_with_pure_script_mod.pdf ***
Injecting an interaction with a pure script mod is a lot easier than you might think! There are only three parts you absolutely must have to make an interaction injection mod:
- The interaction itself. This looks the same as any other interaction.
- The Instantiator class. You'll need this to load the interaction into the game and apply it to game objects/sims/terrain.
- The Instantiator XML. This is just an XML file that will be part of your package file. If your interaction requires no tuning, your package file could have only two resources: the S3SA with the script and the instantiator XML.
NOTE: This post assumes you are already familiar with scripting modding in the Sims 3. If you have never made a script mod for The Sims 3, please read the pure scripting modding tutorial at MTS.
Part One - The Interaction
Make an interaction. I'm going to assume you know how to make an interaction already. If you don't, go read the Object Modding (aka Adding Interactions) Tutorial. I also highly recommend Cmar's Tutorial: Adding pie menu options to sims.
This post is primarily about the instantiator class.
Part Two - The Instantiator
Now it's time to make your Instantiator class. Most of this is covered in the Pure Scripting Modding Tutorial at MTS, but I'll go over it again.
Download: Example Instantiator Class
Step One: Give your assembly the [assembly: Tunable] attribute.
Before you go further, go to the AssemblyInfo.cs and make sure you add the following (if you haven't already):
using Sims3.SimIFace;
[assembly: Tunable]
It is extremely important that you add these lines to your AssemblyInfo.cs, as without them, you cannot load your script into the game. Actually, you could put the [assembly: Tunable] attribute in your Instantiator cs but you might as well just get into the habit of adding it to your AssemblyInfo. It should look something like this:
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Sims3.SimIFace;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: Tunable]
[assembly: AssemblyTitle("Nona_WhatsMyTemp")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Nona_WhatsMyTemp")]
[assembly: AssemblyCopyright("Copyright © 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
etc.
Step Two: Make an Instantiator.cs (Download example instantiator)
Since I'm writing this, we're doing doing it my way. This is how the skeleton of your Instantiator class will look. Feel free to copy & paste this or just use my example instantiator as a sort of template/guideline. Just don't forget to change the namespace!
using Sims3.Gameplay.Actors;
using Sims3.Gameplay.Autonomy;
using Sims3.Gameplay.EventSystem;
using Sims3.SimIFace;
using System;
namespace NonaMena.WhatsMyTemp.Common
{
public static class Instantiator
{
[Tunable]
internal static bool kInstantiator = false;
static Instantiator()
{
World.OnWorldLoadFinishedEventHandler += new EventHandler(OnWorldLoadFinished);
}
private static void OnWorldLoadFinished(object sender, EventArgs e)
{
}
}
}
The tunable kInstantiator is what will get our foot in the door. Then we will use the OnWorldLoadFinished() callback method to get our interaction injected.
Step Three: Put together the OnWorldLoadFinished() callback method.
Interaction injection is easy. All we need to do is tell the game to add our interaction to whatever objects need it. In the case of my What's My Temp? mod, I am adding the interaction to all sims. Put this in your OnWorldLoadFinished() callback method :
foreach (Sim sim in Sims3.Gameplay.Queries.GetObjects<Sim>())
{
AddInteractions(sim);
}
AddInteractions is the method I will use to apply the interactions. You can change the name to whatever you like. However, it should look like this:
private static void AddInteractions(Sim sim)
{
foreach (InteractionObjectPair pair in sim.Interactions)
{
if (pair.InteractionDefinition.GetType() == CheckMyTemp.Singleton.GetType())
{
return;
}
}
sim.AddInteraction(CheckMyTemp.Singleton);
}
Like this, your instantiator will apply your interaction to all sims in the game when the game is loaded. But what about sims that get added to the game during gameplay, such as newborns or immigrants?
Next: Step Three: Using the EventTracker to apply interactions to newly instantiated sims.