Tutorial:Sims 3 Script Mod Extra Sim Data

From SimsWiki
Jump to: navigation, search

This tutorial will explain how to store extra data about sims through a pure scripting mod. In this tutorial we will make a script that logs how many sims each particular sim has met since installing the mod. You will probably have little need for such a mod, but the purpose is to demonstrate how the number of sims will be stored.

Contents

Getting Started

First, we need to start our project. This process is explained in the pure scripting modding tutorial. Add [assembly: PersistableStatic] to your AssemblyInfo.cs property. This is required for the data to be able to survive save and reload. Start off with a basic instantiator that includes an OnWorldLoadFinished event handler, like the following code:

using System;
using System.Collections.Generic;
using System.Text;
using Sims3.SimIFace;

namespace Zerbu.SimMeetLog
{
    public class Instantiator
    {
        [Tunable]
        protected static bool kInstantiator = false;

        static Instantiator()
        {
            World.OnWorldLoadFinishedEventHandler += new EventHandler(OnWorldLoadFinished);
        }

        private static void OnWorldLoadFinished(object sender, EventArgs e)
        {
            // the code will be inserted here
        }
    }
}

The Dictionary

Add the following code to your instantiator to define a dictionary that will store any additional information about a sim. You can add it to a different class or create a custom class especially for sim data if you want, but for this tutorial we will just use the instantiator.

[PersistableStatic]
public static Dictionary<ulong, int> NumberOfSimsMet = new Dictionary<ulong, int>();

The ulong is the ID of the sim the data is being stored for and should be kept the way it is. The int, however, can be changed to the type of data you want to store, so for example, if you want to store text instead of a number you can use string instead of int.

Applying Data to Sims

Add the following to your Instantiator class:

[PersistableStatic]
protected static bool mSimDataInstantiated = false;

It is important that the value remains as false because this will be changed to true in the script to tell the system that the system has already instantiated in the game. Next, add this to your OnWorldLoadFinished method:

if (!mSimDataInstantiated)
{
  foreach (Sim sim in Sims3.Gameplay.Queries.GetObjects<Sim>())
  {
    NumberOfSimsMet.Add(sim.SimDescription.SimDescriptionId, 0);
  }
  mSimDataInstantiated = true;
}

This will set the value of "NumberOfSimsMet" to 0 for all existing sims whenever a new game is started or a game that was made before the mod was installed is loaded. However, that is not enough, because we want the data to also be instantiated when a new sim is inserted or born into the neighbourhood. We can do that with an event call to kSimInstantiated:

EventTracker.AddListener(EventTypeId.kSimInstantiated, new ProcessEventDelegate(OnSimInstantiated));
private static ListenerAction OnSimInstantiated(Event e)
{
    foreach (Sim sim in Sims3.Gameplay.Queries.GetObjects<Sim>())
    {
        NumberOfSimsMet.Add(sim.SimDescription.SimDescriptionId, 0);
    }
}

The code should look something like this so far:

using System;
using System.Collections.Generic;
using System.Text;
using Sims3.SimIFace;

namespace Zerbu.SimMeetLog
{
    public class Instantiator
    {
        [Tunable]
        protected static bool kInstantiator = false;

        [PersistableStatic]
        protected static bool mSimDataInstantiated = false;

        [PersistableStatic]
        public static Dictionary<ulong, int> NumberOfSimsMet = new Dictionary<ulong, int>();

        static Instantiator()
        {
            World.OnWorldLoadFinishedEventHandler += new EventHandler(OnWorldLoadFinished);
        }

        private static void OnWorldLoadFinished(object sender, EventArgs e)
        {
            if (!mSimDataInstantiated)
            {
                foreach (Sim sim in Sims3.Gameplay.Queries.GetObjects<Sim>())
                {
                    NumberOfSimsMet.Add(sim.SimDescription.SimDescriptionId, 0);
                }
                mSimDataInstantiated = true;
            }
            EventTracker.AddListener(EventTypeId.kSimInstantiated, new ProcessEventDelegate(OnSimInstantiated));
        }
        private static ListenerAction OnSimInstantiated(Event e)
        {
            NumberOfSimsMet.Add(sim.SimDescription.SimDescriptionId, 0);
        }
    }
}

Displaying and Changing the Data

You can access the value of NumberOfSimsMet for a sim using the code:

NumberOfSimsMet[(sim ID)]

So next we'll instruct the script to increment the value when a sim meets another sim using this code, and we'll also make it display a notification of the number of sims met:

EventTracker.AddListener(EventTypeId.kMetSim, new ProcessEventDelegate(OnMetSim));
private static ListenerAction OnMetSim(Event e)
{
    NumberOfSimsMet[e.Actor.SimDescription.SimDescriptionId]++;
    StyledNotification.Format Format = new StyledNotification.Format("Number of sims met since the mod was installed: "+NumberOfSimsMet[e.Actor.SimDescription.SimDescriptionId], ObjectGuid.InvalidObjectGuid, e.Actor.ObjectId, StyledNotification.NotificationStyle.kGameMessagePositive);
    StyledNotification.Show(Format, "tns_icon_bulb");
}

And the final code should look like this:

using System;
using System.Collections.Generic;
using System.Text;
using Sims3.SimIFace;

namespace Zerbu.SimMeetLog
{
    public class Instantiator
    {
        [Tunable]
        protected static bool kInstantiator = false;

        [PersistableStatic]
        protected static bool mSimDataInstantiated = false;

        [PersistableStatic]
        public static Dictionary<ulong, int> NumberOfSimsMet = new Dictionary<ulong, int>();

        static Instantiator()
        {
            World.OnWorldLoadFinishedEventHandler += new EventHandler(OnWorldLoadFinished);
        }

        private static void OnWorldLoadFinished(object sender, EventArgs e)
        {
            if (!mSimDataInstantiated)
            {
                foreach (Sim sim in Sims3.Gameplay.Queries.GetObjects<Sim>())
                {
                    NumberOfSimsMet.Add(sim.SimDescription.SimDescriptionId, 0);
                }
                mSimDataInstantiated = true;
            }
            EventTracker.AddListener(EventTypeId.kSimInstantiated, new ProcessEventDelegate(OnSimInstantiated));
            EventTracker.AddListener(EventTypeId.kMetSim, new ProcessEventDelegate(OnMetSim));
        }
        private static ListenerAction OnSimInstantiated(Event e)
        {
            NumberOfSimsMet.Add(sim.SimDescription.SimDescriptionId, 0);
        }
        private static ListenerAction OnMetSim(Event e)
        {
            NumberOfSimsMet[e.Actor.SimDescription.SimDescriptionId]++;
            StyledNotification.Format Format = new StyledNotification.Format("Number of sims met since the mod was installed: "+NumberOfSimsMet[e.Actor.SimDescription.SimDescriptionId], ObjectGuid.InvalidObjectGuid, e.Actor.ObjectId, StyledNotification.NotificationStyle.kGameMessagePositive);
            StyledNotification.Show(Format, "tns_icon_bulb");
        }
    }
}

You now know how to assign extra data to sims. This script is just an example, so hopefully you'll be able to come up with a more useful use of this feature.

Personal tools
Namespaces

Variants
Actions
Navigation
game select
Toolbox