Regex Efficiency Question
So, I've made code that creates multi-page Dialogue for an NPC, and it fully works, but I'm pretty new to C# so I'm having a problem with efficiency.
This is the relevant code:
public class NPCInteractor : MonoBehaviour {
private bool inRange; // Used to store if you're in range of the NPC or not
private bool talking; // Used to store if you're talking to an NPc or not
private int dialouge; // Used to store how far along you are with an NPCs dialouge
private string speech; // Holds the speech File as Text
// ⬅️ POINT A
void Start () {
dialouge = 0;
speech = (speechFile.text);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("e") || Input.GetKeyDown("joystick button 0")) // When E is pressed
{
if (inRange == true) // And you are in range of the NPC
{
if (talking == true) // if You are already talking to the NPC
{
dialouge++; // Move the dialouge along by One
string[] speechLines = Regex.Split(speech, "##"); //⬅️ POINT B
dialougeField.text = speechLines[dialouge];
}
else if (talking == false) // If you aren't already talking to the NPC
{
talking = true; // Note you are now talking to the NPC
textBox.gameObject.SetActive(true);
dialouge = (start);
string[] speechLines = Regex.Split(speech, "##"); //⬅️ POINT C
dialougeField.text = speechLines[dialouge];
nameField.text = npcName;
So, I'm reading the lines from a separate text file, then split the file into a list of lines (Using "##" to separate parts of dialogue), with the NPC being defined in the editor which items in the list are theirs. But the problem is I've no idea how to only have to define the list once at POINT A, since whenever I've tried to use public at POINT B or POINT C it just throws an error at me, the same as if I try to define speechLines with the other variables at POINT A. Although it works, I would obviously prefer if I could make it only have to define the speechLines Variable at the start, Once.
Its probably a pretty basic thing, but it's gonna annoy me if my code has a glaring inefficiency like this,
If it's needed the full code is at https://pastebin.com/x0JcChCh