I’m going to start off by clarifying that yes, I know I’m in over my head trying to grapple things that I know nothing about. But this is how I do things. I try and make things work, and if I can’t find a solution on my own, I seek help.
SO! I’m trying to put together an intractable script, where a player can respond to an NPC in a dialogue window. I’m trying to make this scripting setup flexible enough to where it can trigger events such as spawning objects. What I have is based on this youtube tutorial by gamesplusjames.
The scripts I have are:
Dialogue1
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Dialogue1 : MonoBehaviour {
public string[] NPCText;
public string[] PCReply;
public bool ConvoEnd = false;
public string CurrentConv;
public void Test()
{
while(ConvoEnd == false)
NPCText[1] = "Hi!";
NPCText[2] = "What would you like to do?";
PCReply[1] = "1) Nothing, just wandering.";
PCReply[2] = "2) I'm not sure. Do you have any suggestions?";
var input = Input.inputString;
switch (input)
{
case "1":
NPCText[3] = "Alright, take care.";
ConvoEnd = true;
break;
case "2":
CurrentConv = "TestA";
TestA();
ConvoEnd = true;
break;
default:
break;
}
}
public void TestA()
{
while (ConvoEnd == false)
NPCText[1] = "Hmm. I don't know...\n This isn't getting anyone anywyere.";
PCReply[1] = "1) Yeah, I suppose I'll leave.";
PCReply[2] = "2) What are you talking about? This is fun!";
PCReply[3] = "3) Let's go back a step...";
var input = Input.inputString;
switch (input)
{
case "1":
NPCText[4] = "Alright, take care.";
ConvoEnd = true;
break;
case "2":
NPCText[4] = "No, it's boring. Go away.";
ConvoEnd = true;
break;
case "3":
NPCText[4] = "Okay.";
CurrentConv = "Test";
Test();
ConvoEnd = true;
break;
default:
break;
}
}
}
Speaker
using UnityEngine;
using System.Collections;
public class Speaker : MonoBehaviour {
public int startLine;
public int endLine;
public TextManager theTextBox;
public bool requireButton;
private bool waitForPress;
public bool destroyWhenActivate;
public string StartDialogue;
// Use this for initialization
void Start()
{
theTextBox = FindObjectOfType<TextManager>();
}
// Update is called once per frame
void Update()
{
if (waitForPress && Input.GetKeyDown(KeyCode.J))
{
//theTextBox.ReloadScript(StartDialogue);
theTextBox.currentLine = startLine;
theTextBox.endAtLine = endLine;
theTextBox.EnableTextBox();
if (destroyWhenActivate)
{
Destroy(gameObject);
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
if (requireButton)
{
waitForPress = true;
return;
}
theTextBox.ReloadScript(StartDialogue);
theTextBox.currentLine = startLine;
theTextBox.endAtLine = endLine;
theTextBox.EnableTextBox();
if (destroyWhenActivate)
{
Destroy(gameObject);
}
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
waitForPress = false;
}
}
}
TextManager
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextManager : MonoBehaviour {
public GameObject textBox;
public Speaker whoSpeaks;
public Dialogue1 theTextFile;
public int currentLine;
public int endAtLine;
public Player player;
public bool isActive;
public bool stopPlayerMovement;
private bool isTyping = false;
private bool cancelTyping = false;
public float typeSpeed;
public Text theText;
public string[] NPCLines;
// Use this for initialization
void Start () {
if (endAtLine == 0)
{
endAtLine = NPCLines.Length - 1;
}
if (isActive)
{
EnableTextBox();
}
else {
DisableTextBox();
}
}
// Update is called once per frame
void Update () {
NPCLines = theTextFile.NPCText;
if (!isActive)
{
return;
}
if (Input.GetKeyDown(KeyCode.Return))
{
if (!isTyping)
{
currentLine += 1;
if (currentLine > endAtLine)
{
DisableTextBox();
}
else {
StartCoroutine(TextScroll(NPCLines[currentLine]));
}
}
else if (isTyping && !cancelTyping)
{
cancelTyping = true;
}
}
}
private IEnumerator TextScroll(string lineOfText)
{
int letter = 0;
theText.text = "";
isTyping = true;
cancelTyping = false;
while (isTyping && !cancelTyping && (letter < lineOfText.Length - 1))
{
theText.text += lineOfText[letter];
letter += 1;
yield return new WaitForSeconds(typeSpeed);
}
theText.text = lineOfText;
isTyping = false;
cancelTyping = false;
}
public void EnableTextBox()
{
textBox.SetActive(true);
isActive = true;
if (stopPlayerMovement)
{
player.canMove = false;
}
StartCoroutine(TextScroll(NPCLines[currentLine]));
}
public void DisableTextBox()
{
textBox.SetActive(false);
isActive = false;
player.canMove = true;
}
public void ReloadScript(TextAsset Dialogue)
{
if (Dialogue != null)
{
NPCLines = new string[1];
//NPCLines = (Dialogue.text.Split('\n'));
}
}
}
The issue I’m running in to is, as far as I’m aware, getting the TextManager script to read the method from Dialogue as indicated from Speaker (StartDialogue is the name of the method in Dialogue)
Thanks in advance for any help provided!