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

Mini film trivia

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

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

How it works

The service always checks if the game was interrupted in the previous session, and if it was, it skips resetting the variables from the last round and continues with the last unanswered question. Otherwise, it automatically resets the variable, 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, it starts a new round with new questions. It exits the game 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 game or each time the previous game was fully completed.

We start in the Session start block. 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.

The Else container is executed and after the brief greeting, it sends us to the Home block.

The Home block executes the Load Questions fragment which uses API to download film trivia questions and answers.

It checks if the response code was valid and then proceeds to store downloaded data in a multidimensional questions array. Each answer is attributed to a letter – a, b, c, or d.

Next, the Init Score fragment sets the score variable to zero and we move on to the Rounds block.

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.

It has its own built-in intents but you can add additional intents and processors like the Repeat Intent processor in the Other processors container. The Repeat Intent processor repeats the current question and answers to the player.

The 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": [
        {
            "question": "Which of the following is not the name of a \"Bond Girl\"? ",
            "answers": [
                {
                    "answer": "Wai Lin",
                    "letter": "a"
                },
                {
                    "answer": "Vanessa Kensington",
                    "letter": "b"
                },
                {
                    "answer": "Pam Bouvier",
                    "letter": "c"
                },
                {
                    "answer": "Mary Goodnight",
                    "letter": "d"
                }
            ],
            "correct": "b",
            "correct_answer": "Vanessa Kensington"
        },
        {
            "question": "What is the name of James Dean's character in the 1955 movie \"Rebel Without a Cause\"?",
            "answers": [
                {
                    "answer": "Frank Stark",
                    "letter": "a"
                },
                {
                    "answer": "Jim Stark",
                    "letter": "b"
                },
                {
                    "answer": "Jim Kane",
                    "letter": "c"
                },
                {
                    "answer": "Ned Stark",
                    "letter": "d"
                }
            ],
            "correct": "b",
            "correct_answer": "Jim Stark"
        },
        {
            "question": "Who plays Alice in the Resident Evil movies?",
            "answers": [
                {
                    "answer": "Madison Derpe",
                    "letter": "a"
                },
                {
                    "answer": "Milla Johnson",
                    "letter": "b"
                },
                {
                    "answer": "Milla Jovovich",
                    "letter": "c"
                },
                {
                    "answer": "Kim Demp",
                    "letter": "d"
                }
            ],
            "correct": "c",
            "correct_answer": "Milla Jovovich"
        },
        {
            "question": "Darth Vader's famous reveal to Luke is iconic. But which of these is the right one?",
            "answers": [
                {
                    "answer": "\"No. I am your father.\"",
                    "letter": "a"
                },
                {
                    "answer": "\"You're wrong. I am your father.\"",
                    "letter": "b"
                },
                {
                    "answer": "\"The truth is that I am your father.\"",
                    "letter": "c"
                },
                {
                    "answer": "\"Luke, I am your father.\"",
                    "letter": "d"
                }
            ],
            "correct": "a",
            "correct_answer": "\"No. I am your father.\""
        }
    ]

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}

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

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.

The 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 need to provide an array of players containing player names and scores. The element then calculates a player’s rank based on score and user names and loops through the collected information. You can access individual player rank, score, name and check whether it’s the first (winning) player or the last under the Status variable name.

Example: ${statusVariableName.rank}

But let’s get back to the template. Lastly, 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.

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.

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 Then container.

We move on from the Session start block to the Rounds block. The status values from the previous game are stored, and the score variable isn’t reset so the player continues the interrupted game.

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

Starting the game over

The 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. If the player answers in the affirmative to the question if they want to start over, the game goes back to the Home block and resets the score, and reloads the questions. If the answer is negative, the game continues from the last state.

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.

Change 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

Pick 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.

Click the JSON Reader and change the category in the URL field to match the new variable’s name.

Was this article helpful to you? Yes No

How can we help?