Using Quiz Generator with Puppeteer's Trivia Quiz Game

Using Quiz Generator with Puppeteer’s Trivia Quiz Game

In this post, I'll show how to easily integrate Quiz Generator with Puppeteer's Trivia Quiz Game, to generate an endless stream of random questions.

There are many trivia games and quiz games templates in Unity’s Asset Store. One of the best of them is Puppeteer’s Trivia Quiz Game. In this post, I’ll show you how to easily extend the template to use the Quiz Generator, allowing you create a Quiz Game in unity, with an endless stream of random questions.

Read more about Quiz Generator >>

The first thing you need to do is to import the two packages – Trivia Quiz Game and Quiz Generator – to your project. The Trivia Quiz Game is a beautiful game template, with many options, which allows you to create any trivia game in Unity. To augment the template, we” use the Quiz Generator asset that will serve as our trivia questions generator, providing an endless stream of questions.

Let’s start by downloading the two assets from the Asset Store:

Preparing a scene for the new Game Mode

Next, you’ll need to create a new scene to show our game mode. A good starting point is to use the CS_GameLocalXML scene – you’ll just have to change the way that the quiz questions are generated. You can duplicate that scene, and remove the TQGDynamicXML component from the GameControllerLocalXML game object:

Quiz Generator Scene Duplicated from GameLocalXML

Integration with Quiz Generator

Next, you’ll need to implement a script that will generate the questions. The script below is based on the Category script. I’ve removed all the irrelevant fields and logic. Instead, I introduced the creation of the QuizBuilder (with the various question types). From it, we generate questions, and convert them to the Question and Answers objects that the template expects. Lastly, we assign our generated questions to the TQGGameController object.

[Serializable]
public class Generator : MonoBehaviour
{
	public int numberOfQuestions = 20;
	public int bonusPerQuestion = 100;
	public int timePerQuestion = 5;
	public int bonusIncrease = 100;
	public int timeIncrease = 2;
	public string generatorFile = "Assets\Quizes\Sample\Data\Geography.json";

	public Question[] questions;

	[HideInInspector]
	public bool createQuestions;

	internal bool quizChanged;

	public void OnValidate()
	{
		// The attributes were changed since the last time we created the questions
        quizChanged = true;

		// Create the list of questions based on the attributes we entered
		if (createQuestions)
		{
			CreateQuestions();

			// Reset the button so we can click on it again
			createQuestions = false;

			// The question list fits the attributes we set
			quizChanged = false;
		}
	}

	public void Awake()
	{
		// If we changed the math quiz attributes without updating the questions (didn't press CreateQuestion() button), then the question will be recreated to make sure they fit the new attributes
		if (quizChanged)
		{
			CreateQuestions();

			// The question list fits the attributes we set
			quizChanged = false;
		}
	}

	/// <summary>
	/// Creates the questions in the attached quiz game controller based on the attributes set in the math category
	/// </summary>
	public void CreateQuestions()
	{
		var qb = new QuizBuilder<GeoQuizData>(new List<IQuestionGenerator>
		{
			new CityIsCapitalOfWhichCountry(),
			new InWhichCountryIsCityLocated(),
			new InWhichCountryIsLanguageSpoken(),
			new InWhichCountryIsReligionPracticed(),
			new WhatLanguageIsSpokenInCountry(),
			new WhatReligionIsPracticedInCountry(),
			new WhichCityIsLocatedInCountry(),
			new WhichCityIsTheCapitalOfCountry(),
			new WhichCountryBordersWithCountry(),
			new WhichCountryBordersWithSea(),
			new WhichCountryDoesNotBordersWithSea(),
			new WhichSeaBordersWithCountry(),
			new InWhichCountryIsPOILocated(),
			new WhichPOIIsLocatedInCountry(),
			new InWhichCityIsPOILocated(),
			new WhichPOIIsLocatedInCity(),
			new WhenDidCountryDeclareIndependence(),
			new InWhichCountryIsCurrencyUsed(),
			new WhatCurrencyIsUsedInCountry(),

			new WhichTwoCountryShareABorder(),
			new WhichTwoCountryShareABorderWithTheSameNamedSea(),
			new WhichTwoCountryShareABorderWithTheSameSea(),
			new WhichTwoCountryShareABorderWithTheSameSeaVar2()
		});

		qb.LoadFromFile(generatorFile);

		var questionsGenerator = qb.Build();

		// Create a list of questions based on the total number of questions in the math quiz
		questions = new Question[numberOfQuestions];

		var questionsFromGenerator = questionsGenerator.GetQuestions();
		var questionsEnumerator = questionsFromGenerator.GetEnumerator();
		// Go through the list of questions we created and fill them up
		for (var index = 0; index < questions.Length; index++)
		{
			questionsEnumerator.MoveNext();
			var generatedQuestion = questionsEnumerator.Current;

			// Declare the new question, so that we can fill it with info
			var currentQuestion = questions[index] = new Question();

			// Create a list of answers based on the number of answers per question
			currentQuestion.answers = new Answer[generatedQuestion.AllAnswers.Count];

			// Go through the list of answers we created and fill them up
			var indexB = 0;
			foreach (var generatedAnswer in generatedQuestion.AllAnswers)
			{
				// Declare the new answer, so that we can fill it with info
				var currentAnswer = currentQuestion.answers[indexB] = new Answer();

				// Set the answer value
				currentAnswer.answer = generatedAnswer.Name;

				// This answer is correct
				currentAnswer.isCorrect = generatedAnswer == generatedQuestion.CorrectAnswer;

				indexB++;
			}

			// Show the full question, with numbers, operation, and result
			currentQuestion.question = generatedQuestion.Question;

			// Fill out the bonus based on the level we are in
			currentQuestion.bonus = bonusPerQuestion + Mathf.FloorToInt(index / numberOfQuestions) * bonusIncrease;

			// Fill out the time based on the level we are in
			currentQuestion.time = timePerQuestion + Mathf.FloorToInt(index / numberOfQuestions) * timeIncrease;
		}

		// If we have a Quiz GameController, fill it up with the questions
		if (GetComponent<TQGGameController>())
		{
			GetComponent<TQGGameController>().questions = questions;
		}
	}
}

To use this script, just add it to the scene’s TQGGameController object (instead of any other questions generating script, such as the TQGDynamicXML script).

Extending the integration

The script above was kept simple on purpose, to demonstrate the integration. Once you’ve got things going, here are a few extra things you can do:

  • Support images – you could include images (or images URLs) as part of your quiz objects, and use them in the template to generate image-based questions
  • Difficulty levels – by keeping a complexity level on quiz objects and questions, you can grant a different bonus based on the question’s difficulty, or just sort the questions by their difficulty
GET NOTIFIED

Sign up to receive notifications when a new Unity Asset is released.