1. Home
  2. Docs
  3. Publishers
  4. Tutorial – Service templates walkthrough
  5. Mini trivia

Mini trivia

Mini Trivia is one of the templates you can select during service creation. You can use it to quickly create your own trivia game.

template

The upcoming sections will walk you through the template and show how easy it is to customize it.

How it works

In short, the service always checks if the game was interrupted in the previous session, and if it was, it asks the user if they want to continue where they left off. If they answer in the affirmative, the service skips resetting the variables from the last round and continues with the last unanswered question.

Otherwise, it automatically resets the variables, loads the questions again, and starts from the first question.

When the player says ‘Help’, the service provides instructions, and if they say ‘Cancel’ or ‘Stop’, the game exits at any point in the flow.

At the very end, the score is calculated, old questions are deleted and the player is asked if they want to play again. If the player answers in the affirmative, they start a new round with new questions. The game exits otherwise.

I’ll walk you through some of the possible scenarios.

Starting a new game

The player starts a new game if it’s their first time opening the skill or each time the previous game was fully completed.

session start block introWe start with the greetings in the Session start block. After that, we move on to the Home block. There, the If element evaluates if the questions array is not null. For a first-time player or a player that completed the previous game, it should be.

home block

In that case, the Else container is executed. Inside, the New game transition and reset fragment is executed. It resets the trivia variables and loads the questions using an API call to the Open Trivia DB.

load questions fragment

We store the quiz data in a multidimensional questions array. After that, we’re ready to move on to the Rounds block.

round block options

The Rounds block belongs to a special block type included only in the Trivia package called Trivia round block. The Trivia round block iterates through the questions array, evaluates player answer, and executes either the Correct answer given or the Incorrect answer given flow.

round block flowsIt has its own built-in intents but you can add additional intents and processors like the Run current block again processor in the Other processors container. It repeats the current question and answers to the player.

read out answersThe block provides the current questions array item and the current users array item under the Status variable name you define. You can also access the question_index, user_index, and boolean last_question. Status variables are most commonly used in LOOP blocks or elements as context variables for resolving iteration results. In this template, we simply named the variable ‘status’.

Example: accessing user index
{status.user_index}

questions array

You also have to provide the questions array expression, the status expression for evaluating the Correct answer (the position is the same as in the questions array, pictured above), and the status expression for evaluating the Correct letter answer.

Example: status expression for accessing the correct answer
${status.question.correct_answer.letter}

Example: status expression for accessing the answers array
${status.question.correct_answer.text}

You can provide the Users array expression if there are multiple players, and lastly, the Skip reset boolean expression which is optional as well. You set the Skip reset field to true if you don’t want the Trivia round block to reset the status variables after the round is interrupted. But we’ll get to that in the next section.

done flowThe question iteration stops after the last question is answered. The Done container executes and we move on to the Finish block.

The Finish block resets the questions array to null. Using the If/ElseIf/Else flow, the right score text variation is outputted to the player. If you’d like to turn this template into a multiplayer game, we recommend using the Trivia Scores element instead, part of the trivia package for easier score calculation and output. You’d need to provide an array of players containing player names and scores. However, we already provide a ready-to-go multiplayer template with our Trivia Multiplayer.

But let’s get back to this template. In the last step, the player is asked if they want to play again. If the answer is ‘yes’, we go back to the Home block and start the game again.

Continuing from the last round

Let’s presume the player exited the game on the third out of four questions. The next time they run the app, we first check if the question array is null. Since the question array is only reset at the end and the player hadn’t reached the Finish block, the array stored the questions from the previous round. The If Element evaluates to true and the Then container executes. Finally, the user is asked if they want to continue where they left off. If the user answers in the affirmative, the skip_reset variable is set to true.

continue

As mentioned in the previous section, setting the Skip reset boolean expression in the Trivia round block properties is optional. By default, the Trivia round block resets all the status variable values from the previous rounds.

skip reset

We don’t want that when the player interrupted the last round, so we created the ${skip_reset} expression in the Trivia round block properties and then set the skip_reset variable to true within the Yes flow of the Yes/No processor.

After that, we move on from the Home block to the Rounds block. The status values from the previous game are stored, and the score variable isn’t reset so the user continues the interrupted game.

From this point on, the game follows the same pattern as in the previous section.

Starting the game over

start overThe player can say ‘start over’ to start the game over at any point in the game due to the Start over processor in the Service processors special block. The game simply returns to the Home block, resets the score, and reloads the questions.

Modifying the template

You can easily customize the number of total questions per game or even change the trivia category. Switch to the Variables panel to change the number of questions or the service name.

variablesChange the name by editing the SERVICE_NAME variable and the number of total questions by editing the TOTAL_QUESTIONS variable.

We get our questions data from the Open Trivia Database API. To change the trivia category, you need the category’s id. You can view all available categories and the corresponding id’s here: https://opentdb.com/api_category.php

add categoryPick the category you want and add a new variable in the Variables panel containing the category id. Switch to the service Editor and on the right-hand side click the Fragments tab. Select the Load Questions fragment.

question categoryClick the OpenTDB Adapter Element and change the category in the Question Category field to match the new variable’s name.