locked
Get random line from text file RRS feed

  • Question

  • I just decided to learn game dev about 3-4 months ago and mostly started following tutorials on YouTube and googling whatever questions that I had. I don't have a background or previous education or experience with any of it, so it can be confusing. I have a working piece of code that I spliced together from 2 different codes, it works, but I'd like it to work slightly differently. I spliced together a typewriter code and a text reader code, so as of now, the code below works as follows: It gets the entire text of the text file and writes it the UI textbox and therefore displays up to the first 3 lines of the text file that the size of the UI panel will allow. What I have tried with no success is for some way to make it so that only 1 random line of the text file will be displayed whenever the player accesses the scene. Here is what I have, thanks in advance for any guidance on the subject. This is for Unity and C#.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine.UI;
    using System;
    using UnityEngine;
    public class FactGenerator : MonoBehaviour
    {
        public Text text;
        public TextAsset textFile;
        public bool playOnAwake = true;
        public float delayToStart;
        public float delayBetweenChars = 0.075f;
        public float delayAfterPunctuation = 0.0025f;
        private string story;
        private float originDelayBetweenChars;
        private bool lastCharPunctuation = false;
        private char charComma;
        private char charPeriod;
        public List<string> facts;
        public string[] lines;
        void Awake()
        {
            text = GetComponent<Text>();
            originDelayBetweenChars = delayBetweenChars;
            charComma = Convert.ToChar(44);
            charPeriod = Convert.ToChar(46);
            if (playOnAwake)
            {
                ChangeText(text.text, delayToStart);
            }
        }
        public void ChangeText(string textFile, float delayBetweenChars = 0f)
        {
            StopCoroutine(PlayText()); //stop Coroutine if exist
            story = Resources.Load<TextAsset>("dinofacts").text;
            text.text = ""; //clean text
            Invoke("Start_PlayText", delayBetweenChars); //Invoke effect
        }
        void Start_PlayText()
        {
            StartCoroutine(PlayText());
        }

        IEnumerator PlayText()
        {
            foreach (char c in story)
            {
                delayBetweenChars = originDelayBetweenChars;
                if (lastCharPunctuation)
                {
                    yield return new WaitForSeconds(delayBetweenChars = delayAfterPunctuation);
                    lastCharPunctuation = false;
                }
                if (c == charComma || c == charPeriod)
                {
                    lastCharPunctuation = true;
                }
                text.text += c;
                yield return new WaitForSeconds(delayBetweenChars);
                TextAsset factText = Resources.Load<TextAsset>("dinofacts");
                lines = textFile.text.Split("\r\n"[0]);

                bool addingFacts = true;
                for (int i = 0; i < lines.Length; i++)
                {
                    if (lines[i] == "facts:")
                    {
                        addingFacts = true;
                        Debug.Log("adding facts");
                        continue;
                    }
                    if (lines[i] != "")
                    {
                        if (addingFacts)
                        {
                            facts.Add(lines[i]);
                        }
                    }
                }
            }
        }
    }

    Thursday, January 17, 2019 11:55 PM

All replies

  • When you get the lines, you can choose one using the Random class.

    https://docs.microsoft.com/en-us/dotnet/api/system.random?view=netframework-4.7.2

    Something like so.

    Random rand = new Random();
    
    // Etc.
    
    
    List<string> lines = Whatever(); // Get the lines however you do it.
    
    string lineToDisplay = lines[rand(lines.Length - 1)]; // Get the random line.
    
    // And so on.

    Friday, January 18, 2019 12:16 AM
  • Hi momofhfaboy728013,

    Thank you for posting here.

    Since this post is related to Unity, please post it in the following forum.

    https://answers.unity.com/index.html

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 21, 2019 5:00 AM