- Home /
NPC dialogue - Loading a text file to a string?
Hi! So when the player in my game talks to npcs, I use a string variable to show what they say (in a GUI.Label), and i edit this string in the inspector. But if the NPC has a long thing to say, in the inspector I can't do any "\n" or anything, and it also just isn't very convenient because the little box it has for you to enter isn't that great.
So I was thinking, maybe if i type out the things for npc's to say in a text file, then load that file through a script and save line to as a string variable? How would i do this?
Or is there a better alternative? - Thanks
Do you use playmaker? I bet that would be the easiest tool for something like this.
I submitted an engine to the asset store that will soon hopefully be live that does exactly this. Look out for it, it's called QuesText.
I'd rather not use another application, because i'm making my game as a learning experience, so that would sorta defeat the purpose..
Take a look at the X$$anonymous$$L methods in the public domain. I'm starting my $$anonymous$$ission$$anonymous$$anager and starting to ask the same kind of questions.
If you still want to work on the length of the text, take a look at this post:
http://answers.unity3d.com/questions/190800/wrapping-a-textmesh-text.html (c#)
In this post used for a text mesh but it works for every string.
Just put this function in a script and call it.
The first argument would be obviously the text you want to display, and the second, the number of character before line break
Answer by TrickyHandz · Aug 30, 2013 at 11:13 PM
Here is an example of where you might want to start with this using a TextAsset:
C#
using UnityEngine;
using System.Collections;
public class TextRead : MonoBehaviour
{
public TextAsset textFile;
string[] dialogLines;
// Use this for initialization
void Start ()
{
// Make sure there this a text
// file assigned before continuing
if(textFile != null)
{
// Add each line of the text file to
// the array using the new line
// as the delimiter
dialogLines = ( textFile.text.Split( '\n' ) );
}
}
}
JavaScript
var textFile : TextAsset;
var dialogLines : string[];
function Start()
{
// Make sure there is a text
// file assign before continuing
if(textfile)
{
// Add each line of the text file
// to the array using the new line
// as the delimiter
dialogLines = (textFile.text.Split("\n"[0]));
}
}
When you want to display a certain line from your file just call the appropriate index in the array of strings like this:
C#
// Assign the first string
// in the array to a variable
string dialog = dialogLines[0];
JavaScript
// Assign the first string
// in the array to a variable
var dialog : string = dialogLines[0];
Ah, this seems perfect, thanks a bunch! one question first though, is it possible to split it based on the amount of characters in the string? For example, split it after every 100th character?
If not it's not an issue, it would just be nicer for me :p
thanks again :)
It is possible, but it would take a different approach with the code. I'm away from my dev machine right now, but I will post an update to the answer when I can.
Actually it's okay, I've been fiddling with it more and it's actually perfect the way it is :p now it's muuch easier for me to create dialogue, thanks again