Write shared variable
- Functions and limitations →
- Create a shared variable →
- Use a shared variable →
- Clear shared variables →
Usage examples:
- How to make a counter with addition and subtraction →
- How to make a poll with two options in the bot →
Functions limitations
The Write a shared variable component allows all bot users to interact with a variable that was recorded by one of the users. All users can read or change the value of such variables.
Data types and List Management work the same way in Write a shared variable and in a regular Write a variable.
The amount of shared variables in the bot should not exceed 3 megabytes.
Common variables are always relevant at the time of the request. If the first user requests a shared variable at the time when the second user changes its value, the first user will receive the old value, not the new one.
Create a shared variable
1. Add the Write shared variable component to the screen.
2. Come up with Name and Value for the variable, choose the appropriate type, or leave a String if you are going to write the text.
Use a shared variable
1. Add the Read shared variables component. The component should be added and correctly filled in, otherwise the shared variable will not be read.
2. Change or leave unchanged the Name of the variable where the result will be written.
3. In the Names of the shared variables to be read, add the variable name from the Write shared variable component that you filled in before.
Done. The written shared variable will be available to all users of the bot.
For the work of a shared variable, the first screen must be run once for one user. After that, the second screen will work for all users without activating the first one.
The variable is output to the text using the stringify helper:
{{stringify shared.door}}
Several variables will be output like this: {{shared.variable_name_1}} and {{shared. variable_name _2}}
If the result is written to the shared variable, as in our example, then the JSON output with the variable looks like this: {{stringify shared}}
If you complement the scenario, other users will be able to influence the variable value. For example, if one of the users passes the Assign a variable screen with the same name, but with a different value, then the value will change for everyone.
No matter where the variable is output or changed — next to the original assignment or somewhere separately in the bot - you always need to add the Read shared variables component before the output message. The variable will not be output without it.
The table with all common variables is located at the bottom of the Users section, in the Shared Variables section. Click Show shared variables.
Shared variables are displayed individually for each platform.
Re-upload shared variables button updates the list of shared variables.
Close shared variables button closes the table of common variables.
Shared variables are cleared only with the Clear shared variables component. You cannot delete them through the Users section.
Clear shared variables
Shared variables are deleted with the Clear Shared Variables component. This component works the same way as the regular Clearing variables.
If you need to delete shared variables from all users at once, you can send the Clear shared variables screen in a Broadcast message. If there is nothing else on this screen, then such a broadcast will be invisible to users.
Usage examples
How to make a counter with addition and subtraction
The starting point is 0. Each user will be able to add or subtract one from the starting point, that is from zero. The numbers after each addition or subtraction will be written to a shared variable. If you output this variable in the bot, then each bot user will have access to the overall results at any time of voting.
Write shared variable
1. Add the Write shared variable component to the screen.
2. Come up with any variable name.
3. Select the Number Type.
4. Write down the number from which the count starts into a shared variable, in our case it is 0.
This screen may not transfer anywhere. It needs to be run once in order for the variable, where the count will be kept, to be assigned to the bot. You can reproduce the screen separately from the main scenario with Template Event or by clicking on the link from the screen settings on the right panel of the bot editor.
If you run this screen again after receiving any results from users, the counter will be reset to zero and all results will disappear beyond recovery.
5. Add Buttons with the "plus" and "minus" options.
6. Add two new screens for plus and minus and make the transition from the buttons to these screens.
7. Add Write shared variable to the plus screen.
8. Copy the variable name from the first screen, in our case it is var1.
9. Change the type to Increment number, write the number by which you want to increase the original number in the value. In the example, the number will increase by 1, so the value will be 1.
10. Copy Write shared variable to the screen for the minus sign.
11. On the minus screen, change Type to Decrement number.
Read shared variables
Reading and then output shared variables is useful if you show users poll results after they make their choice.
1. Right after Write shared variable add the Read shared variables component to the same screens for plus and minus.
2. On both screens, you can change The name of the variable, where to write the result or you can leave it unchanged. In the example, we named this variable nums.
3. Write down the variable name from the first screen in the Names of shared variables to read you need to read field. In our case, you write var1.
4. And add a message with the results:
Thanks for the vote. The current post rating is {{stringify nums.var1}}
Done.
If the user does not need to see the result or the result should be displayed further in the bot, reading and output can be in another place or, for example, you can send it as Alert to the operator.
Scenarios can be any, but it is important not to forget the rule that the Read shared variables component should always be before the result output.
The bot administrator should reproduce the first screen from this scenario. After that, he can send out or add to the script a chain starting from the buttons.
If any user activates the first screen during the voting process, where zero is written to the variable, the counter will be reset and the results will be lost beyond recovery.
How to make a poll with two options in the bot
Users must select the channel avatar by a general vote. The option with the most votes will win.
Write shared variables
1. Add a new screen. Add two consecutive Write shared variable components on this screen.
2. In the first component, the Variable name will be blue, in the second component it will be white.
3. Change the type of both variables to Number.
4. Enter 0 in both components in the Value field.
This screen may not transfer anywhere. It needs to be run once in order for the variable, where the count will be kept, to be assigned to the bot. You can reproduce the screen separately from the main scenario with the Template Event or by clicking on the link from the screen settings on the right panel of the bot editor.
If you run this screen again after receiving any results from users, the counter will be reset to zero and all results will disappear beyond recovery.
Read shared variables
1. Create a new screen, add the Read shared variables component to it.
2. You can change The name of the variable, where to write the result or you can leave it unchanged. In the example, we changed the variable name to colors.
3. Click the Add button and add the Names of shared variables to read In our case, these are blue and white.
4. Add a Buttons component with two buttons to the same screen.
5. Since we have already added the Read Shared Variables component, you can output the results in the text in front of the buttons before the user votes:
Those who voted for the blue background: {{stringify colors.blue}} Those who voted for a white background: {{stringify colors.white}}
6. Add screens for button transitions, set these transitions from the buttons.
7. On the screen where the blue color selection button leads, add the Write shared variable component.
8. Add blue to the Name field.
9. Change Type to Increment number, add 1 to the value.
10. On the screen of the option with a white color, also add the Write shared variable component.
11. In the Name field, add the name of the second assigned variable. In our case, it's white.
12. Change Type to Increment number, add 1 to the value.
13. Copy the Read shared variables component that was added in front of the buttons if you want to show the user the result after his voice.
14. And add a message with the results:
Thanks for the vote!
Now the results are as follows:
For a blue background: {{stringify colors.blue}} For a white background: {{stringify colors.white}}
Done.
To prevent the user from voting a second time, add Remove a screen with buttons to the scenario or additionally assign ordinary variables to the users who voted to check them with Fork later.