Skip to main content

Toggle Button

In this tutorial, we will create a button that will turn On and Off individual functions or menu items in your project, if the item is given an appropriate Condition.

For example, by pressing a button, your admin will be able to close access to certain sections of the menu for users, globally allow or prohibit receiving a Bonus, charge an additional bonus or include a temporary additional commission in any function, move the user to different sections of the menu. The number of ways to use the toggle is unlimited. In any place where something can be turned on or off - for the convenience of turning it on and off - you can use this Toggle Button.

A Toggle Button can be one of the admin's interface elements or a part of some game mechanics. The essence of this button is that it changes the state of the variable, setting a certain values ​​​​for it in a circle (the very value of this variable is a part of a Condition on the particular button or in some the function - determining the accessibility or inaccessibility, enabling or disabling of this element). 

Let's imagine that some menu item is closed using the Condition in the Global variable - the item is available if the value of the variable is 1 (one), and inaccessible if 0 (zero) - thus, by changing the value of this variable, you can open and close access to that element. For example, your admin manually turns on and off some temporary function dedicated to a particular event in your project, or temporarily activates a bonus button for use. So, the Toggle Button can be created for the convenience of the admin in a special section like "Admin Panel". Obviously, the value of the variable can also be changed manually through the Admin section, however, firstly, not all admins in your project may have access to that Admin section, and secondly, manual changes may cause errors due to the human factor.


  Purchase of functionality


All functionality is acquired  IN YOUR BOT, for this go to your bot:
☞ 🔐Admin | 🧩Extensions

IMPORTANT: The purchase of functionality for the bot is available only to the Creator (owner) of the bot. Admins CANNOT buy functionality!


You will need:

1. Variable - 1 piece
☞ 🔐Admin | 🧩Extensions | 📂Variables

2. Actions - 1 piece
☞ 🔐Admin | 🧩Extensions | 📂Actions

3. Plugin - Button Conditions
☞ 🔐Admin | 🧩Extensions | 📂Buttons

4. Plugin - Macros and Expressions for Buttons
☞ 🔐Admin | 🧩Extensions | 📂Buttons

5. Plugin - Bonus in Global Variable
☞ 🔐Admin | 🧩Extensions | 📂Buttons

6. An idea of ​​what a function addl() and bool_symb() is, from the Expression Guide.


❖ Creation of the toggle button


1. Necessary variables

1.1 First, let's create a global variable, the value of which will be used in the Condition.

Go to your bot:
☞ 🔐Admin | 🗄Variables | 📘Global | ➕Create Variable
Name: "access" - you can choose your own.

1.2 Since global variables are text variables, such a variable must be assigned an initial value. Therefore, without leaving the variable editor, assign it a value of 0 (zero).


2. Toggle

For clarity, let's imagine that you close and open access to some button of your bot's menu. The button condition checks the value of the access variable we have created. If the access is 0 (zero) - access is closed, and if 1 (one) - then it is open. The condition on the menu button will look like this: {%access% == 1}.

2.1 Creating the Toggle Button itself in the menu.

For information on how to create a button, see the Basic Functions Guide.

2.2 Without leaving the Buttons Editor (if you left, go back) press the inline Settings button [*].
Click "Assign Pre-Action":
● Select "Action of Numbers"
Variable to change: "access"
Operation type: Set Value
Value: {addl(%access%, 1, 2)}
Condition: none

2.3 You can add a message to the Toggle Button you have created with a status indicator:
• Message:
{bool_symb((%access% == 1), "✅ Access Granted", "❌ Access Denied")}

 For information on how to add a message, see the Basic Functions Guide.

As you understand, using this mechanics, you can not only open the access to some sections of the menu, but also, for example, transfer the user to different menu branches depending on the state of the variable. To do this, you need to add two Actions of Commands with different conditions to the desired button. And, depending on the Condition, each of these Actions will transfer the user to the right place - corresponding to his command, and the admin will be able to manage all this using the Toggle Button you have created.


❖ Multi-State Toggle Button


As you understand, the Toggle Button can have not only 2 states - 1 (one) is On and 0 (zero) is Off. The number of states in it depends on the "Overflow Boundary Limit" specified in the function addl().

To create a button with three states, use the following Expression in Action of Numbers:
Value: {addl(%access%, 1, 3)} - Variable states: 0, 1, 2.

To create a button with four states:
Value: {addl(%access%, 1, 4)} - Variable states: 0, 1, 2, 3.

 

And so forth...