Scripting Text Strings Question

Discuss modding questions and implementation details.
Post Reply
User avatar
harbinger451
Posts: 191
Joined: Mon Apr 13, 2020 4:08 pm
Contact:

Scripting Text Strings Question

Post by harbinger451 »

I've done a script for replacing various text strings with corrections and changes RE my Background Question And History Fixes Mod. It's working fine, except for the few that are supposed to select an option from two or three choices (it always seems to pick the first listed) and insert them into the main body of the History text.

EXAMPLE:

Code: Select all

		case 4215: return CreateTokens(TextFile.Formatting.JustifyLeft,
		"corrupt knights",
		"oppressive warriors",
		"berserker soldiers"
		);
I have a feeling it's the commas that are the problem (should they be a semi-colon perhaps?), but really I have no idea.
Image

User avatar
Magicono43
Posts: 1141
Joined: Tue Nov 06, 2018 7:06 am

Re: Scripting Text Strings Question

Post by Magicono43 »

Atleast from what I can see from that example, that is a singular text-token, not multiple. So it's likely not "choosing" anything, but just displaying that singular token and the rest is being cut out from whatever the display panel dimensions are.

User avatar
harbinger451
Posts: 191
Joined: Mon Apr 13, 2020 4:08 pm
Contact:

Re: Scripting Text Strings Question

Post by harbinger451 »

Yeh, you're right. I suppose I need to know how to make multiple tokens for the same text string. Maybe make three separate listings for case 4215, for example, each with one of the choices ... or would that just cause more problems?
Last edited by harbinger451 on Sun Feb 26, 2023 4:37 pm, edited 1 time in total.
Image

User avatar
Magicono43
Posts: 1141
Joined: Tue Nov 06, 2018 7:06 am

Re: Scripting Text Strings Question

Post by Magicono43 »

I'd have to see the rest of the code to know because I don't know what the case values even mean in this context, so I can't really recommend what to do.

User avatar
harbinger451
Posts: 191
Joined: Mon Apr 13, 2020 4:08 pm
Contact:

Re: Scripting Text Strings Question

Post by harbinger451 »

The whole script is as follows. Most cases do not involve choices, but cases 4213, 4215, 4409, 4443 and 4652 are supposed to, I just don't know how to code for that. Each line in those cases (towards the bottom of the code) are the different choices.

Code: Select all

using UnityEngine;
using DaggerfallWorkshop.Game;
using DaggerfallWorkshop.Game.Utility.ModSupport;
using DaggerfallWorkshop.Utility;
using DaggerfallConnect.Arena2;
using DaggerfallWorkshop;

namespace HistoryFixesMod
{
    public class HistoryFixes : MonoBehaviour
    {
        private static Mod mod;

        [Invoke(StateManager.StateTypes.Start, 0)]
        public static void Init(InitParams initParams)
        {
            mod = initParams.Mod;

            var go = new GameObject(mod.Title);
            go.AddComponent<HistoryFixes>();

            mod.IsReady = true;
        }

        private void Start()
        {
            DaggerfallUnity.Instance.TextProvider = new TextProvider(DaggerfallUnity.Instance.TextProvider);
        }
    }

    class TextProvider : FallbackTextProvider
    {
        public TextProvider(ITextProvider fallback)
            : base(fallback)
        {

        }

        public override TextFile.Token[] GetRSCTokens(int id)
        {
            // Corrections to text strings that contribute to the class histories accessible in your character sheet
            switch(id)
            {
                case 4119: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "Your parents complemented one another, both as sorcerers as",
                    "well as educators. Your mother was most attentive in teaching",
                    "you the finer points of %q1,",
                    "but your father would use more dramatic methods. As you grew",
                    "in your talents and your mother taught you the spells, your",
                    "father would show you how you must attract and draw upon the",
                    "magicka of the universe to have the power to cast those spells.",
                    "He explained that you, like them, were not able to regenerate",
                    "magicka within your own body. It was commonplace for him to",
                    "walk in the room and cast a lightning spell at you. Frightful",
                    "though it was, you would feel exhilaration upon drawing upon",
                    "the magicka.",
                    " ",
                    "As you grew, it was apparent that you had unusually strong",
                    "magical powers. Soon you were able to %q1a",
                    "yourself. You remember your parents talking about an evil",
                    "Battlemage, Jagar Tharn, how it was known in their circle",
                    "that he had usurped the power of the land away from its",
                    "rightful ruler. No one dared to move against him.",
                    "%q2.",
                    "One day in your twentieth year, a courier arrived at your",
                    "house with the news that a powerful hero had killed",
                    "Jagar Tharn and restored the rightful Emperor to the throne.",
                    "Even your gruff father nearly wept with joy at the news.",
                    " ",
                    "The courier also brought the news that there would be a great",
                    "celebration in Imperial City and that all the people who had",
                    "worked secretly to depose Tharn were invited. Once the",
                    "courier left, you asked your parents if you could go to the",
                    "celebration. Your mother and father looked concerned and",
                    "explained that there was not enough gold for all three of you",
                    "to make the trip. Downcast, you tried to hide your",
                    "disappointment. Your father, however, turned to you and said",
                    "that it was time you made your own way in the world. He gave",
                    "you %q4 and a very unusual staff",
                    "and sent you on your way to the Imperial City and the",
                    "celebration.",
                    " ",
                    "After many days of travel, you approached the capital of the",
                    "realm -- Imperial City. You noticed a small band of travelers",
                    "a short distance in front of you. As you were about to hail",
                    "them, they were attacked by brigands who had been lying in",
                    "wait in the woods that lined the road. As you rushed to help,",
                    "one of the brigands, obviously a Mage, raised his staff and",
                    "cast a spell at you. Your ears roared as the flame bolt hit",
                    "you, but you absorbed the magicka and let fly a very powerful",
                    "shock spell. When the bolt struck the brigand mage, he erupted",
                    "into a column of flames and was reduced to a pile of ashes.",
                    "The brigands and the travelers all stopped and stared, and the",
                    "brigands ran back into the woods. The travelers thanked you",
                    "and introduced themselves as members of the Imperial Family",
                    "returning to Imperial City for the celebrations. They insisted",
                    "that you come with them to the palace and have an audience",
                    "with the Emperor.",
                    " ",
                    "The Emperor was very impressed by your bravery and knew",
                    "your family as loyal supporters of the Empire. He presented",
                    "you with %q5. The celebrations continued for",
                    "weeks, and the Emperor often called on you for informal talks.",
                    "When you were not in audience, you usually spent your time",
                    "%q3.",
                    "One night you were called to the Emperor's presence in a",
                    "manner such that you knew the business was serious. He",
                    "met you in his study and there told you he had a favor",
                    "to ask."
                    );
				case 4122: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "Your parents traveled with a carnival throughout your youth,",
                    "so you were exposed to the best and worst of Tamriel. From your",
                    "mother you learned how to %q1.",
                    "From your father you learned many things -- how to",
                    "%q2, to %q3,",
                    "and the rudiments of %q4.",
                    "All could be used for defense as well as entertainment.",
                    " ",
                    "The years between your tenth and twentieth birthdays are among",
                    "the worst Tamriel has ever suffered. The carnival was not such",
                    "a happy place, and movement within the land was severely",
                    "restricted. Agents of the Emperor would search the countryside",
                    "for enemies of the Empire and would question all quite rudely.",
                    "You always hoped that such a refugee would make his way to the",
                    "carnival, and then one day, one did. When the bedraggled young",
                    "man was hidden away, you had your opportunity to have fun with",
                    "the Imperial agents. Gleefully, you convinced half of the agents",
                    "that you were their commanding officer in disguise and sent",
                    "them after the other agents, insisting they were double-",
                    "agents. It turned out to be your last prank. You were",
                    "shortly after jailed as an enemy of the Emperor.",
                    " ",
                    "When the true Emperor took his throne back from the usurper",
                    "Jagar Tharn, all political prisoners, including you, were",
                    "released from the dungeons. You and only you were bathed,",
                    "dressed in fine clothing, and presented to the Emperor",
                    "himself. Emperor Uriel told you that the young man whose",
                    "life you saved at the carnival was his own son, the prince",
                    "%imp. The Emperor congratulated you on your",
                    "ability to charm, confuse, and befuddle and gave you",
                    "%q6 as a token of his appreciation.",
                    "He also asked you for a special favor."
                    );
				case 4124: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "You grew up on the streets, an urchin in your little village",
                    "in the %hpw of %hpn. You do not know when you",
                    "first learned to %q1 or to %q2 or when",
                    "%q3.",
                    "You only know that your skills and your philosophy are what",
                    "kept you alive and sane during the dark years when Jagar Tharn",
                    "usurped the throne of the rightful emperor. Tharn's agents",
                    "even searched out your little village, looking for political",
                    "dissidents and other undesirables. Those they found",
                    "disappeared forever.",
                    " ",
                    "For the benefit of all, an uneasy alliance formed between you",
                    "and all the other villagers who were in questionable lines of",
                    "work. One night, you witnessed a ragged boy being pursued by",
                    "a dozen blackclad agents of Jagar Tharn. You took a shortcut",
                    "through the alleys to meet the boy, and it took little effort",
                    "to convince him to run with you. The two of you stole into a",
                    "concealed cellar and waited for the agents to pass. The boy",
                    "was obviously not from the village, and when you asked him",
                    "his name, he told you, hesitantly, %imp.",
                    "You laughed at his royal sounding name, and he told you very",
                    "seriously that he was the son of the rightful ruler of Tamriel",
                    "and that an imposter was on the throne. At the time, you",
                    "humored his boyish imagination. Now, of course, you know that",
                    "he was telling the truth. You told him the best way out of the",
                    "village, avoiding the areas frequented by the blackclad ones,",
                    "and he told you that when he saw his father again, you would",
                    "be properly thanked for that act of kindness.",
                    " ",
                    "Some years later, your luck ran out and you were imprisoned",
                    "on the charge of %q4.",
                    "You had been in jail for several days when you received a",
                    "visitor in the full regalia of an Imperial courier. He told",
                    "you that in reward for your service to Prince %imp,",
                    "you were granted a full pardon and %q5",
                    "You were also given a simple, unpretentious letter inviting",
                    "you to the Imperial City for an audience with the Emperor. You",
                    "had your eyes set on a bigger fishpond anyhow, so you went."
                    );
				case 4125: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "You grew up on a farm and became quite agile from",
                    "continually rounding up wandering sheep. One Merchants",
                    "Festival, while visiting the village with your family, you",
                    "were intrigued with a group of wandering acrobats and decided",
				"that you wanted to become an acrobat yourself. At home you",
                    "started practicing even though your parents complained that",
                    "you were neglecting your duties. Finally, when you were just",
                    "old enough to be called an adult, you ran away from home.",
                    " ",
                    "You never did find that carnival troupe. You wandered to a small",
                    "village deep in the %hpw and learned to %q1",
                    "and to %q2 and",
                    "%q3.",
                    " ",
                    "You know that your skills and your philosophy are what",
                    "kept you alive and sane during the dark years when Jagar Tharn",
                    "usurped the throne of the rightful emperor. Tharn's agents",
                    "even searched out that little village, looking for political",
                    "dissidents and other undesirables. Those they found",
                    "disappeared forever.",
                    " ",
                    "For the benefit of all, an uneasy alliance formed between you",
                    "and all the other villagers who were in questionable lines of",
                    "work. One night, you witnessed a ragged boy being pursued by",
                    "a dozen blackclad agents of Jagar Tharn. You took a shortcut",
                    "through the alleys to meet the boy, and it took little effort",
                    "to convince him to run with you. The two of you stole into a",
                    "concealed cellar and waited for the agents to pass. The boy",
                    "was obviously not from the village, and when you asked him",
                    "his name, he told you, hesitantly, %imp.",
                    "You laughed at his royal sounding name, and he told you very",
                    "seriously that he was the son of the rightful ruler of Tamriel",
                    "and that an imposter was on the throne. At the time, you",
                    "humored his boyish imagination. Now, of course, you know that",
                    "he was telling the truth. You told him the best way out of the",
                    "village, avoiding the areas frequented by the blackclad ones,",
                    "and he told you that when he saw his father again, you would",
                    "be properly thanked for that act of kindness.",
                    " ",
                    "Some years later, your luck ran out and you were imprisoned",
                    "on the charge of %q4.",
                    "You had been in jail for several days when you received a",
                    "visitor in the full regalia of an Imperial courier. He told",
                    "you that in reward for your service to Prince %imp,",
                    "you were granted a full pardon and %q5",
                    "You were also given a simple, unpretentious letter inviting",
                    "you to the Imperial City for an audience with the Emperor. You",
                    "had your eyes set on a bigger fishpond anyhow, so you went."
                    );
				case 4126: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "You might not ever have been a thief, for you had been",
                    "raised and trained from an early age to be %q1.",
                    "Until the fateful day when the caravan your master,",
                    "%mn, and you were traveling in",
                    "was waylaid by an orc tribe, you were content that your place in",
                    "life was set. Suddenly, everything you believed in was proved",
                    "false. The treacherous %q1a you had put",
                    "your faith in sacrificed you in order that he might escape the",
                    "orcs. Though you managed to survive in your new role as a",
                    "captive of the orcs, you never again would trust",
                    "%q1.",
                    " ",
                    "You had grown up among the superstitious people in the backwater",
                    "%hpw of %hpn, and had believed that orcs",
                    "were demons or daedra, but you found the tribe to be surprisingly",
                    "civil. They needed a %ra to interpret the books they",
                    "had found about the ancient orcish capital of Orsinium. As you",
                    "stayed with them and learned their language, you developed a",
                    "respect for their primitive but noble ambitions. Nevertheless,",
                    "you knew that you needed the society of other %ra",
                    "people, so when the opportunity rose, you escaped.",
                    " ",
                    "Times were difficult all over %hpn, but you found",
                    "that the %q2 and %q3 skills you had",
                    "developed in the orc camp allowed you to be a fairly successful",
                    "thief. One night, while ransacking a nobleman's townhouse, you",
                    "came across correspondence between him and the former Imperial",
                    "Battlemage Jagar Tharn. You sold these to a contact you had in",
                    "town for more gold than you anticipated they would be worth.",
                    "You did not realize their full value until you received a",
                    "visit from Prince %imp in your room at the tavern.",
                    " ",
                    "He told you that Jagar Tharn had been magically impersonating",
                    "his father for the past ten years, and only recently had the",
                    "true Emperor been restored. Loyal subjects like you were rare",
                    "and the Emperor wished to thank you personally. You traveled",
                    "with Prince %imp to the Imperial City and",
                    "there you were presented to the Emperor. He was indeed very",
                    "grateful for your service and gave you %q4.",
                    "He also asked you for a favor."
                    );
				case 4127: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "You grew up on the streets, an urchin in your little village",
                    "in the %hpw of %hpn. You do not know when you",
                    "first learned to %q1 or to %q2 or when",
                    "%q3.",
                    "You only know that your skills and your philosophy are what",
                    "kept you alive and sane during the dark years when Jagar Tharn",
                    "usurped the throne of the rightful emperor. Tharn's agents",
                    "even searched out your little village, looking for political",
                    "dissidents and other undesirables. Those they found",
                    "disappeared forever.",
                    " ",
                    "For the benefit of all, an uneasy alliance formed between you",
                    "and all the other villagers who were in questionable lines of",
                    "work. One night, you witnessed a ragged boy being pursued by",
                    "a dozen blackclad agents of Jagar Tharn. You took a shortcut",
                    "through the alleys to meet the boy, and it took little effort",
                    "to convince him to run with you. The two of you stole into a",
                    "concealed cellar and waited for the agents to pass. The boy",
                    "was obviously not from the village, and when you asked him",
                    "his name, he told you, hesitantly, %imp.",
                    "You laughed at his royal sounding name, and he told you very",
                    "seriously that he was the son of the rightful ruler of Tamriel",
                    "and that an imposter was on the throne. At the time, you",
                    "humored his boyish imagination. Now, of course, you know that",
                    "he was telling the truth. You told him the best way out of the",
                    "village, avoiding the areas frequented by the blackclad ones,",
                    "and he told you that when he saw his father again, you would",
                    "be properly thanked for that act of kindness.",
                    " ",
                    "Some years later, your luck ran out and you were imprisoned",
                    "on the charge of %q4.",
                    "You had been in jail for several days when you received a",
                    "visitor in the full regalia of an Imperial courier. He told",
                    "you that in reward for your service to Prince %imp,",
                    "you were granted a full pardon and %q5",
                    "You were also given a simple, unpretentious letter inviting",
                    "you to the Imperial City for an audience with the Emperor. You",
                    "had your eyes set on a bigger fishpond anyhow, so you went."
                    );
				case 4177: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "the Orcs from a nearby stronghold"
                    );
				case 4213: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "corrupt marketeers",
                    "extortionate traders"
                    );
				case 4215: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "corrupt knights",
                    "oppressive warriors",
                    "berserker soldiers"
                    );
				case 4227: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "learned scholar"
                    );
				case 4230: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "converse with the Orcs"
                    );
				case 4409: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "jumping high and long",
                    "developing your jumping ability"
                    );
                    case 4435: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "the mischievous imps of Oblivion"
                    );
				case 4443: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "wealthy merchant",
                    "rich merchant"
                    );
				case 4652: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "a scholar",
                    "an intellectual"
                    );
				case 4653: return CreateTokens(TextFile.Formatting.JustifyLeft,
                    "scholar"
                    );
            }

            return base.GetRSCTokens(id);
        }
    }
}
Image

User avatar
Magicono43
Posts: 1141
Joined: Tue Nov 06, 2018 7:06 am

Re: Scripting Text Strings Question

Post by Magicono43 »

I'm going to have to ask the person who wrote the "FallbackProvider" script, that being kaboissonneault it seems. It looks like it somehow splits those values into multiple pieces of text based on something, but I'm not sure where those values would be coming from to determine what "choice" you made on a particular question.

User avatar
Kab the Bird Ranger
Posts: 123
Joined: Tue Feb 23, 2021 12:30 am

Re: Scripting Text Strings Question

Post by Kab the Bird Ranger »

This is not exactly related to my code, but I think I have something that could help.

The CreateTokens you're using is for creating a single, multi-line record. Text records like 4215 act as multiple subrecords under one record, that DFU picks randomly. This is done using a subrecord separator in the token sequence.

So, instead of sending everything through TextProvider.CreateTokens, my text provider has these 4 utilities:

Code: Select all

        public static TextFile.Token[] CreateSimpleTokens(string line)
        {
            return new TextFile.Token[] { new TextFile.Token(TextFile.Formatting.Text, line), new TextFile.Token(TextFile.Formatting.EndOfRecord) };
        }

        public static TextFile.Token[] CreateSimpleTokens(string[] lines)
        {
            return CreateFormattedTokens(TextFile.Formatting.NewLine, lines);
        }

        public static TextFile.Token[] CreateFormattedTokens(TextFile.Formatting formatting, params string[] lines)
        {
            List<TextFile.Token> tokens = new List<TextFile.Token>();

            foreach (string line in lines)
            {
                tokens.Add(new TextFile.Token(TextFile.Formatting.Text, line));
                tokens.Add(new TextFile.Token(formatting));
            }

            tokens.Add(new TextFile.Token(TextFile.Formatting.EndOfRecord));

            return tokens.ToArray();
        }

        public static TextFile.Token[] CreateAlternativeTokens(params string[] lines)
        {
            if (lines.Count() == 0)
                return Enumerable.Empty<TextFile.Token>().ToArray();

            List<TextFile.Token> tokens = new List<TextFile.Token>();

            for(int i = 0; i < lines.Count() - 1; ++i)
            {
                tokens.Add(new TextFile.Token(TextFile.Formatting.Text, lines[i]));
                tokens.Add(new TextFile.Token(TextFile.Formatting.SubrecordSeparator));
            }

            tokens.Add(new TextFile.Token(TextFile.Formatting.Text, lines[lines.Count() - 1]));
            tokens.Add(new TextFile.Token(TextFile.Formatting.EndOfRecord));

            return tokens.ToArray();
        }
CreateSimpleTokens is for creating a simple message with no special alignments, with an overload that handles multi-lines.
CreateFormattedTokens is really the same as TextProvider's CreateTokens, just renamed for clarity.
And the relevant one here is CreateAlternativeTokens: this is the one that creates a subrecord for each string, rather than create a newline.

By having these functions in your text provider, you should have all the cases covered.

User avatar
harbinger451
Posts: 191
Joined: Mon Apr 13, 2020 4:08 pm
Contact:

Re: Scripting Text Strings Question

Post by harbinger451 »

Okay ... thanks for that ... I'll try a bit of experimenting with the CreateAlternativeTokens for the text strings that need it, and see if I can get it to work in my script. Fingers crossed. I'm pretty new to this scripting lark. :oops:
Image

Post Reply