I was looking into auto scrolling text for the Text 101 game and came across a tutorial https://www.youtube.com/watch?v=vdSxOttY3zg&t=424s which seems to be exactly what i am looking for but they aren’t using states for their dialog box. As I am very knew to this i’m wondering how one would go about making auto scrolling text for states.
So the code I implemented from this tutorial seems to be counting the exact letters of my starting state. Even after I moved to room 1.
I can also see lettering scrolling quickly beside the period.
EDIT: Some slight progress made, I can now see it typing out “This is the starting state” While in room 1.
EDIT 2: It is now able to type out the states text correctly for each room.
Old Code
public class AdventureGame : MonoBehaviour
{
private bool isTyping = false;
private bool cancelTyping = false;
public float textSpeed;
[SerializeField] Text textComponent;
[SerializeField] State startingState;
State currentState;
// Start is called before the first frame update
void Start()
{
currentState = startingState;
textComponent.text = currentState.GetStateStory();
}
// Update is called once per frame
void Update()
{
ManageState();
}
void ManageState()
{
var nextStates = currentState.GetNextStates();
if (Input.GetKeyDown(KeyCode.Alpha1))
{
currentState = nextStates[0];
StartCoroutine(TextScroll(textComponent.text));
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
currentState = nextStates[1];
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
currentState = nextStates[2];
}
textComponent.text = currentState.GetStateStory();
}
private IEnumerator TextScroll (string lineofText)
{
int Letter = 0;
textComponent.text = "";
isTyping = true;
cancelTyping = false;
while (isTyping = true && !cancelTyping && (Letter < lineofText.Length - 1))
{
textComponent.text += lineofText[Letter];
Letter += 1;
yield return new WaitForSeconds(textSpeed);
}
textComponent.text = lineofText;
isTyping = false;
cancelTyping = false;
Debug.Log(Letter);
}
}
So the code works correctly now and it will Auto scroll the text for each state, very happy about that.
There are still some more things ill need to add in, I want to put in the cancel typing so they can fast forward the text if they don’t wish to wait. There is also a little bug were if you click 1/2/3 before it is done typing it will get a little wonky. So i will need to fix that, however i want choices to be made by using a mouse so i’m not to worried about it.
Alright here is the updated code for auto scrolling text, the user can now cancel text if they dont want to wait and they can no longer press 1/2/3 if it is typing. The only thing I would want to do now is make buttons the user has to click in order to make choices
public class AdventureGame : MonoBehaviour
{
private bool isTyping = false;
private bool cancelTyping = false;
public float textSpeed;
[SerializeField] Text textComponent;
[SerializeField] State startingState;
State currentState;
// Start is called before the first frame update
void Start()
{
currentState = startingState;
textComponent.text = currentState.GetStateStory();
StartCoroutine(TextScroll(currentState.GetStateStory()));
}
// Update is called once per frame
void Update()
{
ManageState();
}
void ManageState()
{
var nextStates = currentState.GetNextStates();
if (Input.GetKeyDown(KeyCode.Alpha1) && !isTyping)
{
currentState = nextStates[0];
StartCoroutine(TextScroll(currentState.GetStateStory()));
}
else if (Input.GetKeyDown(KeyCode.Alpha2) && !isTyping)
{
currentState = nextStates[1];
StartCoroutine(TextScroll(currentState.GetStateStory()));
}
else if (Input.GetKeyDown(KeyCode.Alpha3) && !isTyping)
{
currentState = nextStates[2];
StartCoroutine(TextScroll(currentState.GetStateStory()));
}
// Allows the user to skip text, only when automatic typing is active.
if (Input.GetKeyDown(KeyCode.Space) && isTyping && !cancelTyping)
{
cancelTyping = true;
Debug.Log(cancelTyping);
}
}
private IEnumerator TextScroll (string lineofText)
{
int Letter = 0;
textComponent.text = "";
isTyping = true;
cancelTyping = false;
while (isTyping = true && !cancelTyping && (Letter < lineofText.Length - 1))
{
textComponent.text += lineofText[Letter];
Letter += 1;
yield return new WaitForSeconds(textSpeed);
}
textComponent.text = lineofText;
isTyping = false;
cancelTyping = false;
Debug.Log(cancelTyping);
}
}