Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fadi-l · Aug 12, 2011 at 11:47 AM · textseperate

load line from a text file and separate it

Hi... I wanna make a contest game ( question and 4 answers one of them right).

my idea : put all questions in a text file then load the questions one by one.

the questions in the text file will be like this :


The country called the Land Of Rising Sun is ? Chiana $Japan France UK

What is the common name for ascorbic acid ? $Vitamin C Vitamin A Vitamin D Vitamin B - - - - - - $ : it's mean the right answer.

what I want :

1 - How can I load just to the first line to "GUI objectS"

2- How can I separate it like this

Ques_GUI = The country called the Land Of Rising Sun is ?

Ans1_GUI = Chiana

Ans2_GUI = Japan

Ans3_GUI = France

Ans4_GUI = UK

is that possible ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by BerggreenDK · Aug 13, 2011 at 10:28 PM

You could use a simple PLAIN ASCII file too (like the good old days before XML was invented)

A format like this:

 Q:100:The country called the Land Of Rising Sun is
 A:0:Chiana
 A:1:Japan
 A:0:France
 A:0:UK

Each linebreak means a new line, if it starts with Q, you have a new beginning of a question with "some answers" following.

Now to load a question, you seek through the textlines http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx

The first character to look for is a Q, this control code states that you have a line that begins a Question. So if you use string.Substring(0,1) or string.StartsWith('Q') you then you should have a way of detecting if you are reading a question.

Next I used : as a seperator char, this allows you to split a string. The split function returns a nice array of strings. so

 string[] question = questionLineFromfile.Split(':');
 string[] answer = answerLineFromfile.Split(':');

My suggestion for a format is this:

Question:

  • Q (to identify its a question)

  • nn (number to tell which question this is, if you need to have id's on them?)

  • text (this part is the actual visual text-string to show)

You get it by indexing like this: guitext = question[2];

Now the answers:

  • A (to identify its an answer to the question, as long as we read A, its another answer)

  • n (a value instead of the $ sign, this value states how many POINTS the answer is worth, you could also just use it for true/false)

  • text (this is the answer to show in GUI)

Same index type as the question.

Here is my idea fast written, not tested and not finished... but try to see if you can build on this:

  using (StreamReader sr = new StreamReader("questions.txt"))
  {
     string line; // this is the buffer for the content from your file
     while ((line = sr.ReadLine()) != null)
     {
        // now your buffer has 1 single line, time to parse it for contents.

        // first split it:
        string[] items = line.split(':');

        // the test if its a question or answer:
        if (items[0] == 'Q')
        { // we have a question here 
            Debug.Log("Question: " + items[2] + "?");
        }
        else
        {
          // then it must be an answer (if you have blank lines or extra lines, then check if its 'A' or not too.
            Debug.Log("Answer: " + items[2]);
        }
     }
 }


let me know if this works for you or not.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by CHPedersen · Aug 12, 2011 at 12:21 PM

If I were you, I think I would define an XML format for the questions, and then have the answers be child tags of each question. A child tag would then have two attributes, one with the answer text, and another with a boolean True/False indicating whether that particular answer was correct or not.

Here's an example of what such an XML file COULD look like:

<?xml version="1.0" encoding="utf-8" ?>
<Questions>
  <Question Text="The country called the Land Of Rising Sun is ?">
    <Answer Text="China" Correct="False"></Answer>
    <Answer Text="Japan" Correct="True"></Answer>
    <Answer Text="France" Correct="False"></Answer>
    <Answer Text="UK" Correct="False"></Answer>
  </Question>
  <Question Text="What is the common name for ascorbic acid ?">
    ...(Answer tags)
  </Question>
</Questions>

Then, in your script, you load the file using this simple command:

System.Xml.XmlDocument questions = new System.Xml.XmlDocument();
questions.Load("Questions.xml");

Assuming you call the file "Questions.xml", of course. You then have an xml document with nodes and everything that you can traverse in code. Here's an example that generates buttons out of the answers:

// Grab the second node (The first is an XmlDeclaration) foreach (XmlNode xmlNode in questions.ChildNodes[1].ChildNodes) { // Put the question's text attribute in a label GUILayout.Label(xmlNode.Attributes["Text"].Value);

 // For each of the answers in the question node
 foreach (XmlNode answer in xmlNode.ChildNodes)
 {
     // Make a button that you can click to select that answer
     if (GUILayout.Button(answer.Attributes["Text"].Value))
     {
         // If clicked, check that node's Correct-attribute to see if it was correct
         if (answer.Attributes["Correct"].Value == "True")
         {
             // YAY, that was correct
         }
         else
         {
             // Wrong answer
         }
     }
 }            

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fadi-l · Aug 12, 2011 at 04:32 PM 0
Share

thank u for answering

but there is an error here :

System.Xml.XmlDocument questions = new System.Xml.XmlDocument();

it doesn't work

avatar image CHPedersen · Aug 13, 2011 at 09:19 PM 0
Share

It's quite impossible to help you at all if you don't provide any information about what the error is, except for saying "it doesn't work". What about it doesn't work? Did you copy-paste my C# code into a JavaScript source file or something?

avatar image
0

Answer by $$anonymous$$ · Aug 14, 2011 at 09:28 AM

First of all to design the xml file as you want.

 <levels>  
   <level>  
     <name>Level 1 (xml)</name>  
     <tutorial>Try comes close to all four objects. (xml)</tutorial>  
     <object name="Cube"> Hi, I'm a cube! (xml) </object>  
     <object name="Cylinder"> Hi, I'm a Cylinder! (xml) </object>  
     <object name="Capsule"> Hi, I'm a Capsule! (xml) </object>  
     <object name="Sphere"> Hi, I'm a Sphere! (xml)</object>  
     <finaltext>Very Good! (xml)</finaltext>  
   </level>  
   <level>  
     <name>Level 2 (xml)</name>  
     <tutorial>Try comes close to all four objects, again. (xml)</tutorial>  
     <object name="Cube"> Hi, I'm a cube in the level 2! (xml)</object>  
     <object name="Cylinder"> Hi, I'm a Cylinder in the level 2! (xml)</object>  
     <object name="Capsule"> Hi, I'm a Capsule in the level 2! (xml)</object>  
     <object name="Sphere"> Hi, I'm a Sphere in the level 2! (xml)</object>  
     <finaltext>Congratulations! (xml)</finaltext>  
   </level>  
 </levels>

Any node "level" can represents a question for your game.

Try to study this method: http://unitynoobs.blogspot.com/2011/02/xml-loading-data-from-xml-file.html

 public void GetLevel()  
  {  
   XmlDocument xmlDoc = new XmlDocument(); // xmlDoc is the new xml document.  
   xmlDoc.LoadXml(GameAsset.text); // load the file.  
   XmlNodeList levelsList = xmlDoc.GetElementsByTagName("level"); // array of the level nodes.  
    
   foreach (XmlNode levelInfo in levelsList)  
   {  
    XmlNodeList levelcontent = levelInfo.ChildNodes;  
    obj = new Dictionary<string,string>(); // Create a object(Dictionary) to colect the both nodes inside the level node and then put into levels[] array.  
      
    foreach (XmlNode levelsItens in levelcontent) // levels itens nodes.  
    {  
     if(levelsItens.Name == "name")  
     {  
      obj.Add("name",levelsItens.InnerText); // put this in the dictionary.  
     }  
       
     if(levelsItens.Name == "tutorial")  
     {  
      obj.Add("tutorial",levelsItens.InnerText); // put this in the dictionary.  
     }  
       
     if(levelsItens.Name == "object")  
     {  
      switch(levelsItens.Attributes["name"].Value)  
      {  
       case "Cube": obj.Add("Cube",levelsItens.InnerText);break; // put this in the dictionary.  
       case "Cylinder":obj.Add("Cylinder",levelsItens.InnerText); break; // put this in the dictionary.  
       case "Capsule":obj.Add("Capsule",levelsItens.InnerText); break; // put this in the dictionary.  
       case "Sphere": obj.Add("Sphere",levelsItens.InnerText);break; // put this in the dictionary.  
      }  
     }  
       
     if(levelsItens.Name == "finaltext")  
     {  
      obj.Add("finaltext",levelsItens.InnerText); // put this in the dictionary.  
     }  
    }  
    levels.Add(obj); // add whole obj dictionary in the levels[].  
   }  
  } 

And to get the values:

 string lvlName = "";  
   levels[actualLevel-1].TryGetValue("name",out lvlName);  
     
   string tutorial = "";  
   levels[actualLevel-1].TryGetValue("tutorial",out tutorial);  
     
   levels[actualLevel-1].TryGetValue("Cube",out Cube_Character);  
   levels[actualLevel-1].TryGetValue("Cylinder",out Cylinder_Character);  
   levels[actualLevel-1].TryGetValue("Capsule",out Capsule_Character);  
   levels[actualLevel-1].TryGetValue("Sphere",out Sphere_Character);  
     
   string finaltext = "";  
   levels[actualLevel-1].TryGetValue("finaltext",out finaltext);  

Note: actualLevel is just a int value, you can increment it and pass with your answers.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by grahambarry · May 24, 2017 at 07:43 AM

I am a bit confused about how to access different nodes ([actualLevel-1])

  1. I would like to know how to access the level 2 node node.

  2. I would like to access the level 2 node

Thanks

,If I wanted to access the level 2 node and drill into object cylinder for example. What code would I need. I have this running but the array containing what looks like a range [actualLevel-1] I am confused about. Thanks

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

3D Text Object does not change to the color specified on Mouse Over 0 Answers

Fall down a line in Unity 4.6 text via script 3 Answers

What is wrong with my text changing script? 1 Answer

How do I refer to a .text from a separate scene. 0 Answers

Objects shows text above them 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges