Tutorial:Sims 3 In Depth Scripting

From SimsWiki
Revision as of 13:37, 5 October 2011 by Odistant (Talk | contribs)

Jump to: navigation, search

Contents

Intro

My goal in this guide is to help the complete "noobs" at scripting languages and put them in words that they can describe. A lot of people who have gone through Buzzlers tutorial either didn't understand it or it was too confusing for them. This guide assumes you know absolutely nothing about C# and you really want to learn how to make those cool scripts for objects that you see all the time. This will go over methods, classes, strings, overrides, and more. You should have a very, very basic understanding of the InteliSense system that Visual Studio provides as well as a basic idea of using Reflector.
THIS IS A WORK IN PROGRESS

Requirements

  • Microsoft Visual C# Express 2008 - I will call this program VS for short. I highly reccomend that you use this version instead of the 2010 as it seems to not work correctly when it comes to referencing.
  • Sims3 Package Editor - I call it S3PE for short in this guide.
  • Sims3 Object Cloner - S3OC isn't mentioned very much in this guide until the very end. You'll normally use this program for finding what objects are derived from what and cloning items to change their scripts.
  • .NET assembly browser/decompiler - I use Reflector 6.8. You can get the trial version of Reflector 7 if you want.
  • A good understanding of using text editors and computers. Programming is not for the people who don't know how to use computers.

Lets do this!

Classes

Classes are the building blocks of scripting. They allow you to group up parts of your code and to "derive" parts of code. You will ALWAYS start off your code with a class of some sort.
Still don't understand the concept of a class? Think of them as groups. You group your code together. Similar to how baseball would be under the group of sports. Now, there are a bunch of different types of sports like football, tennis, and volleyball. All of them are sports because they are grouped under sports. It's common knowledge. When you think of baseball, you think of sports. When you think of sports, you think of baseball.

There are a few different types of classes. Here's a chart of them and what you would use them for. These are not all of them. They are just the ones that are commonly used throught Sims 3 scripting.

Type of Class How to use it

class

This is normally used right after the namespace. It is typically followed by a public/private class.

public class

This is used right after you have a class, however a class is not needed to use "public class". You can start off with "public class" right after your namespace. This makes your class accessible by anyone trying to use your code.

private class

This is used for making a class unaccessable by anyone but you. This means that if someone references your DLL they will not be able to use methods inside that class.


Deriving A Class

Deriving a class means that you're basing your class off of another class. Let's take a look at some Sims 3 objects. When you go into reflector and look at the FishTankModern class which is found under Sims3.Gameplay.Objects.Decorations.
When you look at this code you will notice a " : " after FishTankModern. This is telling us to derive, or use, the same exact functions, or methods, that FishTank has. It's really that simple.

Why should I derive off of another class? Object scripting in Sims 3 without deriving will make your life a living hell and Sims 3 popping up a bunch of errors when you try to run it. Like I said, deriving will allow us to use those same functions without having to create that part of code ourselves. Every single object is derived off of GameObject in one way or another. This is the class that allows objects to show interactions, do certain stuff, get the catalog names, etc. If you look at our example of FishTankModern and click on FishTank, Reflector will bring you to the FishTankClass. As you can see, FishTank is also derived off of GameObject. This gives any object derived off of FishTank all the methods of GameObject and FishTank.

I still don't get it... Lets take a look at a Dell computer. Notice how I'm saying a Dell computer? It's not just any type of computer, it's a Dell computer. A Dell computer is the same as a regular computer, but has different/similar/enhanced features. If we were to think of a Dell computer as being derived off of a class, it would be something like "public class DellComputer : Computer". This gives the DellComputer the same features as Computers but will allow us to add in even more methods without having to change Computer.
Now we have not just any type of DellComputer but a DellInspirion. We want the DellInspirion to have the same features as a DellComputer would have but more added to it. We would do "public class DellInspirion : DellComputer". Again, the DellInspirion is a DellComputer and the DellComputer is a Computer. So the DellInspirion would be able to use methods from DellComputer and Computer. You can keep going on with this for ever and ever.

Methods

Methods tell Sims 3 what to do when something happens. The game already comes with a variety of methods that you can use to implement into your script. The most common one is WorldEventHandler, TNSMessages, and Interaction methods.
Think of methods as steps to solve a math problem or functions. When you do a math problem, you follow certain steps to get to the final outcome. This is essentially the same with methods. They give the game steps on what to do when someone clicks on the object or uses a interaction.

Interaction Methods


Interactions are the pie menus that show up when you click on a object. When you look at the FishTank class in Reflector you can see some more classes inside the FishTank class. Most of these are the interactions. Looking at the Feed class you can see that it is derived off of Interaction, but it has those weird "greater than or less than" symbols. The best way to learn why you put those symbols in is by looking up at the Interaction class. Going to the Interaction class will show Interaction<TActor, TTarget>. TActor is the person performing the interaction. In our case, it will be a Sim. Simply known as Sim in scripting language. The TTarget will be the object that the player clicks on. That will be MyFishTank. I gave the interaction the name of Hello. So, you should have this:

public class MyFishTank : FishTank
    {
        public sealed class Hello : Interaction<Sim, MyFishTank>
        {

        }
    }

Run Method

You are now able to add in a Run method. This method tells the game to go ahead and do those "problems". We do this by adding in this code into the Hello sealed class we just created:

public override bool Run()
            {
                return true;
            }


Bool must always return a value of some sort. We simply use true in here.

Making Run do something

We need to add in something for the game to do now. I used the TNS method. This shows the messages that you see in the upper right hand corner. One of the ways to call a TNS is through ShowTNSIfSelectable. You do not make it as a new method unlike with the Run method. You put this one inside the Run method. Inside that method you call the TNS message by typing in base.Actor.ShowTNSIfSelectable. We cannot leave it just like that and end it there. You need to add in some parameters. The ShowTNSIfSelectable allows us to add in what we want it to show. Upon inspection of this method in Reflector, you can see that it requires us to set a string and a notification style.

Strings in programming simply means text, or words. In this case you put in the string, or text, that you want the TNS message in-game to display. I used "Hi". Notice the quotation marks. This lets game know that it's a string.

We now need to tell the game what type of message to show. I used the kSimIsTalking tip. This shows the Sim that is currently selected when the player performs the interaction. You do this by typing StyledNotification.NotificationStyle.kSimTalking. You should then end up with something similar to this:

public override bool Run()
            {
                base.Actor.ShowTNSIfSelectable("Hi", StyledNotification.NotificationStyle.kSimTalking);
                return true;
            }

So what we now have is a message that will show up saying Hi with the selected Sims portrait.

Can I use different types of "Notification Styles"?
Yep! When you type in StyledNotification.NotificationStyle. you'll notice that it will give you some different options to choose from. These are called enums, or enumerators. They are similar to int, or integers. Instead of using numbers, you use words associated to letters. If you go to the NotificationStyle enum in Reflector you'll see something like this:

public enum NotificationStyle : uint
{
    kCelebrityUpdate = 7,
    kCustom = 6,
    kDebugAlert = 5,
    kGameMessageNegative = 4,
    kGameMessagePositive = 3,
    kSimTalking = 1,
    kSystemMessage = 2
}

You can find more about Integers and Enumerators through some google searching. You won't need to know how to make enums very often in scripting for Sims 3. Because this is a basic guide, I won't go over more about them. However, if you're wanting to make a complex scripting mod then you'd best be off using integers and enumerators.

Why is everything starting with "base"? Good question. When you use "base" you're referring to the Interaction we derived from. If you notice with all interactions run methods in sims 3 will almost always begin with "base". We used based.Actor because Actor is the Sim performing the interaction and we wanted it to show a TNSmessage so we used the Notification method. Experiment with the different classes and methods you can use. The possibilities are endless. It'll take me days to go over every single one.

OnStartup

Personal tools
Namespaces

Variants
Actions
Navigation
game select
Toolbox