Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am having trouble creating a tutorial level for my game in Unity. The first problem is efficiency. I have a script below, which displays the dialogue. And I want to know the best way to create goals that the player can meet without excessive amounts of script.

using UnityEngine;
using System.Collections;
using System.IO;
using System;
using UnityEngine.UI;

public class TutorialSystem : MonoBehaviour 
{
bool canSpeak = true;
protected StreamReader reader;
public Text Dialogue;
string sLine = " ";
public string SourceName;
string[] lines;
ArrayList arrText = new ArrayList();
int i = 0;

void Start()
{

    reader = new StreamReader("Assets/Resources/" + SourceName + ".txt");
    while(sLine != null)
    {
        sLine = reader.ReadLine();
        if(sLine != null)
        {
            arrText.Add(sLine);
        }
    }
    reader.Close();
    sLine = arrText[i].ToString();
    StartCoroutine(Typewrite(sLine));
    i++;
}

void Update()
{
    if(Input.GetButtonDown("Submit") && canSpeak)
    {
        sLine = arrText[i].ToString();
        StartCoroutine(Typewrite(sLine));
        i++;
    }
    if (Input.GetButtonDown("Submit") && !canSpeak && i == arrText.Count)
    {
        Dialogue.text = null;
    }

}

IEnumerator Typewrite(string message)
{
    canSpeak = false;
    Dialogue.text = null;
    foreach (char letter in message.ToCharArray())
    {
        Dialogue.text = Dialogue.text + letter;

        yield return new WaitForSeconds(0.01f);
    }
    canSpeak = true;
    if (i == arrText.Count)
    {
        canSpeak = false;
    }
}

}

The second problem is how to I make it expandable, currently the text reads a .txt file line by line. Does anyone have a suggestion on how I can change this to be used in situations such as character interaction or should I just make another script entirely for that?

Many thanks in advance.

share|improve this question
1  
This isn't really a question that can be answered in a definitive way. Is there any specific concerns you have in terms of scaling this? What kind of interactions were you looking to do? How would these interactions benefit the use of this particular script? –  Jon Jun 30 at 15:16
    
Thanks for the comment. I was looking towards proggression, so when something is completed such as moving to a certain position or dying for example it will load the next comment line.(if that makes sense) –  Joseph Cooper Jun 30 at 18:48
1  
using event triggers/listeners might be a good way to solve this...you could associate event ID's with dialog, load the text and id in as a Key Value pair and then have a method that captures the event and display the associated text with the event on screen. Defining enums for the different trigger ID's would make this nice and readable –  Matthew Pigram yesterday
    
Thanks, for the response. I was thinking along those lines but wasn't quite sure what I could use. A value pair enum would probably do the job. thanks again for the response. –  Joseph Cooper 16 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.