- Home /
Make a method that returns a string from a text document.
I am currently making an item system where the item has a text document that looks something like this:
Name Flashlight
Description It makes light
ID 1
Type Tool
I want a script with a function that can find a line and return some text from that line. For example: public string Name; public string Description; public int ID;
public string DefineAttribute(string Attribute)
{
string Result = //looks in the text file and looks for Attribute. Result is all the text that follows the Attribute on the same line.
return Result;
}
Name = DefineAttribute(Name); //this should return flashlight.
If anyone knows how to make this function, please let me know. Also, it doesn’t have to be a .txt document, but that would be the easiest for me to work with.
Answer by TomColdtree · May 23, 2018 at 02:55 AM
This is best broken into two problems. The first what is the best structure to represent the data. The second is what is the best way to store and retrieve that data.
To structure the data you would probably want a dictionary. Useful link C# Dictionary Example
To save to file and read in the data you want to look at serialisation. Unity has built in functions for JSON serialisation which are easy to use. Unity Manual JSON serialisation Unfortunately you may need to wrap the dictionary in a class or struct to use it with Unity's JSON serialiser. Something like this:
[Serializable]
public class ItemData
{
public Dictionary<string, string> dictionary = new Dictionary<string, string>();
}
// ... Useage ...
ItemData myItemData = new ItemData();
myItemData.dictionary.add("Name", "Flashlight"); // and so on for the others
You can then use Unity's JsonUtility to convert to JSON and save to a file plus read the file and convert from JSON.
You can then write a method that returns a string from the class or just use the C# dictionary methods since it is essentially doing just that. Eg
String jsonString = // read the file into a string here
ItemData readItemData = new ItemData();
readItemData = JsonUtility.FromJson<ItemData>(jsonString);
Name = readItemData.dictionary["Name"];
The above code will save the ID as a string. This may be desired as said you want to return a string. If having an int is important to your data structure you can use Dictionary and make sure you parse the int to a string before returning it.
This seems pretty useful, is there anyway I could format the JSON file like this though?
Name Flashlight
Description It makes light
ID 1
Type Tool
@$$anonymous$$Coldtree
That would not be valid JSON. If you wanted to do it that way you would have to make your own parser for the text file. However JSON is designed to be read by computers and humans. That means once you create the first JSON file it would be easy to modify by hand if that is the purpose.
Ok. Do you know anywhere I might be able to find a tutorial for making your own text parser? @$$anonymous$$Coldtree
Answer by Theangrybagel · May 23, 2018 at 03:48 PM
@TomColdtree would this work?
String Test = "Name: Flashlight(;) Description: It makes light(;)";
String[] Attributes;
public String FindAttribute(String AttributeName) {
Attributes = Test.Split("(;)"); //splits each line.
forEach(string Line in Attributes)
{
String[] a;
if(Line.contains(AttributeName + ":")){
//the line is the right attribute.
a = Line.split(":");
}
return a[1];
}
}
I would seriously look at using a JSON parser or some other exisiting parser rather then reinvent the wheel. Since you have already added ":" and "(;)" to delimit your fields I don't see why it would be a problem to put it in JSON format like this.
{ "Name": "Flashlight", "Description": "It makes light", "ID": 1, "Type": "Tool" }
Your method may work in this particular case but may easily become unstuck on a different case and will be hard to expand on in the future.
Does JSON format require quotation marks around everything? @$$anonymous$$Coldtree
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How Do I Convert String to String Array In List.Add? 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
Convert Input Field to String 0 Answers