Hi there! You are currently browsing as a guest. Why not create an account? Then you get less ads, can thank creators, post feedback, keep a list of your favourites, and more!
Space Pony
Original Poster
#1 Old 16th Jul 2018 at 1:59 AM Last edited by gamefreak130 : 4th Jan 2019 at 3:44 AM.
Default Tutorial: Randomized String Outputs Using {Variation.X}
Note: This tutorial assumes you already have experience with script modding and string creation. If you're new to script modding, see TUTORIAL: Object Modding (aka adding interactions) and Tutorial:Sims 3 Localized Coding.

A neat little feature that I discovered when making the strings for my Advice Social Interactions mod was the dynamic text element {Variation.X}. This is used all the time in the game from Late Night onwards to generate a random string from a number of possible options with no scripting overhead. Despite its usefulness, however, I haven't seen it used in very many scripting mods, so I figured I may as well explain how to use it.

Say, for example, you have an instantiated immediate interaction that you want to function like a magic 8 ball, generating a random, possibly fortune-telling message (for the sake of simplicity, the interaction name is not localized):

Code:
public class Magic8Ball : ImmediateInteraction<Sim, Sim>
{
        [DoesntRequireTuning]
	private sealed class Definition : ImmediateInteractionDefinition<Sim, Sim, Magic8Ball>
	{
		public override string GetInteractionName(Sim a, Sim target, InteractionObjectPair interaction)
		{
			return "Shake Magic 8 Ball";
		}
		public override bool Test(Sim actor, Sim target, bool isAutonomous, ref GreyedOutTooltipCallback greyedOutTooltipCallback)
		{
			return !isAutonomous && actor == target;
		}
	}
	public static readonly InteractionDefinition Singleton = new Definition();

	public override bool Run()
	{
                string message = Localization.LocalizeString("Gamefreak130/LocalizedMod/Magic8Ball:Message", new object[0]);
		base.Actor.ShowTNSIfSelectable(message, StyledNotification.NotificationStyle.kSystemMessage);
		return true;
	}
}


As you can see in the Run() method, there is no need to use any sort of randomizer within the script itself; you only have to reference a single string.

Now for the real fun. When creating the lookup table, enter {Variation.X} for the associated string, where X is the number of different strings to select from:

Code:
<?xml version="1.0" ?>
<TEXT>

<KEY>Gamefreak130/LocalizedMod/Magic8Ball:Message</KEY>
<STR>{Variation.6}</STR>

<KEY>Gamefreak130/LocalizedMod/Magic8Ball:Message_1</KEY>
<STR>Without a doubt.</STR>

<KEY>Gamefreak130/LocalizedMod/Magic8Ball:Message_2</KEY>
<STR>Most likely.</STR>

<KEY>Gamefreak130/LocalizedMod/Magic8Ball:Message_3</KEY>
<STR>Cannot predict now.</STR>

<KEY>Gamefreak130/LocalizedMod/Magic8Ball:Message_4</KEY>
<STR>Concentrate and ask again.</STR>

<KEY>Gamefreak130/LocalizedMod/Magic8Ball:Message_5</KEY>
<STR>Don't count on it.</STR>

<KEY>Gamefreak130/LocalizedMod/Magic8Ball:Message_6</KEY>
<STR>Very doubtful.</STR>

</TEXT>


As you can see, in this case, there are a total of six possible responses, and the game will automatically choose one at random when the interaction is performed.

Each of the possible options takes the name of "[Original String Name]_1", "[Original String Name]_2", etc. This is very important; you MUST name the strings in this way, or else a blank string will be called instead!

Assuming the strings and the {Variation} expression are labelled correctly, however, you can select at random from any number of string outputs in this way. In fact, the callback when asking a butler for advice uses a {Variation.X} to select from over 50 possible responses!

"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
Back to top