Storing input to array
I am creating this quiz app and I'm having a little bit of trouble in storing the data and loading/displaying it afterwards. I will try to be as detailed as much as I can in explain my questions.
It's supposed to be this way
five text fields
1 for question
4 for choices
after user clicks save
the question will be stored in an array
new questions will be stored in the same array as the old questions
The way I structured the question and answers are like this:
Quiz is their parent
save button is not within quiz
I want to store/save the questions with its choices in an array so i can load them for future use.
EXTRA QUESTION:
Is there a way that I can randomize the order for the answers(when displayed)
For example inputs are
What is 2+2
1
2
3
4
Then it will display
What is 2+2
3
2
4
1
[EDIT]
Below is the class that accepts the user input for the question.
public class NewQuestion{
public static void getQuestion(string newQuestion)
{
Debug.Log("input" + newQuestion);
string question = newQuestion;
}
}
Below is one of the classes that accepts the input for the answers.
public class choiceA : MonoBehaviour
{
public void getAnswerA(string A)
{
Debug.Log("input" + A);
string optionA = A;
}
}
Below is the class where I am trying to call all the inputs when I click a save button.
[System.Serializable]
public class OnClickSave : MonoBehaviour
{
public string question;
public string optionA;
public string optionB;
public string optionC;
public string optionD;
public ArrayList[] items;
}
Answer by Namey5 · Sep 11, 2016 at 07:08 AM
Thank you for updating your question. It's actually one of the better one's I've seen on this website, it was just a bit difficult to see where you were going. This is only part of the question answered, I'm just posting this here as an update;
using UnityEngine;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
[System.Serializable]
public class Data
{
public int questionNumber;
public string question;
public string optionA;
public string optionB;
public string optionC;
public string optionD;
}
public class DataTest : MonoBehaviour
{
public Data[] inputData;
public Data[] data;
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown ("Fire1")) {
data = new Data[inputData.Length];
foreach (Data d in inputData)
Save (d, true);
}
if (Input.GetButtonDown ("Fire2"))
data = Load ();
}
void Save (Data d, bool s)
{
data[d.questionNumber] = d;
if (s)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.dataPath + "/data.dat");
bf.Serialize(file, data);
file.Close();
}
}
Data[] Load ()
{
if (File.Exists (Application.dataPath + "/data.dat"))
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.dataPath + "/data.dat", FileMode.Open);
Data[] d = (Data[])bf.Deserialize (file);
file.Close ();
return d;
}
else
{
return new Data[1];
}
}
}
This class should allow for the saving and loading of data. The great thing about this is, because we are using a custom class as a serializable container, we can save and load data as that format, meaning we don't have to do type conversions, etc. I'll work on the rest of the question when I have time.
Forgive my lack of punctuation. This is a very big help. Thank you.
The answer has been updated. Again, thank you for updating the question, and I will work more on this when I have time.
Thank you. It was late when I realized that there's a different rule with white spaces. :)