Home>

I am currently creating the dialogue display part of the RPG game, but I am struggling to change what the character speaks according to the progress of the story. Specifically speaking, when talking for the first time with the character, when talking about the second time after the story is not progressing (like a summary of the first conversation), talking when the story is advanced, I want to display these three. The source code is attached, so it would be greatly appreciated if you could answer after reviewing it (please see other than the commented part).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu (fileName = "DBVillager", menuName = "DB/DBVillager")]
public class DBVillager: ScriptableObject // Database
{
    // public serifname [] sn;
    // public string name;
    [SerializeField, TextArea (1,100)]
    public string serif;
}
// [System.Serializable]
// public class serifname
// {
// public string name;
// [SerializeField, TextArea (1,100)]
// public string serif;
//}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Villagers: MonoBehaviour
{
    // public serifname data;// stored as cannot be passed directly from DBVillager
    public DBVillager serif2;
    // public DBVillager seriftenpre;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MessageManager: MonoBehaviour
{
    [SerializeField] Text messagebox;
    [SerializeField] Text namebox;
    [SerializeField] CanvasGroup group;
    [SerializeField] Trigger trigger;
    [SerializeField] Villagers villagers;
    public string data;
    public string [] _data;
    float interval = 0.05f;
    public byte line_max = 2;
    bool flag1 = false;
    bool flag2 = true;
    void Start ()
    {
        group.alpha = 0;
    }
    public void MessageStart ()
    {
        group.alpha = 1;
        MessageInit ();
        / * if (! flag1){
            data = villagers.seriftenpre.serif;
            flag2 = true;
        } * /
        string data_text = data;
        data_text.Replace ("\ r", string.Empty);
        _data = data_text.Split ('\ n');
        StartCoroutine (Messagerrr ());
    }
    void MessageInit ()
    {
        messagebox.text = "";
        namebox.text = "";
    }
    public IEnumerator Messagerrr ()
    {
        byte line = 0;
        for (byte l = 0;l<_data.Length;l ++)
        {
            if (Tag (l)) continue;
            line ++;
            for (byte c = 0;c<_data [l] .Length;c ++)
            {
                // if (! flag2)
                // break;
                messagebox.text + = _data [l] [c];
                yield return new WaitForSeconds (interval);
            }
            // messagebox.text + = _data [l] + "\ n";
            // yield return new WaitForSeconds (0.5f);
            if (line% line_max == 0 || (l + 1<_data.Length&&_data [l + 1] [0] == 'n'))
            {
                line = 0;
                yield return new WaitUntil (() =>Input.GetButtonDown ("Submit"));
                messagebox.text = "";
            }
            else messagebox.text + = "\ n";
        }
        yield return new WaitUntil (() =>Input.GetButtonDown ("Submit"));
        group.alpha = 0;
        trigger.push = true;
        yield break;
    }
    bool Tag (byte l)
    {
        int start = _data [l] .IndexOf ("=", 0);
        if (start == -1) return false;
        // Get the number of characters with .Length
        int last = _data [l] .Length;
        string find = "";
        string find_data = "";
        for (int i = 0;i<start;i ++)
        {
            find + = _data [l] [i];}
        for (int i = ++ start;i<last;i ++)
        {
            find_data + = _data [l] [i];
        }
        switch (find)
        {
            case "name":
                namebox.text = find_data;
                break;
            case "pose":
                int num = data.IndexOf ("Is it so");
                break;
                 // if (flag2)
                 // flag1 = false;
                 // flag2 = false;
                // break;
                // StartCoroutine (Messagerrr ());
                // else
                // _data [l] = _data [l-2];
                // StartCoroutine (Messagerrr ());
                // break;
        }
        return true;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Trigger: MonoBehaviour
{
    //assign
    public bool push = true;
    public MessageManager messageManager;
    void Start ()
    {
    }
    void OnTriggerStay (Collider other)
    {
        if (other.gameObject.tag == "Villager")
        {
             if (Input.GetButtonDown ("Submit")&&push)
            {
                push = false;
                messageManager.data = other.GetComponent<Villagers>(). serif2.serif;
                messageManager.MessageStart ();
            }
        }
    }
    void Update ()
    {
    }
}
  • Answer # 1

      

    I am currently creating the dialogue display part of the RPG game, but I am struggling to change what the character speaks according to the progress of the story.

    It is common to create event production tools.
    It is a good idea to be able to display events just by reading the saved data discharged by the tool in the game.
    Then, all you have to do is change what you speak with the tool, so you can ask someone who isn't a programmer to make it, which makes it very easy.

  • Answer # 2

    A flag, but not a bool but an enum or an int, and it is not necessary to keep that state

Related articles