VeryVariable: Toggle Between Two States

  • 48 favourites

Stats

4,908 visits, 7,931 views

Translations

This tutorial hasn't been translated.

Tools

Having a toggle switch is one of the most useful tools to have when creating any kind of game. It allows one button to be used to change between two different states.

In this example, we will make a sprite switch between being visible and invisible.

This is what you'll need to do:

Let's break this down.

At the top line is a global variable. This is a variable that can be accessed by any part of the event sheet. Right-click on a blank part of the event sheet to find the option in the menu. I called the variable "toggle_switch" and made it equal to zero.

The first two events are made to switch the variable between 0 and 1. The first event (1) says, on a mouse event left-click the system will add 1 to the "toggle_switch". The second event (2) says, when the "toggle_switch" is greater than 1 it will become 0.

Now that we can switch the variable between 1 and 0 we want to let the system know what each one means. So the variables were set to certain states of the sprite. When the "toggle_switch" is 0 the sprite is visible (3) and when it is 1 the sprite is invisible (4).

If you test it out, every time you click it should switch back and forth between the two states.

Various Ways to Use: Beginner

Actual switches

This can be used to make actual switches in a game. For example, when you click on the red button it will turn the light on or off.

Changing animation frames

For each sprite you can have different frames of animation. With a toggle switch in place you can have a character change between two different frames.

Changing in-game conditions

Toggle switches don't have to be used only for sprites, but can also be used to change the conditions of the game. For example, it can change time scale. When the switch is 0 the speed is normal, but when the switch is 1 the speed is twice as fast or twice as slow. This way when you click you can go back and forth between how fast the game is playing.

Various Ways to Use: Advanced

Multiple states

Toggles typically have two states, but it can always have more. Here I used the add feature to make the states sequential. It can go from state 0 to 1 to 2 to 3 and back to 0. By using other mathematical functions it can be toggled in other ways, such as randomly or in reverse sequence.

Multiple variables

The main function of a toggle switch is to switch back and forth between different states, but with additional variables it could also create other effects. For example, every click the "toggle_switch" variable will change from 0 to 1 to 0, but a "click_count" variable could add 1 for every click. So if you switched from 0 to 1 to 0 to 1 to 0, the "click_count" variable will be 4 (because it is 0 by default) while the "toggle_switch" variable only changes between 0 and 1.

Additional variables create more numbers for you to work with and add even more depth into the game experience.

  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!