1. Home
  2. Docs
  3. Publishers
  4. Tutorial – Service templates walkthrough
  5. Trivia multiplayer

Trivia multiplayer

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

It’s a more complex version of the Mini Film Trivia template and for a more in-depth walkthrough of the workflow, you can view the Mini Film Trivia walkthrough. This one is going to focus mainly on the features specific to the Trivia Multiplayer template and Trivia package in general.

How it works

The service always checks if the game was interrupted in the previous session. Four specific phases when the game can be interrupted are taken into account:

  • After the player number was set (playersTotal !== null)
  • After the player names were set (player !== null && count(player) == playersTotal)
  • During trivia rounds phase (questions !== null)
  • At the end of the game, during score output (variable finish is set to 1)

If the previous game wasn’t interrupted, the game moves on to the Set player count block. New game variables are initialized and the user has to select how many players want to play the game with one being the minimum and four the maximum number.

After the player names are set and the scores initialized to zero in the Set player names block, the game moves on to the Get questions block where players can choose between five categories. After that, the game moves 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 Intent Readers and additional processors like the Repeat question processor in the Other processors container.

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 player index
${status.user_index}

You have to provide the questions array expression, the status expression for evaluating the Correct answer, 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}

Since this is the multiplayer version we entered the player array expression in the Users field. The player array contains player names and scores and we can access both under status variable name during iteration.

Example: accessing player’s name
${status.user.name}

Lastly, there’s the Skip reset optional boolean expression.

We use the skip_reset variable to avoid resetting Trivia Round block status variables after the round was interrupted.

The question iteration stops after the last question is answered. The Done container executes and we move on to the Finish block. The finish variable is set to 1. It lets the game knows it was finished in case it’s interrupted before the session ends.

After that, the score is calculated and outputted using the Trivia Scores Reader.  The Trivia Scores Reader iterates through the players array, and based on the player scores executes either the Single, Multiple, or All container. The Single container is executed when there’s only one player, or the player’s score is unique. The Multiple container is executed when there are two or more players with the same score, and lastly, the All container is executed when all the players have the same score. The Trivia Scores Reader calculates all that for you. You just have to place the expression to evaluate the players array in the Players field. The array has to contain player names and player scores.

You should also enter the name of the name field under the Name field property, and the name of the score field under the Score field property.

This enables you to access different values under Status variable name. You can access the current player name, or names array when multiple players have the same score. You can also access score, rank, and check if it’s the first or the last ranking player.

Example: accessing rank
${status.rank }

The Trivia Multiplayer has separate workflows for cases when there’s only one player and cases when there are multiple players playing.

But, as you can see, you can use Trivia Scores Reader for both scenarios.

Lastly,  players are asked if they want to play again. If the answer is in the affirmative, player scores and some of the other variables are reset and players are sent back to the Get questions block. If the players answer in the negative, variables are reset for a new game, and the game exits.

Modifying the template

You can easily customize the number of question rounds per game, change the max number of players, add or remove categories and change the service name. Simply switch to the Variables panel and there you’ll see all the values you can modify.

Change the name by editing the SERVICE_NAME variable and the number of rounds by editing the MAX_ROUNDS variable.

To change the max number of players, edit the PLAYER_MAX variable.

We get our questions data from the Open Trivia Database API. We calculate the number of questions for API calls in the Load category and questions init fragment by multiplying the MAX_ROUNDS and totalPlayers variables.

To add a trivia category, you’ll need the category’s id. You can view all available categories and the corresponding id’s here: https://opentdb.com/api_category.php.

After you’ve selected the category you want to add, simply add a new CATEGORIES_ID sub-array containing the category name and id.

You’ll also have to add the new category to the CATEGORIES string.

Was this article helpful to you? Yes No

How can we help?