Trivia Multiplayer is one of the templates you can select during service creation. You can use it to quickly create your own trivia multiplayer game.
It’s a more complex version of the Mini Trivia template and for a more in-depth walkthrough of the general workflow, you can view the Mini 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. If that is true (the finish variable must be null and the playersTotal variable has to have a value assigned), the user is asked if they want to continue where they left off. This prompt varies depending on at which point the game was interrupted. Once the appropriate phase is determined in the Read Phase of the Home block, the accompanying prompt is outputted to the user.
If the user answers in the affirmative – yes they want to continue, two phases of the game are taken into account in the Process Phase.
The player/s can continue onto the trivia rounds phase (questions !== null), or if only the player number had been set (players !== null || playersTotal !== null), they can continue to choosing the trivia category.
If however 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 being the maximum number.
After the number of players has been set, each player gets assigned a number and the game moves on to the Choose trivia category block.
There the player/s 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
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
Example: status expression for accessing the answers array
Since this is the multiplayer version we entered the players array expression in the Users field. The players array contains player names and scores and we can access both under the status variable name during iteration.
Example: accessing player’s 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 Trivia Round block stops the question iteration 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 was 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 all the players have a unique score. 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
The Trivia Multiplayer has separate flows for cases when there’s only one player and cases when there are multiple players playing.
And, 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 Choose trivia category 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, if it’s not already listed on the Variables panel, you’ll need to add 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 want to add the category’s name to the CATEGORIES variable, after the last SSML break.