[Resolved] Read text file and get data from it.

Hi,

I’m working on a game project and it will contain dialogue and notes about the story.
To store all this text I was thinking of using external .txt file.

Is it possible to read a text and find variable I need inside a text file organize like below ?

mynotename01:"Jean-Michel"
mynotedate01:"01/04/2015"
mynotetext01:"Mytext"

Is it a good idea ? Or would that be better to make class like this :

[System.Serializable]
public class MyNotes
{
    public string noteName;
    public string noteDate;
    public string noteText;
}

Thanks

XML or ScriptableObject databases would be my suggestion. You’ll have to do some research for either though, they aren’t exactly something you can stumble through (intermediate level programming).

Either of your options are possible, but frankly using a text file (not XML) in that manner would be more difficult than just using XML- harder to parse and easier to mess up on. The latter (custom class) is likely going to be used regardless of which method you pick, because that’s how you’ll probably store the information once you’ve read it back into the program from the file.

You can use the second one directly, if you want to populate your variables in a Start() function or something, but I don’t recommend that. You could also use it more directly by making a prefab and slapping a one-time script on it that has a list of public serialized MyNotes objects. Then you can populate the list in the inspector and use the prefab like a database. This was one of the primary methods of storing large amounts of information internally (no external files needed) before ScriptableObjects were created as an alternative.

I see, well I thought text file was easy but it’s not :slight_smile: and XML looks also difficult, not impossible of course but difficult (for me).

I like the idea of using ScritableObject, something like this ? : http://www.jacobpennock.com/Blog/unity-pro-tip-use-custom-made-assets-as-configuration-files/

I was thinking about this, I used this method to store items, I had a prefab in my scene with a script I could populate with items and their properties, same as you say ?.

Thanks.

Yep, that’s how it works. You’ll never instantiate the prefab or actually have it in the scene, you’ll just reference it in scripts by dragging it onto “database” slots where needed and then accessing the data that way. This was a very common method before ScriptableObjects, and it’s far easier because ScriptableObject databases tend to have custom editors which can be time-consuming to create.

I’m trying ScriptableObjects, I think it’s perfect for my needs!

Thank you!