QuizGame.Start()
In the class QuizGame, look for the TODO in the Start() function.
void Start()
{
...
{ // TODO load in the TextAsset for the specified "file"
}
...
}
- You can get the filename for the TextAsset with the static function
GameManager.GetXMLFile()- The
GetXMLFile()function just returns s_xmlFile, and that is currently empty"" - Change the
GameManager.s_xmlFilevariable to"Games"so that we will load the file Games.xml
- The
A static function does not refer to any instance of a class.
This lets us communicate a piece of data between scenes.
When a scene transitions, the objects are destroyed, but we want this data to pass from one scene to another.
We have used a static function to get the name of the file from one scene to another.
- Load the TextAsset with
Resources.Load<TextAsset>() - Use the TextAsset to construct a new StringReader
- Create an XmlSerializer of type QuizList like so:
new XmlSerializer(typeof(QuizList)) - Use the function
Deserialize()on your XmlSerializer using your StringReader as input - Set
m_listto the results of theDeserialize()- You will have to convert the output of
Deserialize()to be of type QuizList.
- You will have to convert the output of
- Release the TextAsset by calling
Resources.UnloadAsset()with it- Once the text has been read in and parsed, we do not need to keep the XML file loaded.
- We have to call
Resources.UnloadAsset()to release the memory.
QuizGame.NextQuestion()
Look for the TODO in the function NextQuestion().
bool NextQuestion()
{
bool ret = true;
{
// TODO advance to the next element in the list
// set m_text.text to the text of the new question
// If the list is NOT over, set ret = false
}
// reset the colors to the original colors for the question
m_text.color = m_origTextColor;
m_background.color = m_origBackgroundColor;
return ret;
}
- The list of questions can be found inside
m_list - The index of the current question is
m_listElement(it starts off as -1) - If we are not yet at the last question, advance to the next index
- Set the on-screen text element
m_text.textto the next question - Set the value of
rettotrueorfalseas appropriate- If you could NOT update
m_listElementbecause we’re already at the end, setret = true - If you DID update
m_listElementto the next question, setret = false
- If you could NOT update
You will have noticed that I’m putting these TODO blocks in block scopes. A block scope is just a block of code in brackets.
You can use these to organize code any way you want.
I find them useful because the contain the scope of variables.
A variable declared inside the scope cannot be accessed outside the scope.
This helps prevent accidental use of the wrong variables.
{ // TODO load in the TextAsset for the specified "file"
// this is a "block scope"
}
Give it a try.
The game will start, but you can’t get past the first question yet.
The first correct answer is “Tony Hawk”. 